Game Development Reference
In-Depth Information
It follows that:
v = d s /dt = dx/dt i + dy/dt j
a = d v /dt = d 2 s /dt 2 = d 2 x/dt 2 i + d 2 y/dt 2 j
Consider a simple example where you're writing a shooting game and you need to figure
out the vertical drop in a fired bullet from its aim point to the point at which it actually
hits the target. In this example, assume that there is no wind and no drag on the bullet
as it flies through the air (we'll deal with wind and drag on projectiles in Chapter 6 ).
These assumptions reduce the problem to one of constant acceleration, which in this
case is that due to gravity. It is this gravitational acceleration that is responsible for the
drop in the bullet as it travels from the rifle to the target. Figure 2-2 illustrates the
problem.
Figure 2-2. A 2D kinematics example problem
While we're talking about guns and shooting here, we should point out
that these techniques can be applied just as easily to simulating the flight
of angry birds in Angry Birds being shot from oversized slingshots, as
in the very popular iPhone app. Heck, you can use these techniques to
simulate flying monkeys, ballistic shoes, or coconuts being hurled at
Navy combatants! This particle kinematic stuff is perfect for diversionā
ary smartphone apps.
Let the origin of the 2D coordinate system be at the end of the rifle with the x-axis
pointing toward the target and the y-axis pointing up. Positive displacements along the
x-axis are toward the target, and positive displacements along the y-axis are upward.
This implies that the gravitational acceleration will act in the negative y-direction.
Treating the x and y components separately allows you to break up the problem into
small, easy-to-manage pieces. Looking at the x component first, you know that the bullet
will leave the rifle with an initial muzzle velocity v m in the x-direction, and since we are
neglecting drag, this speed will be constant. Thus: