Newton and Euler Best Friends Forever

We are mostly concerned with motion physics of so-called point masses, which refers to the change in position, velocity, and acceleration of an object over time. Point mass means that we approximate all our objects with an infinitesimally small point that has an associated mass. We do not deal with things like torque—the rotational velocity of an object around its center of mass—because that is a rather complex problem domain about which more than one complete book has been written. Let's just look at these three properties of an object:

■ The position of an object is simply a vector in some space—in our case a 2D space. We represent it as a vector. Usually the position is given in meters.

■ The velocity of an object is its change in position per second. Velocity is given as a 2D velocity vector, which is a combination of the unit-length direction vector the object is heading in and the speed that the object will move at, given in meters per seconds. Note that the speed just governs the length of the velocity vector; if we normalize the velocity vector by the speed, we get a nice unit-length direction vector.

■ The acceleration of an object is its change in velocity per second. We can either represent this as a scalar that only affects the speed of the velocity (the length of the velocity vector), or as a 2D vector, so that we can have different acceleration in the x- and y-axes. Here we'll choose the latter, as it allows us to use things such as ballistics more easily. Acceleration is usually given in meters per second per second (m/s2). No, that's not a typo—we change the velocity by some amount given in meters per second, each second.

When we know these properties of an object for a given point in time, we can integrate them to simulate the object's path through the world over time. This may sound scary, but we already did this with Mr. Nom and our Bob test. In those cases we just didn't use acceleration; we set the velocity to a fixed vector. Here's how we can integrate the acceleration, velocity and position of an object in general:

Vector2 position = new Vector2(); Vector2 velocity = new Vector2(); Vector2 acceleration = new Vector2(0, -10); while(simulationRuns) {

float deltaTime = getDeltaTime();

velocity.add(acceleration.x * deltaTime, acceleration.y * deltaTime); position.add(velocity.x * deltaTime, velocity.y * deltaTime);

This is called numerical Euler integration, and is the most intuitive of the integration methods used in games. We start off with a position at (0,0), a velocity given as (0,0), and an acceleration of (0,-10), which means that the velocity will increase by 1 meter per second on the y-axis. There will be no movement on the x-axis. Before we enter the integration loop, our object is standing still. Within the loop we first update the velocity based on the acceleration multiplied by the delta time, and then update the position based on the velocity times the delta time. That's all there is to the big, scary word integration.

NOTE: As usual, that's not even half of the story. Euler integration is an "unstable" integration method and should be avoided when possible. Usually one would employ a variant of the so-called verletintegration, which is just a bit more complex. For our purposes, the easier Euler integration is sufficient though.

0 0

Post a comment