Game Development Reference
In-Depth Information
A better solution is to separate physics update time-steps from frame refresh
time-steps. The physics engine should receive fixed-size time deltas, while the
rendering engine should determine how many physics updates should occur per
frame. The fixed-size deltas avoid an inconsistent rounding error and ensure that
there are no giant leaps between frames. The following code shows how to divide the
amount of time between frames into discrete chunks to use for physics calculations:
// Globals
INV_MAX_FPS = 1 / 60;
frameDelta = 0;
clock = new THREE.Clock();
// In the animation loop (the requestAnimationFrame callback)…
frameDelta += clock.getDelta();
while (frameDelta >= INV_MAX_FPS) {
update(INV_MAX_FPS); // calculate physics
frameDelta -= INV_MAX_FPS;
}
First, we declare INV_MAX_FPS , the multiplicative inverse of the maximum frames
per second that we want to render ( 60 in this case). This is the time-step we will
feed to our physics engine via the update function, and you may need to adjust it
depending on how slowly your simulation runs (keep in mind that most monitors
can't refresh faster than 60 frames per second, and above 30 is usually considered
acceptable). Then, we start tracking our frameDelta , the accumulated amount of
time since the last physics update. Our clock will be used to keep track of the time
between rendering frames.
In the animation loop, we first add the amount of time since the last render to
frameDelta , then perform as many fixed-size physics updates as we need. We
might end up with some time left over in frameDelta , but it will be used up
during the next frame.
For our purposes, "physics updates" means both moving objects in our world and
moving the player's camera.
First-person shooter project
Let's write a real game! This project will be bigger than any others we've done,
so let's start by specifying exactly what to accomplish. We're going to build an
arena-based first-person shooter game with the following features:
• A world based on a voxel map
• A player that can look, run, and jump around in the world
 
Search WWH ::




Custom Search