Game Development Reference

In-Depth Information

two data points and has a nonzero, fixed value. Therefore, the equation can be rewritten

as:

(f(x+h) - f(x)) / h = ∆f(x)/h

where ∆
f
(
x
) is the position at the second timestamp minus the position at the first

timestamp, and
h
is the difference in time. This is relatively straightforward, as we are

just calculating the distance traveled divided by the time it took to travel that distance.

This is the definition of average velocity.

Note that because we are finding the average velocity between the two data points, if the

time delta,
h
, is too large, this will not accurately approximate the instantaneous velocity

of the object. You may be tempted to push whatever hardware you have to its limit and

get the highest possible sampling rate; however, if the time step is too small, the sub‐

traction of one displacement from another will result in significant round-off error

using floating-point arithmetic. You must take care to ensure that when you're selecting

a timestamp, (
t[i] + h
)
- t
[
i
] is exactly
h
. For more information on tuning these param‐

eters, refer to the classic
Numerical Recipes in C
. The function to find velocity from our

data structure would be as follows. Note that in our notation,
t
[
i
−
1
] is behind in time

compared to
t
[
i
], so we are using the backward form. Your program needs to ensure

that
t
[
i
−1] exists before executing this function:

Vector findVelocity (x[i-1], y[i-1], z[i-1], t[i-1], x[i], y[i], z[i], t[i]){

float vx, vy, vz;

vx = (x[i] − x[i-1])/(t[i]-t[i-1]);

vy = (y[i] − y[i-1])/(t[i]-t[i-1]);

vz = (z[i] − z[i-1])/(t[i]-t[i-1]);

vector velocity = {vx, vy, vz};

return velocity;

}

To compute the acceleration vector, we need to compare two velocities. However, we

note that to get a velocity, we need two data points. Therefore, a total of three data points

is required. The acceleration we solve for will actually be the acceleration for the middle

data point as we compare the backward and forward difference. This technique is named

the
second-order central difference
. In general, that form is as follows:

f''(x) = (f(x+2h) - 2f(x+h) + f(x)) / h
2

This allows you to compute the acceleration directly without first finding the velocities.

Here again,
f
(
x
) is the position reported by the sensor and
h
is the time step between

data points. The same discussion of
h
applies here as well. Some tuning of the time step