Game Development Reference
In-Depth Information
The fourth-order Runge-Kutta method (also known as RK4 ) is another commonly
used method for computing motion over time. RK4 extrapolates several intermediate
states between frames, resulting in a more accurate final approximation for the state
in the next frame. The trade-off for increased accuracy is increased complexity and
decreased speed. Because of its complexity, we won't cover it here, but usually if you
need something as sophisticated as this, you will want to delegate physics handling
to one of the libraries addressed in the next section on collision.
Updating the player's movement and rotation
Let's listen for the movement keys so that we know when to move the player:
document.addEventListener('keydown', function(event) {
switch (event.keyCode) {
case 38: // up
case 87: // w
player.moveDirection.FORWARD = true;
break;
case 37: // left
case 65: // a
player.moveDirection.LEFT = true;
break;
case 40: // down
case 83: // s
player.moveDirection.BACKWARD = true;
break;
case 39: // right
case 68: // d
player.moveDirection.RIGHT = true;
break;
case 32: // space
player.jump();
break;
}
}, false);
We'll check these flags in every frame to see how much thrust to apply. We also need
a keyup listener, which looks almost identical to the keydown listener except that it
should set our directions back to false when the relevant keys are released.
 
Search WWH ::




Custom Search