This post is part of my Game Physics Series.
Before delving deep into the programming aspect of physics engines, I would like to first go over the fundamentals of motion dynamics. Unless specified otherwise, all discussion is in 3D. Also, when it comes to formula consisting of vectors and matrices, all vectors are considered to be column vectors.
Position & Orientation
Position and orientation of a rigid body are both positional properties. Position is a linear property that defines where the object is located in space, and orientation is an angular property that defines how the object is oriented.
Position is typically represented as a vector, and orientation in 3D can be represented by a 3D vector (the direction is the rotation axis, and the magnitude is the rotation angle), a 3-by-3 matrix (basis vectors as columns), or a quaternion.
I prefer storing a 3-by-3 orientation matrix and, in addition, its inverse. These two matrices are used to transform vectors between world space (global coordinates) and model space (rigid body’s local coordinates), an operation that would be carried out quite frequently within a single time step.
Linear Velocity & Angular Velocity
Linear velocity, denoted , is the rate of change of position, denoted . The position change over a time step is the integration of velocity over time. Using the Euler Method, the formula is as follows:
Angular velocity, denoted , is the rate of change of orientation, denoted . In 2D, angular velocity is just a scalar, and the change of orientation is very similar to the formula for position change. Again, the following formula uses the Euler Method.
However, this gets trickier in 3D. In 3D, angular velocity is typically represented as a 3D vector, where its direction is the axis of rotation, and its magnitude is the rotation angle. True, if the representation of choice for orientation is also a 3D vector, then the formula for change of orientation would be the same as the 2D case (as shown above). However, if the orientation is represented as a 3-by-3 matrix, then computing the change of orientation involves first converting the angular velocity vector into a rotation matrix and then prepend that matrix to the orientation matrix.
where is normalized, is the magnitude of , and is the angle-axis rotation matrix with axis and angle .
Force & Torque
When you apply a force, denoted , to a rigid body, it would change its velocity and possibly, depending on the point of force application, its angular velocity.
Each force application corresponds to a resulting torque, denoted . If the torque is non-zero, then the angular velocity is changed. Torque is defined as the cross product between the vector from center of mass to the force application point, denoted , and the force vector.
Mass & Moment of Inertia
Mass, denoted , is a physical property that defines how difficult it is to move an object. The larger the mass, the harder it is to move an object.
Moment of inertia is the angular counterpart of mass that defines how difficult it is to rotate an object along a certain axis. With different rotation axis of choice, the moment of inertia might be different. To fully describe the moment of inertia of an object with respect to any arbitrary axis, we usually use a 3-by-3 matrix called “inertia tensor”, denoted . The moment of inertia with respect to an arbitrary axis is then:
where is the transpose of the column vector . You can find a list of inertia tensors of some common primitive shapes here.
For other shapes, the inertia tensor can be calculated using the formula below:
where is the entire volume of the shape, is the infinitesimal volume within at , and is the density of . Note that are the coordinates relative to the reference point of choice.
Linear Momentum & Angular Momentum
Linear momentum, denoted , is a physical property that defines how difficult it is to change an object’s linear motion. Linear momentum is defined as the product between mass and linear velocity.
Angular momentum, denoted , defines how difficult it is to change an object’s angular motion. Angular momentum is defined as the product between the inertial tensor and angular velocity. Note that this quantity is a 3D vector, as the inertia tensor is a 3-by-3 matrix and the angular velocity is a 3D vector.
Linear Impulse & Angular Impulse
The integrals of force and torque over time are respectively defined as linear impulse, denoted , and angular impulse, denoted . Linear impulse is also equal to the change of linear momentum, and angular impulse is also equal to the change of angular momentum, hence the deltas in their notations. Using the Euler Method, we get:
Applying linear impulse and angular impulse would change the velocity and angular velocity, respectively.
The change of linear velocity due to a linear impulse is the linear impulse divided by mass or, in other words, product between inverse mass and linear impulse.
The change of angular velocity due to an angular impulse is the product between the inverse of inertia tensor and angular impulse. Note that we have to convert the angular impulse to the object’s model space and then convert the change of angular velocity back to world space, since the inertia tensor is computed in the object’s model space.
where is the object’s orientation matrix. Since is orthogonal, is equal to .
End of Motion Dynamics Fundamentals
That’s it. I have covered the aspects of motion dynamics you’ll need for building a physics engine. I strongly encourage you to get very familiar with these definitions and relationships between properties, so familiar that they become your second instinct. After all, you probably don’t want to be bothered by these fundamentals when you really want to focus on the physics engine.