Game Development Reference

In-Depth Information

Velocity Verlet integration.
A variant of Verlet integration that is sometimes

used is the velocity Verlet algorithm (also called Leapfrog integration):

x
(
t
+Δ
t
)=
x
(
t
)+
v
(
t
)Δ
t
+
a
(
t
)(Δ
t
)
2
/
2
,

v
(
t
+Δ
t/
2) =
v
(
t
)+
a
(
t
)Δ
t/
2
,

a
(
t
+Δ
t
)=
f
(
x
(
t
+Δ
t
)
,
v
(
t
+Δ
t/
2))
,

v
(
t
+Δ
t
)=
v
(
t
+Δ
t/
2) +
a
(
t
+Δ
t
)Δ
t/
2
,

where
f
is a function of position and velocity that yields the acceleration given

by the current context.

The physics engine in
Hitman
relies on basic Verlet integration only, but in

situations that call for higher accuracy or additional robustness, velocity Verlet

integration may sometimes be more suitable.

Using Verlet integration in a physics simulation.
It is easy to implement the

results of the above section in a function that updates a set of unconstrained

particles.

// Use Verlet integration to advance an array of particles

// t: Size of time step

// x: Array of current positions of particles

// x prev: Array of previous positions of particles

// a: Current acceleration of each particle

// n: Total number of particle coordinates

//

void
VerletTimeStep(
double
t,
double

∗

x,
double

∗

x prev,

double
∗
a,
int
n)

{

double
x old;

for
(
int
i=0; i
<
n; i++)
{

x old =
∗
x;

∗
x = 1.99
∗∗
x
−
0.99
∗∗
x prev +
∗
a
∗
t
∗
t;

∗

x prev = x old;

x++;

x prev++;

a++;

}

}

The above code has been written for clarity, not speed. Note that it is possible to

save memory transfers with a double-buffering approach by alternating between

two arrays. Note also that the Verlet formula has been changed slightly to include

the two factors 1
.
99 and 0
.
99 in order to introduce a small amount of drag in the

system for further stabilization.