Game Development Reference
In-Depth Information
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—you change
the velocity by some amount given in meters per second, each second.
When we know the 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 BobTest class. In those cases, we didn't use acceleration; we simply
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 it 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 m/s 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 multiplied by 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
verlet integration , which is just a bit more complex. For our purposes, however, the easier Euler
integration is sufficient.
Force and Mass
You might wonder where the acceleration comes from. That's a good question, with many
answers. The acceleration of a car comes from its engine. The engine applies a force to the car
that causes it to accelerate. But that's not all. A car will also accelerate toward the center of the
earth, due to gravity. The only thing that keeps it from falling through to the center of the earth
is the ground, which it can't pass through. The ground cancels out this gravitational force. The
general idea is this:
force mass
=
×
acceleration
You can rearrange this to the following equation:
acceleration
=
force / mass
 
 
Search WWH ::




Custom Search