Game Development Reference
In-Depth Information
Next, we invoke the physics engine to do its thing. Although most of
this code is in the physics engine itself, it will communicate back with the
game code for several purposes, either just for notification, or perhaps to
provide customization opportunities to the application. We review a few
different approaches that are used by physics engines later in this section.
When the physics engine is complete, some miscellaneous steps need
to happen that we have grouped together under the function simPost
Physics() . Perhaps the most important step is to update our game objects
with the new positions and orientations that have been determined by the
physics engine. We might do this by looping through all the objects and
polling the physics engine for the updated position. Or we might receive
notification in the form of a callback. The updated object positions are
not the only output of the physics update. We might also be interested in
the forces that were required to maintain constraints, or the list of colli-
sions that occurred. Depending on the game design, and how the camera
is simulated, we often update the camera after the physics has completed,
so that it moves in response to the player movement.
Finally, of course, at some point we need to draw the scene, as indicated
by the presence of the function render() .
In our pseudocode, physicsEngine->update() represents the heart of
the physics engine. As we've mentioned, no two physics engines work ex-
actly the same, but there are some common themes. Here we briefly out-
line a few strategies, summarizing the more in-depth survey of Erleben et
al. [19].
Penalty Methods. Penalty methods resolve collisions with a spring-like
mechanism. The collision detection provides a list of penetrating collision
shapes. For each pair, we locate the dynamics bodies that own these shapes
and apply a repulsive force to each, where the magnitude of this force is pro-
portionate to the penetration depth. In other words, the penalty method
does not attempt to resolve collisions on the same time step that they are
detected; rather, over time the force will cause the objects to separate.
Of course, we must tune our “springs” carefully; for stacked objects, the
spring force will balance with gravity when the objects are in equilibrium,
so in general the penalty method does not attempt to completely eliminate
penetration, but rather just to limit it to an acceptable level. Listing 12.4
shows a simplified version of how this could be done.
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 ( ) ;
/ /
L o c a t e
p e n e t r a t i n g
c o l l i s i o n
geometry ,
and
t h e i r
owner
b o d i e s
Search WWH ::




Custom Search