Game Development Reference
In-Depth Information
Updating the simulation loop
A physics system typically operates by looping through every object in the game,
updating the physical properties of each object (position, velocity, and so on),
checking for collisions, and reacting to any collisions that are detected.
The simulation loop, which calls the physics engine for each object of interest, is
kept as separate as possible from the rendering loop, which draws the graphical
representation of the objects. Indeed the main loop of our game consists of just two
lines of code: one calls the render update and the other line calls the simulation
update. The simulation should be able to run without rendering anything to screen if
the rendering and simulation aspects of the code are correctly decoupled. The part of
the code which accesses the physics engine will typically run at a higher frame rate
than the rest of the game, especially the renderer.
Let's now first use our JigLibJS to initialize our physics system and also plan when
we want to update our physics objects. The following code snippet contains the
jigLib.PhysicsSystem.getInstance() method:
var system = jigLib.PhysicsSystem.getInstance();
system.setCollisionSystem(); // CollisionSystemBrute
// system.setCollisionSystem(true); // CollisionSystemGrid
system.setSolverType("FAST");
// system.setSolverType("NORMAL");
//system.setSolverType("ACCUMULATED");
We first initialize our physics system ( jigLib.PhysicsSystem.getInstance() ).
Then, we set the type of collision system (brute/grid).
Brute force would store two lists of collision objects in a collision system. One list
would be for dynamic objects and the other would include both dynamic and static
objects (each dynamic object would be in both lists). For each frame, it would loop
through the dynamic list and pass each object to the larger list and check for collisions.
Grid collision is a system that uses spatial hashing, in which we basically establish
a grid. In a grid, we mark down what is in touch with each grid. Then, we later go
through the relevant cells of the grid and check whether everything in each relevant
cell is actually intersecting with anything else in the cell. The grid collision is related
to the concept of space partition.
Physics engines are split into two major parts: collision detection and collision
resolution. The solver is just responsible for the latter.
 
Search WWH ::




Custom Search