Game Development Reference
In-Depth Information
Finds the angle Theta2 between the
NewForwardXZ
and the
VehicleToWayPoint
vectors.
7.
8.
If Theta2 is greater than Theta, turn the tank's main body to the right.
9. If Theta2 is less than Theta, turn the tank's main body to the left.
The
SteerVehicleWaypointSpeed()
function decelerates the tank around the waypoints. If the tank
is within TurnArea radius of the last waypoint or the current waypoint, this function decelerates the
tank; otherwise, the function accelerates the tank. (See Listing 8-35.)
Listing 8-35. Changing the Speed of the Vehicle Around Waypoints
void SteerVehicleWaypointSpeed(Vector3 WayPoint)
{
// If vehicle is close to waypoint then slow down vehicle
// else accelerate vehicle
Tank AIVehicle = GetParent().GetAIVehicle();
Vector3 VehiclePos = AIVehicle.GetMainBody().m_Orientation.GetPosition();
Vector3 DistanceVecLastWayPoint = Vector3.Subtract(VehiclePos,m_LastWayPoint);
Vector3 DistanceVecCurrentWayPoint = Vector3.Subtract(VehiclePos, m_WayPoint);
float TurnArea = GetParent().GetTurnArea();
float DLastWayPoint = DistanceVecLastWayPoint.Length();
float DCurrentWayPoint = DistanceVecCurrentWayPoint.Length();
if ((DLastWayPoint <= TurnArea) || (DCurrentWayPoint <= TurnArea))
{
// Decrease speed
GetParent().GetAISteering().SetSteeringSpeed(SpeedSteeringValues.Deccelerate, 0.04f,
0.03f, 0.005f);
GetParent().GetAISteering().SetTurnDelta(3.0f);
}
else
{
GetParent().GetAISteering().SetSteeringSpeed(SpeedSteeringValues.Accelerate, 0.04f,
0.03f, 0.005f);
}
}
The
SteerVehicleToWayPoint()
function steers the vehicle left or right toward the waypoint
location by calling
SteerVehicleToWayPointHorizontal()
and adjusts the tank's speed by calling
SteerVehicleWaypointSpeed()
. (See Listing 8-36.)
Listing 8-36. Steering the Vehicle to the Waypoint
void SteerVehicleToWayPoint(Vector3 WayPoint)
{
SteerVehicleToWayPointHorizontal(WayPoint);
SteerVehicleWaypointSpeed(WayPoint);
}