Game Engine Architecture

(Ben Green) #1
675

and PhysX, make use of collision islands—groups of rigid bodies that can be
simulated independently of one another. This design lends itself well to a fork
and join architecture, as the islands can be dynamically distributed among the
available threads.


Jobs


A job model can be particularly useful for physics processing if the physics
SDK allows the individual phases of its update step (integration, collision de-
tection, constraint solving, CCD, etc.) to be run independently. This allows
us to kick off each phase whenever it is most convenient and perform useful
unrelated work in the main thread while we wait for the physics engine to do
its thing.
Jobs are even more useful when doing collision queries (ray and shape
casts). This is because while a game engine typically only needs to step the
physics simulation once per frame, collision queries may be required at many
diff erent points during the game loop. If lightweight jobs are used to run
queries, we can simply kick off jobs whenever we need them. On the other
hand, if collision queries can only be run at certain times during the frame
(because they are being executed by a fork or a dedicated thread), this makes
the job of the game programmer more diffi cult. He or she needs to collect all
the collision queries in a queue and then execute them as a batch the next time
queries are run during the frame. These two approaches are compared in Fig-
ure 12.36.


PPU

SPU Job Job Job

Main
Thread
Collision
Thread Process Batch Process Batch P...

Job Job Job J J..

Game Loop

Game Loop

Figure 12.36. Collision queries are often batched, to be run at a few well-chosen points dur-
ing the game loop. However, with a job model, queries can be kicked off at any time, without
the need to batch them.


12.5.3. Example Uses of Collision and Physics in a Game


To make our discussion of collision and physics more concrete, let’s take a
high-level look at a few common examples of how collision and/or physics
simulations are commonly used in real games.


12.5. Integrating a Physics Engine into Your Game

Free download pdf