Bodies, Forces and Mass
A rigid body is an object whose shape never changes: a crate, a barrel, a car chassis. The engine doesn't care what it looks like; it stores a small block of numbers and updates them every step. This lesson builds that block and the rules that change it: forces, impulses and damping.
The state of a body
| Field | Meaning | Changes when |
|---|---|---|
position |
center of mass in world space | every step, from velocity |
velocity |
meters per second | forces, impulses, contacts |
invMass |
1 / mass, or 0 for static bodies |
rarely (set at creation) |
force |
the sum of forces applied this step | cleared after each step |
orientation, angularVelocity, invInertia |
the rotational twins of the above | see Rotation, Torque and Inertia |
The position and velocity are vectors, so everything in Vectors applies: adding a velocity times dt to a position is the same p + v * dt you saw there.
Forces accumulate, then integrate
Newton's second law, F = m * a, says a force changes velocity in proportion to the inverse mass: a = F * invMass. Engines don't apply each force immediately. Gameplay code calls AddForce any number of times during a step, the engine sums them into an accumulator, and the integrator uses the total once:
#include <iostream>
#include <iomanip>
struct Vec2 { double x, y; };
Vec2 operator+(Vec2 a, Vec2 b) { return {a.x + b.x, a.y + b.y}; }
Vec2 operator*(Vec2 v, double s) { return {v.x * s, v.y * s}; }
struct Body {
Vec2 position{0, 0}, velocity{0, 0}, force{0, 0};
double invMass = 1;
void addForce(Vec2 f) { force = force + f; }
void integrate(double dt) {
velocity = velocity + force * (invMass * dt); // a = F / m
position = position + velocity * dt;
force = {0, 0}; // forces last exactly one step
}
};
int main() {
std::cout << std::fixed << std::setprecision(3);
Body rocket;
double mass = 2.0;
rocket.invMass = 1 / mass;
const double dt = 0.1;
for (int step = 1; step <= 5; step++) {
rocket.addForce({0, -9.81 * mass}); // gravity: weight = m * g
rocket.addForce({4, 30}); // engine thrust
rocket.integrate(dt);
std::cout << "step " << step << ": pos (" << rocket.position.x << ", " << rocket.position.y
<< ") vel (" << rocket.velocity.x << ", " << rocket.velocity.y << ")\n";
}
}step 1: pos (0.020, 0.052) vel (0.200, 0.519)
step 2: pos (0.060, 0.156) vel (0.400, 1.038)
step 3: pos (0.120, 0.311) vel (0.600, 1.557)
step 4: pos (0.200, 0.519) vel (0.800, 2.076)
step 5: pos (0.300, 0.778) vel (1.000, 2.595)
Gravity is added as m * g and then divided by m again, so every body falls with the same acceleration. That is why engines usually apply gravity directly as an acceleration (velocity += gravity * dt) and skip the multiply and divide. Box2D, PhysX, Jolt and Godot all treat gravity this way, with a per-body gravity scale.
Force or impulse?
A force acts over time: it changes velocity by F * invMass * dt, so the effect depends on the step length. An impulse is an instant change of momentum, J = m * Δv: it changes velocity by J * invMass, regardless of dt. Engines expose both (Unity's ForceMode.Force vs ForceMode.Impulse, Unreal's AddForce vs AddImpulse, Godot's apply_force vs apply_impulse):
| Use | Choose |
|---|---|
| Something that pushes continuously: engines, wind, buoyancy, springs | force, applied every step |
| Something that happens once: a jump, an explosion, a hit | impulse, applied once |
The mistake to avoid: applying a jump as a force for one step. The jump height then depends on the step length, and it changes the day someone edits the fixed timestep.
#include <iostream>
#include <iomanip>
double jumpVelocity(double dt, bool asImpulse) {
const double mass = 70, strength = 350; // 350 N·s of jump impulse, or a 350 N "force"
double vy = 0;
if (asImpulse) vy += strength / mass; // Δv = J / m, independent of dt
else vy += strength / mass * dt; // Δv = F / m * dt, one step of force
return vy;
}
int main() {
std::cout << std::fixed << std::setprecision(3);
for (int hz : {30, 50, 60, 120}) {
double dt = 1.0 / hz;
std::cout << hz << " Hz: impulse -> " << jumpVelocity(dt, true) << " m/s, one-step force -> "
<< jumpVelocity(dt, false) << " m/s\n";
}
}30 Hz: impulse -> 5.000 m/s, one-step force -> 0.167 m/s
50 Hz: impulse -> 5.000 m/s, one-step force -> 0.100 m/s
60 Hz: impulse -> 5.000 m/s, one-step force -> 0.083 m/s
120 Hz: impulse -> 5.000 m/s, one-step force -> 0.042 m/s
Momentum is conserved
When two bodies push on each other, the impulses are equal and opposite, so the total momentum m₁v₁ + m₂v₂ doesn't change. That property is what makes collision response believable, and it is also a good debugging check: if total momentum drifts in a closed system, something applies an impulse to one body only.
#include <iostream>
#include <iomanip>
int main() {
std::cout << std::fixed << std::setprecision(3);
double m1 = 1, v1 = 4; // a 1 kg ball moving right
double m2 = 3, v2 = 0; // a 3 kg ball at rest
std::cout << "before: v1=" << v1 << " v2=" << v2 << " momentum=" << m1 * v1 + m2 * v2 << "\n";
// A perfectly inelastic hit (they stick together): both end with the same velocity.
double j = (v2 - v1) / (1 / m1 + 1 / m2); // impulse on body 1 (body 2 gets -j)
v1 += j / m1;
v2 -= j / m2;
std::cout << "impulse " << j << " N*s\n";
std::cout << "after: v1=" << v1 << " v2=" << v2 << " momentum=" << m1 * v1 + m2 * v2 << "\n";
}before: v1=4.000 v2=0.000 momentum=4.000
impulse -3.000 N*s
after: v1=1.000 v2=1.000 momentum=4.000
The formula for j is a preview of Impulses and Restitution: divide the velocity difference by the sum of the inverse masses, and apply the result to both bodies with opposite signs.
Damping
Real objects lose energy to air and friction. Engines approximate this with linear and angular damping, a per-body number that drains velocity every step. A common form, used by Box2D among others, is v *= 1 / (1 + c * dt), which is stable for any c (a naive v -= c * v * dt reverses the velocity when c * dt > 1).
#include <iostream>
#include <iomanip>
int main() {
std::cout << std::fixed << std::setprecision(3);
const double dt = 1.0 / 60.0;
for (double c : {2.0, 60.0, 150.0}) { // c * dt = 0.033, 1.0 and 2.5
double vNaive = 10, vStable = 10;
std::cout << "damping " << c << ":\n";
for (int i = 1; i <= 4; i++) {
vNaive -= c * vNaive * dt;
vStable *= 1 / (1 + c * dt);
std::cout << " step " << i << ": naive " << std::setw(8) << vNaive << " stable " << vStable << "\n";
}
}
}damping 2.000:
step 1: naive 9.667 stable 9.677
step 2: naive 9.344 stable 9.365
step 3: naive 9.033 stable 9.063
step 4: naive 8.732 stable 8.771
damping 60.000:
step 1: naive 0.000 stable 5.000
step 2: naive 0.000 stable 2.500
step 3: naive 0.000 stable 1.250
step 4: naive 0.000 stable 0.625
damping 150.000:
step 1: naive -15.000 stable 2.857
step 2: naive 22.500 stable 0.816
step 3: naive -33.750 stable 0.233
step 4: naive 50.625 stable 0.067
With damping 60 the naive version stops dead in one step (1 - c * dt = 0), and with 150 it flips sign every step and grows: the body shakes itself to infinity. The stable form just slows down faster as c grows. Use damping to tune feel (a floaty spaceship, a heavy boulder), not to hide instability: if a simulation explodes, damping only makes the explosion slower.
Tip: Unity 6 renamed the old
dragandangularDragproperties tolinearDampingandangularDamping, andvelocitytolinearVelocity. Old tutorials still use the previous names.
Test yourself
Why are forces cleared at the end of each physics step?
- To save memory
- A force only acts for the step in which it was applied; continuous forces are re-applied every step
- Because forces are converted to impulses
- So gravity can be added later
A jump is implemented as a force applied for one physics step. What happens when the fixed timestep is halved?
- The jump is twice as high
- The take-off velocity halves, so the jump is much lower
- Nothing changes
- The jump becomes an impulse automatically
Why is v *= 1 / (1 + c * dt) preferred over v -= c * v * dt for damping?
- It is faster
- It never reverses or amplifies the velocity, even for large c·dt
- It conserves momentum
- It works only with fixed timesteps