Game Development Reference
In-Depth Information
frame). When the maximum velocity is reached, the car will not be able to accelerate any further.
When the up arrow key is not pressed, a deceleration value will be subtracted from the velocity
until it reaches 0. By pressing the down arrow key, the player will slowly begin to stop and then
start to move in the reverse direction. There is a separate reverse maximum velocity that is
roughly half the forward maximum velocity. Our car cannot turn or rotate unless it has some
forward or reverse velocity.
Moving in a direction
Our velocity value is really just a scalar value that represents the current speed of the car. In
reality, velocity is actually a vector value that includes values for moving along the x and y axis of
a graph.
The direction the player car is facing and the amount to move in that direction will be considered
the vector value for our movement. A vector is a number that includes a magnitude (amount) and
a direction. We will have a variable in our game called velocity, but it really is just half of the value
we need to move our car. Our vectors for movement will be stored as a number and a sign for
both the x and y directions. The number will represent the magnitude, and the sign will represent
the direction. So, for the horizontal direction we might have a value that is -1. This would
represent 1 pixel in the left direction. We might have a vertical value of 3 (no sign means
positive). This would represent a value of 3 pixels up the screen.
When the car is turned or rotated to a new position by pressing the arrow keys, we will need to
calculate the vectors for moving in that new direction. To find the vector value to move in this new
direction, we need to know the angle our car is currently moving.
We will store the vector for moving our car in a rotated direction in two variables. One will be used
for the x direction and one will be used for the y direction. These two values are sometimes
referred to as deltaX (dx) and deltaY (dy). These represent the change (or delta) in direction and
location on each frame for our car. This value is derived using some basic trigonometry to
determine the angle our car should move based on its current rotation. The speed of our car (the
velocity attribute of our car) will be multiplied by the dx and dy values to achieve the actual
movement in both the x and y directions.
Let's do an example where our car has been rotated to a 30-degree angle, and now we need to
calculate the dx and dy values to move it in that direction on the next frame.
Let's take a look at some example code and the output to illustrate this point:
var velocity:Number = 2;
var rotation:Number = 30;
var carRadians:Number = (rotation / 360) * (2.0 * Math.PI);
trace("rotation=" + rotation);
trace("carRadians=" +carRadians);
trace("Math.cos(carRadians)=" + Math.cos(carRadians));
trace("Math.sin(carRadians)=" + Math.sin(carRadians));
var dx:Number=Math.cos(carRadians)*velocity;
var dy:Number= Math.sin(carRadians) * velocity;
trace("dx=" + dx);
trace("dy=" + dy);
Search WWH ::




Custom Search