Game Development Reference
In-Depth Information
Velocity is the change in position over time, and likewise acceleration is the change
in velocity over time. You calculate them like this:
Vec3 CalcVel(const Vec3 &pos0, const Vec3 &pos1, const float time)
{
return (pos1 - pos0) / time;
}
Vec3 CalcAccel(const Vec3 &vel0, const Vec3 &vel1, const float time)
{
return (vel1 - vel0) / time;
}
This is fairly pedantic stuff, and you should remember this from the math you
learned in high school. In computer games, you frequently need to go backward.
You ' ll have the acceleration as a vector, but you ' ll want to know what happens to
the position of an object during your main loop. Here
'
s how to do that:
inline Vec3 HandleAccel(Vec3 &pos, Vec3 &vel, const Vec3 &accel, float time)
{
vel += accel * time;
pos += vel * time;
return pos;
}
Notice that when the acceleration is handled, both the velocity and the position change.
Both are sent into HandleAccel() as references that will hold the new values.
Now that you
ve seen the code, take a quick look Table 17.1, which contains mathemat-
ical formulas for calculating positions and velocities. Hopefully, you won
'
tpassout.
You probably recognize these formulas. When you first learned these formulas, you
were using scalar numbers representing simple one-dimensional measurements like
'
Table 17.1 Formulas for Calculating Positions and Velocities
Formula
Description
p=p 0 + vt
Find a new position (p) from your current position (p 0 ),
velocity (v), and time (t)
v=v 0 + at
Find a new velocity (v) from your current velocity (v 0 ),
acceleration (a), and time (t)
p=p 0 +v 0 t + (at 2 )/2
Find a new position (p) from your current position (p 0 ),
velocity (v 0 ), acceleration (a), and time (t)
Search WWH ::




Custom Search