Game Development Reference
In-Depth Information
// P.Q = P*Q*cos(theta)
// P.Q/P*Q = cos(theta)
// acos(P.Q/P*Q) = theta;
// 3. Get current theta
double Theta = Math.acos(ForwardXZPlane.DotProduct(VehicleToWayPoint));
// 4. Get Theta if boat is turned to left by PI/16
Orientation NewO = new Orientation(GetParent().GetAIVehicle().GetMainBody().m_Orientation);
Vector3 Up = NewO.GetUp();
NewO.SetRotationAxis(Up);
NewO.AddRotation(Physics.PI/16);
Vector3 NewForwardXZ = NewO.GetForwardWorldCoords();
NewForwardXZ.y = 0;
NewForwardXZ.Normalize();
double Theta2 = Math.acos(NewForwardXZ.DotProduct(VehicleToWayPoint));
// 5. Set Steering
if (Theta2 > Theta)
{
GetParent().GetAISteering().SetSteeringHorizontal(HorizontalSteeringValues.Right, 1);
}
else
if (Theta2 < Theta)
{
GetParent().GetAISteering().SetSteeringHorizontal(HorizontalSteeringValues.Left, 1);
}
else
{
GetParent().GetAISteering().SetSteeringHorizontal(HorizontalSteeringValues.None,0);
}
}
The function does the following:
1.
Finds the main tank body's forward vector, which is ForwardXZPlane .
2.
Calculates the vector, which is VehicleToWayPoint , from the tank to the
waypoint.
3.
Normalizes the ForwardXZPlane and VehicleToWayPoint vectors.
4.
Calculates the angle Theta between the ForwardXZPlane and
VehicleToWayPoint vectors.
5.
Creates a new Orientation class object that is the same as the orientation of
the tank's main body but turned by a small angle PI/16, which is NewO.
6.
Finds the forward vector of this new tank main body orientation, which is
NewForwardXZ , and then normalizes it.
Search WWH ::




Custom Search