Game Development Reference
In-Depth Information
//lookradians the same as carRadians for middle
lookAheadPoints[1].x=player.nextX+Math.cos(carRadians)*player.radius;
lookAheadPoints[1].y = player.nextY + Math.sin(carRadians) * player.radius;
lookRadians = ((player.nextRotation+45) / 360) * (2.0 * Math.PI);
lookAheadPoints[2].x = (player.nextX + Math.cos(lookRadians) * player.radius);
lookAheadPoints[2].y = (player.nextY + Math.sin(lookRadians) * player.radius);
}
}
This is the detailed description of the main points of the update function:
1.
The player.move Boolean is set to true . When we do collision detection, we will set this
to false if any one of the three LookAheadPoint references returns true for a WALL hit.
2.
Next, we set the turn speed for the player. This relates directly to the actual velocity
of the car. The maximum turn speed is .6 and minimum is .3, and we can set these
speeds in a simple linear relation to the velocity. We simply divide the current velocity
by 10 and add it to the player.minTurnSpeed . Then we check to make sure it isn't over
our player.maxTurnSpeed .
3.
Exactly as in the previous section in this chapter on moving the car about the world (see
“Adding basic car physics to our game”),we calculate the new dx and dy values using the
player.nextRotation value and the current velocity of the player. We update the
player.worldNextX and player.worldNextY with the dx and dy values. The camera.nextX
and camera.nexty values are based on this new world position for the player.
4.
Next, we set the nextX of the camera in a similar fashion to how we described
orienting it in the discussion in iteration 2. If the camera.nextX or camera.nextY is off
the top, bottom, right, or left of the screen, we orient the associated axis back to 0 and
move the player instead.
5.
The final thing we do is set the three look ahead points to the front, middle, left, and
right of the car if it is going forward (velocity greater than 0) and to the rear of the car if
the velocity is less than 0.
We find these mathematically by calculating the radians for a 45-degree angle from the center of the
car in each of the positive and negative directions and multiply this number by the radius we
calculated earlier (16). The middle LookAheadPoint simply uses the radians for the current rotation.
See Figure 10-10 for an example.
Figure 10-10. Calculated LookAheadPoint instances
Search WWH ::




Custom Search