Game Development Reference
In-Depth Information
// 3. Get current theta
double Theta = Math.acos(ForwardXZPlane.DotProduct(TurretToTarget));
// 4. Get Theta if boat is turned to left by PI/16
Orientation NewO = new Orientation(GetParent().GetAIVehicle().GetTurret().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(TurretToTarget));
// Check if angle within tolerance for firing
float Diff = Math.abs((float)(Theta));
if (!m_FireWeapon)
{
if (Diff <= m_TargetAngleTolerance)
{
m_FireWeapon = true;
m_StartTimeFiring = System.currentTimeMillis();
}
}
// 5. Set Steering
if (Theta2 > Theta)
{
GetParent().GetTurretSteering().SetSteeringHorizontal(HorizontalSteeringValues.Right, 1);
}
else
if (Theta2 < Theta)
{
GetParent().GetTurretSteering().SetSteeringHorizontal(HorizontalSteeringValues.Left, 1);
}
else
{
GetParent().GetTurretSteering().SetSteeringHorizontal(HorizontalSteeringValues.None,0);
}
}
The TurnTurretTowardTarget() function does the following:
1.
Finds the vector representing the firing direction of the tank's weapon in
world coordinates, which is ForwardXZPlane (see Figure 8-6 ).
 
Search WWH ::




Custom Search