- This section deals with unconstrained 2D physics, i.e. steps 2 and 5 of the above algorithm overview. It's straightforward, rather simple mathematics, but definitely worth writing down.
- A body's state in 2D is defined by its position (, ), orientation (), linear velocity (, ) and angular velocity ()
- the acceleration vector for a body consists of with
- and : linear acceleration
- : angular acceleration
- : the body's mass
- : a body's moment of inertia (rotation equivalent of mass)
- the analytical derivation can be complex; it's a surface interval,
- for a 2D box with width W, height H, and uniform density , the solution is
- you can find precomputed formulas for other body shapes here
- you can also compute it numerically, by uniformly sampling points within the body and summing up the integrand, weighted by your sampling cell size. I'll only use boxes and employ the formula above.

- : the torque - rotation equivalent of force, defined as , with
- the vector from the body's center of mass to the point where the force is applied
- denoting the cross product; in 2D, the equivalent is
- i.e. torque is a function of force and force application point, not an independent variable. In other words, a 2D force vector is expanded into a 3D vector . This is also important to understand when computing constraint forces - there is no such thing as a constraint torque that is directly computed. One computed 2D constraint forces, and the constraint torque is just a function of this force.

- the body's velocity is , and it can be derived from the previous velocity by numerical integration schemes such as Euler's method:
- its position and orientiation is and is similarly computed by integration,
- For drawing, another formula is needed. A body is usually described by the absolute (world) position of its center of mass, its orientation, and the (constant) relative positions of its vertices. The absolute position of a vertex given in relative (to the center of mass, with zero rotation) coordinates for a body centered at and orientation , is , with the 2D rotation matrix
- One mostly deals with the inverse of the mass matrix, so bodies usually store the inverse of their mass and the inverse of the moment of inertia. Also, this simplifies dealing with immovable objects like ground planes and walls - the inverse of their mass and of their moment of inertia is simply set to 0, meaning no forces (and for collisions, no impulses) will affect them.
- In source code, one might not use vector and matrix notation as strictly as above - e.g., since the mass matrix is always diagonal, it is faster to implement it as a mass vector that is multiplied with acceleration element-wise; and in 3D, the inertia tensor becomes a 3D matrix. I will use this notation, however, because I believe it is better and shorter for didactic reasons.
- Using the above formulas, one can implement a simple physics engine for unconstrained motion, see the end of this page.
- Unconstrained motion in 3D is a bit more complex - in particular, rotations - but not that hard, either. David Baraff wrote a great document about it for SIGGRAPH2001 (link to the course homepage with additional material). I actually learnt the 3D portion first with Baraff's tutorial, then went back to 2D because visualization is easier.

mass (kg): | ||

x0 (m): | ||

y0 (m): | ||

theta0 (rad): | ||

vx0 (m/s): | ||

vy0 (m/s): | ||

vAng0 (rad/s): | ||

forceX (Newton): | ||

forceY (Newton): | ||

forcePoint: | ||

tStop (s): | ||

t=0s

Most of the relevant code is in unconstrained_physics.js, except for a small math library for vector operations, which is in MV.js, and the code for plotting, which is in the page source itself. For plotting, I use flot.

The code is rather readable I hope, and serves as a basis for the next two pages about constraints. The only ugly portion is the plotting part, but the physics code is pretty compact: there's a class Body and a class World. Bodies are instantiated and added to an instantiated world, then one steps through time by calling world.step() in each time step, followed by redrawing the bodies.

Please get in touch with me (hubert@THISDOMAIN) if anything is wrong, doesn't work or is unclear