Game Development Reference
In-Depth Information
Force is given in the SI unit Newton . (Guess who came up with this.) If you specify acceleration
as a vector, then you also have to specify the force as a vector. A force can thus have a direction.
For example, the gravitational force pulls downward in the direction (0,-1). The acceleration is
also dependent on the mass of an object. The greater the mass of an object, the more force you
need to apply in order to make it accelerate as fast as an object of less weight. This is a direct
consequence of the preceding equations.
For simple games, we can, however, ignore the mass and force and just work with the velocity
and acceleration directly. The pseudocode in the preceding section sets the acceleration to
(0,-10) m/s2 (again, not a typo), which is roughly the acceleration of an object when it is falling
toward the earth, no matter its mass (ignoring things like air resistance). It's true…ask Galileo!
Playing Around, Theoretically
We'll use the preceding example to play with an object falling toward earth. Let's assume that
we let the loop iterate ten times, and that getDeltaTime() will always return 0.1 s. We'll get the
following positions and velocities for each iteration:
time=0.1, position=(0.0,-0.1), velocity=(0.0,−1.0)
time=0.2, position=(0.0,-0.3), velocity=(0.0,-2.0)
time=0.3, position=(0.0,-0.6), velocity=(0.0,-3.0)
time=0.4, position=(0.0,-1.0), velocity=(0.0,-4.0)
time=0.5, position=(0.0,-1.5), velocity=(0.0,-5.0)
time=0.6, position=(0.0,-2.1), velocity=(0.0,-6.0)
time=0.7, position=(0.0,-2.8), velocity=(0.0,-7.0)
time=0.8, position=(0.0,-3.6), velocity=(0.0,-8.0)
time=0.9, position=(0.0,-4.5), velocity=(0.0,-9.0)
time=1.0, position=(0.0,-5.5), velocity=(0.0,-10.0)
After 1 s, our object will fall 5.5 m and have a velocity of (0,-10) m/s, moving straight down to the
core of the earth (until it hits the ground, of course).
Our object will increase its downward speed without end, as we haven't factored in air resistance.
(As mentioned before, you can easily cheat your own system.) We can simply enforce a maximum
velocity by checking the current velocity length, which equals the speed of the object.
All-knowing Wikipedia indicates that a human in free fall can have a maximum, or terminal,
velocity of roughly 125 mph. Converting that to meters per second (125 × 1.6 × 1000 / 3600), we
get 55.5 m/s. To make our simulation more realistic, we can modify the loop as follows:
while (simulationRuns) {
float deltaTime = getDeltaTime();
if if(velocity.len() < 55.5)
velocity.add(acceleration.x * deltaTime, acceleration.y * deltaTime);
position.add(velocity.x * deltaTime, velocity.y * deltaTime);
}
As long as the speed of the object (the length of the velocity vector) is smaller than 55.5 m/s,
we can increase the velocity by the acceleration. When we've reached the terminal velocity, we
 
Search WWH ::




Custom Search