Game Development Reference
In-Depth Information
We can include the damping in the equations to give a damped harmonic oscilla-
tor. The differential equation 6.3 becomes
¨
˙
p
=−
k p
d
p
where k is the spring constant (no need for χ in this case) and d is a drag coefficient
(it matches the k 1 coefficient from equation 5.1 in the previous chapter). This equa-
tion doesn't allow for drag that is proportional to the velocity squared—that is, the k 2
value from equation 5.1. If we added this, the mathematics would become consider-
ably more complex for little visible improvement (remember, we're faking this in any
case). So we stick with the simplest kind of drag.
Solving the differential equation gives an expression for the position at any time
in the future:
p t = p 0 cos (γ t)
c sin (γ t) e
1
2 dt
+
where γ is a constant given by
2 4 k
1
γ
=
d 2
and c is a constant given by
d
2 γ p 0 +
1
γ ˙
c
=
p 0
Substituting the time interval for t in these equations, as before, we can get a value
for p t , and calculate the acceleration required using equation 6.5, as we did for regular
harmonic motion.
Implementation
The code to implement a fake implicit spring force generator looks like this:
Excerpt from include/cyclone/precision.h
/** Defines the precision of the sine operator. */
#define real_sin sinf
/** Defines the precision of the cosine operator. */
#define real_cos cosf
/** Defines the precision of the exponent operator. */
#define real_exp expf
Excerpt from include/cyclone/pfgen.h
/**
* A force generator that fakes a stiff spring force, and where
* one end is attached to a fixed point in space.
Search Nedrilad ::




Custom Search