Game Development Reference
In-Depth Information
In practice, the simulation code is a very simple Euler integration of
Equation (12.11). As shown in Section 12.6.3, this is a fancy way of de-
scribing code that looks like Listing 12.1.
s t r u c t
S p r i n t D a m p e r
{
f l o a t
v a l u e ;
/ /
c u r r e n t
v a l u e
f l o a t
s e t P o i n t ;
/ /
” d e s i r e d ”
v a l u e
f l o a t
v e l o c i t y ;
/ /
c u r r e n t
' ' v e l o c i t y ' '
( d e r i v a t i v e
o f
v a l u e )
f l o a t
c ;
/ /
damping
c o e f f i c i e n t
f l o a t
k ;
/ /
s p r i n g
c o n s t a n t
/ /
Update
t h e
c u r r e n t
v a l u e
and
v e l o c i t y ,
s t e p p i n g
f o r w a r d
/ /
i n
t i m e
by
t h e
g i v e n
t i m e
s t e p
{
v o i d
u p d a t e ( f l o a t
d t )
/ /
Compute
a c c e l e r a t i o n
f l o a t
e r r o r
=
v a l u e
s e t P o i n t ;
f l o a t
a c c e l
= e r r o r k
c v e l o c i t y ;
/ /
E u l e r
i n t e g r a t i o n
v e l o c i t y
+=
a c c e l d t ;
v a l u e
+=
v e l o c i t y d t ;
}
} ;
Listing 12.1
A simple spring-damper control system
Different cars have suspensions that are tuned differently; sports cars are
“tighter” and the cars retirees like to drive are smoother. In the same way,
we tune our control systems to get the response we like. Notice that the
simulation uses the k and c from Equation (12.11). However, most people
don't find those to be the most intuitive dials to have for tweaking. Instead,
the damping ratio and frequency of oscillation are used for the designer
interface, while k and c are computed as derived quantities. To tune the
frequency, we might adjust either the damped or undamped version, using
either angular frequency or simply Hertz; the units and absolute value are
often not important because the value that feels good will be determined
experimentally anyway. For many systems in video games, oscillation is
undesirable, so it's common to assume a critically damped system and fix
ζ = 1, leaving just the “frequency” (we put it in quotes since the system
doesn't oscillate) as the only tunable value. A higher frequency is the sports
car (more responsive, but jerkier), and a lower frequency is smoother, but
can feel “laggy.”
Notice that the kinematic equations (12.13) and (12.15) are not needed
directly by the simulation, nor do we need to explicitly distinguish between
underdamped, critically damped, or overdamped.
Before we leave this discussion, we must mention that the second-order
systems we have described here are certainly not the only type of control
system, nor even the simplest, but they do behave nicely under a very
Search WWH ::




Custom Search