Rigidbody #2: Rotational Forces
Now that we have linear forces acting upon the 2D objects in our scene, it is about time we incorporate rotational forces into our simulation. Please keep in mind that I do not go too into depth on why the rotational quantities and formulas are the way that they are. If you are looking for this information, I have linked to some great articles on physics (specifically Chris Hecker's articles!) on the home page.
What are rotational forces?
Rotational forces help us to model how an object is rotating around its center of mass. The great part is that they have a lot in common with their linear counterparts: torque (τ) is the rotational equivalent of linear force; angular acceleration (α) is the rotational equivalent of linear acceleration; angular velocity (ω) is the rotational equivalent of linear velocity; moment of inertia (I) is the rotational equivalent of center of mass; and the current rotation of an object is the equivalent of the position of the object. In two dimensions, all of the angular quantities can be defined using a single floating point number (in 3D, this gets a little more complicated).
The Data Structure
Rotational forces introduce three new quanities into our Rigid Body data structure:
- Rotational Velocity (float): Much like linear velocity, this value defines the rate at which our object is currently rotating around its center of mass.
- Rotation (float): Much like position in the linear world, this value describes the current rotation of our object around its center of mass.
- Moment of inertia (float): This quanitity defined how much our object wants to rotate around its center of mass. A higher moment of inertia means an object is less likely to rotate, while a lower moment of a inertia means an object is more likely to rotate.
struct Rigidbody {
int32 numImpulses = 0;
Impulse activeImpulses[NUM_IMPULSES];
Vector2 velocity = { 0, 0 };
Vector2 position = { 0, 0 };
float32 mass = 1.f;
float32 rotationalVelocity = 0.f;
float32 rotation = 0.f;
float32 momentOfInertia = 1.f;
...
}
Now, that's enough for the data side, let's see how we can input some forces into this data structure to make it move.
The Functions
When we inputted a force into our system in the first rigidbody example, we simply told the system "apply this force to the whole object over time." With the introduction of the rotational quantities into our model, we now say "apply this force at this particualr point of the object over time." This point is what we'll call the point of application. Our Impulse struct from last time will now look something like this:
struct Impulse {
Vector2 force = { 0, 0 };
Vector2 pointOfApplication = { 0, 0 };
float32 timeOfApplicationSeconds = 0.25f;
float32 timeAppliedSeconds = 0.f;
bool isDead = false;
};
So what's the big deal about including this point of application? Well, from physics, we get the following formula:
One thing to note is that, in 2D, we do not have the cross product, per se. The closest thing we can do is get the vector perpendicular to another vector using the following function:
Vector2 getPerp() {
return { y, -x };
}
Now that we've calculate torque, we just need to get angular acceleration, which, when differentiated with respect to time, will yield the velocity and position. Angualr acceleration relates to torque according to this formula:
With all of this in mind, we're ready for the code that puts it all together!:
struct Impulse {
Vector2 force = { 0, 0 };
Vector2 pointOfApplication = { 0, 0 };
float32 timeOfApplicationSeconds = 0.25f;
float32 timeAppliedSeconds = 0.f;
bool isDead = false;
};
const int32 NUM_IMPULSES = 4;
struct Rigidbody {
int32 numImpulses = 0;
Impulse activeImpulses[NUM_IMPULSES];
Vector2 velocity = { 0, 0 };
Vector2 position = { 0, 0 };
float32 mass = 1.f;
float32 rotationalVelocity = 0.f;
float32 rotation = 0.f;
float32 momentOfInertia = 1.f;
void reset() {
numImpulses = 0;
velocity = { 0, 0 };
rotationalVelocity = 0.f;
rotation = 0.f;
}
void applyImpulse(Impulse i) {
if (numImpulses > NUM_IMPULSES) {
printf("Unable to apply impulse. Buffer full.\n");
return;
}
activeImpulses[numImpulses] = i;
numImpulses++;
}
void applyGravity(float32 deltaTimeSeconds) {
velocity += (Vector2 { 0.f, -9.8f } * deltaTimeSeconds);
}
void update(float32 deltaTimeSeconds) {
applyGravity(deltaTimeSeconds);
Vector2 force;
float32 torque = 0.f;
for (int32 idx = 0; idx < numImpulses; idx++) {
Impulse& i = activeImpulses[idx];
float32 nextTimeAppliedSeconds = i.timeAppliedSeconds + deltaTimeSeconds;
if (nextTimeAppliedSeconds >= i.timeOfApplicationSeconds) {
nextTimeAppliedSeconds = i.timeOfApplicationSeconds;
i.isDead = true;
}
float32 impulseDtSeconds = nextTimeAppliedSeconds - i.timeAppliedSeconds;
Vector2 forceToApply = i.force * (impulseDtSeconds / i.timeOfApplicationSeconds);
force += forceToApply * impulseDtSeconds;
// New! Increment the torque for each force
torque += i.pointOfApplication.getPerp().dot(forceToApply);
i.timeAppliedSeconds = nextTimeAppliedSeconds;
}
Vector2 acceleration = force / mass;
velocity += (acceleration * deltaTimeSeconds);
position += (velocity * deltaTimeSeconds);
// New! Update the rotational velocity as well
float32 rotationalAcceleration = torque / momentOfInertia;
rotationalVelocity += (rotationalAcceleration * deltaTimeSeconds);
rotation += (rotationalVelocity * deltaTimeSeconds);
for (int32 idx = 0; idx < numImpulses; idx++) {
if (activeImpulses[idx].isDead) {
for (int j = idx + 1; j < numImpulses; j++) {
activeImpulses[j - 1] = activeImpulses[j];
}
idx = idx - 1;
numImpulses--;
}
}
}
};
Live Example
This demo is much like the one in the first rigidbody example, with the addition of torque.