Game Development Reference
In-Depth Information
/ /
r e l a t i v e
c o n t a c t
v e l o c i t y
i s
r e s o l v e d
f o r
( ; ; )
{
/ /
F i n d
a
c o l l i s i o n
where
t h e
r e l a t i v e
p o i n t
v e l o c i t i e s
a t
/ /
t h e
p o i n t
o f
c o n t a c t
a r e
such
t h a t
t h e
b o d i e s
a r e
moving
/ /
t o w a r d s
each
o t h e r
a l o n g
t h e
normal .
( C o l l i d i n g
c o n t a c t ,
/ /
r a t h e r
t h a n
r e s t i n g
o r
s e p a r a t i n g
c o n t a c t )
C o l l i s i o n
c
=
n e x t U n r e s o l v e d C o l l i s i o n ( c o l l i s i o n s ) ;
i f
( c
==
NULL )
b re ak ;
/ /
C a l c u l a t e
an
i m p u l s e
u s i n g
a
c o l l i s i o n
law
V e c t o r 3
i m p u l s e
=
c a l c u l a t e C o l l i s i o n I m p u l s e ( c ) ;
/ /
Apply
t h e
i m p u l s e
t o
t h e
two
b o d i e s .
( T h i s
produces
an
/ /
immediate
change
i n
t h e
l i n e a r
and
a n g u l a r
v e l o c i t i e s )
c −> body1 −> a d d I m p u l s e A t P o i n t ( c −> p ,
i m p u l s e ) ;
c −> body2 −> a d d I m p u l s e A t P o i n t ( c −> p ,
i m p u l s e ) ;
/ /
Keep
l o o p i n g
u n t i l
a l l
c o l l i s i o n
v e l o c i t i e s
a r e
r e s t i n g
/ /
o r
s e p a r a t i n g
}
/ / Now s t e p
t h e
p o s i t i o n s
f o r w a r d
based
on
t h e
v e l o c i t i e s
i n t e g r a t e V e l o c i t i e s ( ) ;
}
Listing 12.5
Pseudocode for a sequential impulse physics simulation
The first di culty is that the order that the contacts are processed
(which are often an arbitrary artifact of collision detection) can cause dif-
ferent simulation results. A second is that the simulation can get caught in
an infinite loop, so care must be taken to ensure termination.
Velocity-based simulations. These techniques currently represent the state
of the art in real-time simulations. Preventing penetration and resolving
collisions is viewed as a constraint to be satisfied. As mentioned earlier,
these constraints are treated in a standardized way with user constraints
such as joints and motors. For each constraint, the simulation examines the
ratio of the rate of change of satisfaction of the constraint versus changes
in linear and angular velocity. Using this information (a matrix of partial
derivatives is known as a Jacobian matrix), the simulator solves for the
velocities that satisfy the constraints. This is illustrated in Listing 12.6.
v o i d
P h y s i c s E n g i n e : : u p d a t e ( )
{
/ /
Gather
up
e x t e r n a l
f o r c e s
a c t i n g
on
t h e
dynamics
b o d i e s
/ /
( e x c l u d i n g
t h o s e
from
t h e
c o n s t r a i n t s )
c o m p u t e F o r c e s ( ) ;
/ /
T e n t a t i v e l y
a p p l y
f o r c e s ,
t o
compute
proposed
/ /
( u n c o n s t r a i n e d )
p o s i t i o n s
and
v e l o c i t i e s
i n t e g r a t e F o r c e s T e n t a t i v e l y ( ) ;
/ /
B u i l d
up
l i s t
o f
c o n s t r a i n t s .
These
come
from
two
/ /
s o u r c e s :
c o l l i s i o n
d e t e c t i o n
( c o n t a c t
c o n s t r a i n t s )
and
Search WWH ::




Custom Search