Here is a summary of what I did in this week!
- Implemented the World class for rigid bodies with its fixed time stepping.
- Implemented a reset feature to all the demos. Pressing R would reset a demo.
- Implemented Demo class for rigid bodies and set up a demo scene for visualizing rigid body dynamics.
- Implemented the RigidBody and Orientation classes with the rotation math.
- Implemented the integrator for rigid bodies with logic to handle both, forces and torque.
- Visualized the performance of the integrator by adding impulses to a rigid body in the Torque Demo.
- Implemented Force Generators for rigid bodies and tested the Gravity force generator.
- Implemented Anchored Spring force generator. Visualized the anchored spring in the Torque Demo.
- Implemented memory management of force generators and rigid bodies and their registration to the associated World.
The World class is the core class which manages the simulation. A world (exactly one) must be created before creating any rigid bodies, force generators, etc. Once the world is created it will share pointers to all the entities in the simulation and perform their update.
For now, the responsibility of the world is to update the forces generated through all the force generators registered to it each physics update. And while applying to forces to the rigid bodies, call the integrator on each rigid body. Later, the world would also perform collision detection, contact detection and collision resolution. Thus, the world update performs the update on all the physics entities in this simulation.
A rigid body is the base class for all the physical objects in the simulation. It is analogous to the Particle class in the mass-aggregate engine. However, it is different from particles in its very nature. We treat rigid bodies as point masses at their center of mass. Thus, the position of the rigid body is at the location of its center of mass. However, a rigid body can have an orientation apart from its position, which I’ll talk more about in detail. Analogous to particles, it has a inverse of mass, linear damping and velocity in it. But due to the role of force which can act along paths other than the one which passes through the center of mass, it also has inverse of inertia, angular damping and angular velocity (rotation) to account for.
Similar to particles, forces can be added to the rigid body, these forces are accumulated in the force accumulator. However, rigid body’s also support adding force at a specific point on it in the world space, and this can introduce torque. Thus we have a torque accumulator as well, which can be used to solve for angular acceleration using D’Alembert’s law for rotation.
The integrator for rigid bodies works the same as for particles. However, it also estimates angular acceleration along with linear acceleration. And uses it to estimate angular velocity and the orientation for the rigid body. Once the rotation and rotation are updated, the acceleration (both angular and linear) and cached and the accumulators are cleared for the next update call.
In 2D, the Orientation may be represented as a real number with the value as the angle between the rigid body and the positive x-axis which increases along with counterclockwise direction. However, it is more useful to store the angle as a vector of its cosine and sine.
The orientation class provide the angle between the x-axis and the rigid body in degrees in the ranges between [0,2π]. And the user can set or get the orientation in both degrees or as a orientation vector with the x-component as the cosine of the angle, and y-component as the sine of the angle. The orientation class also has a method to transform an orientation by certain degrees.
Each rigid body has a position and orientation which together represent its transform.
The force generators are a mechanism to add forces to rigid body on each update call. I have implemented gravity and anchored spring force generators for the torque demo.
Each force generator registers and unregisters itself from the world and on each update call of the world, they update the forces acting on their associated rigid bodies. Since the rigid bodies have a mechanism to add forces at specific positions, they may produce torque as well. Thus, these force generators also act as torque generators. The force generators act on rigid bodies at the start of each physics update, before they are integrated.
Anchored Spring Visualization