Game Development Reference

In-Depth Information

In two dimensions, the coordinate transformation involves a little trigonometry as

shown in the following
VRotate2D
function:

Vector VRotate2D( float angle, Vector u)

{

float x,y;

x = u.x * cos(DegreesToRadians(-angle)) +

u.y * sin(DegreesToRadians(-angle));

y = -u.x * sin(DegreesToRadians(-angle)) +

u.y * cos(DegreesToRadians(-angle));

return Vector( x, y, 0);

}

The angle here represents the orientation of the local, body fixed coordinate system with

respect to the world coordinate system. When converting from local coordinates to

world coordinates, use a positive angle; use a negative angle when going the other way.

This is just the convention we've adopted so transformations from local coordinates to

world coordinates are positive. You can see we actually take the negative of the
angle

parameter, so in reality you could do away with that negative, and then transformations

from local coordinates to world coordinates would actually be negative. It's your pref‐

erence. You'll see this function used a few more times in different situations before the

end of this chapter.

Integrator

The
UpdateBodyEuler
method actually integrates the equations of motion for the rigid

body. Since we're dealing with a rigid body, unlike a particle, we have two equations of

motion: one for translation, and the other for rotation. The following code sample shows

UpdateBodyEuler
.

void RigidBody2D::UpdateBodyEuler(double dt)

{

Vector a;

Vector dv;

Vector ds;

float aa;

float dav;

float dr;

// Calculate forces and moments:

CalcLoads();

// Integrate linear equation of motion:

a = vForces / fMass;

dv = a * dt;

vVelocity += dv;