Game Development Reference
In-Depth Information
s t r u c t
C o l l i s i o n
{
DynamicsBody
body1 ,
body2 ;
/ /
t h e
b o d i e s
i n v o l v e d
V e c t o r 3
p ;
/ /
l o c a t i o n
o f
c o l l i s i o n
V e c t o r 3
n ;
/ /
c o n t a c t
normal
f l o a t
p e n e t r a t i o n D e p t h ;
} ;
v e c t o r < C o l l i s i o n > c o l l i s i o n s
=
c o l l i s i o n D e t e c t i o n ( ) ;
/ /
T r e a t
each
c o l l i s i o n
a s
a
s p r i n g
f o r
( each
c o l l i s i o n )
{
/ /
C a l c u l a t e
a
f o r c e
based
on
t h e
p e n e t r a t i o n
depth
V e c t o r 3
f
=
c a l c u l a t e F o r c e ( c o l l i s i o n s [ i ] ) ;
/ / Add t h i s f o r c e t o t h e two b o d i e s
c o l l i s i o n s [ i ] . body1 −> a d d F o r c e A t P o i n t ( c o l l i s i o n s [ i ] . p ,
f ) ;
c o l l i s i o n s [ i ] . body2 −> a d d F o r c e A t P o i n t ( c o l l i s i o n s [ i ] . p ,
f ) ;
}
/ /
I n t e g r a t e
t h e
f o r c e s
( a c c e l e r a t i o n s )
i n t o
v e l o c i t y ,
and
/ /
v e l o c i t i e s
i n t o
p o s i t i o n ,
t o
move
t h e
s i m u l a t i o n
f o r w a r d
i n t e g r a t e F o r c e s ( ) ;
}
Listing 12.4
Pseudocode for a physics simulation based on the penalty method
Sequential impulse simulations. These methods were popularized by Mir-
tich [46]. Both resting and colliding contact are modeled as (possibly very
high frequency) collisions. When a collision is detected between two objects
A and B, a collision law (such as the simple Newton collision model shown
in Section 12.5.4) is used to calculate an impulse that will prevent pene-
tration. However, this might result in a different collision, either elsewhere
between A and B, or perhaps between B and C, so the process must be
repeated until all of the relative velocities at the contacts are resting or
separating. Listing 12.5 illustrates the basic idea.
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
/ /
( i . e .
g r a v i t y ,
s p r i n g s ,
e t c )
c o m p u t e F o r c e s ( ) ;
/ /
I n t e g r a t e
f o r c e
( a c c e l e r a t i o n )
i n t o
v e l o c i t i e s ,
b u t
don ' t
y e t
/ / update p o s i t i o n s
u p d a t e V e l o c i t i e s ( ) ;
/ /
L o c a t e
c o l l i d i n g
b o d i e s
s t r u c t
C o l l i s i o n
{
DynamicsBody
body1 ,
body2 ;
/ /
t h e
b o d i e s
i n v o l v e d
V e c t o r 3
p ;
/ /
l o c a t i o n
o f
c o l l i s i o n
V e c t o r 3
n ;
/ /
c o n t a c t
normal
f l o a t
p e n e t r a t i o n D e p t h ;
} ;
v e c t o r < C o l l i s i o n > c o l l i s i o n s
=
c o l l i s i o n D e t e c t i o n ( ) ;
/ /
Keep
a p p l y i n g
i m p u l s e s
u n t i l
a l l
Search WWH ::




Custom Search