-1

I've beeen going through Reimer's flightsim tutorial and wanted to expand it by adding anti air turrets shooting at the ship. Im still new to programming in c# and xna and also a bit horrible in maths :P. How would I go about calculating how the turret would track the ship which is moving in 3 axes? Looking through the tutorial I assume I'll be using a quaternion and using lerp to try and smooth the tracking movement?

My model is just a simple hovering ball with a gun so theres no animation or bones to worry about.

skon
  • 109
  • 2

2 Answers2

0

Quaternion is the right keyword! Look at this question which deals with the same problem.

Basicly your objects have a position and an orientation. To point the turret at the ship you need to find the vector pointing from turret to ship. Then you can manipulate the turret to point at the ship and start you shoot logic / animation.

floAr
  • 1,005
  • 1
  • 7
  • 13
0

You could use a "LookAt" view matrix for the World transform of the turret.

See Matrix.CreateLookAt

This method requires you to figure out the Up vector though. The cross product of two perpendicular vectors will give you the third perpendicular vector in the direction of the handedness of the system.

///  position = my position, lookat = the position of the object I want to look at
public static Matrix LookAt(Vector3 position, Vector3 lookat)
{
    Matrix rotation = new Matrix();

    rotation.Forward = Vector3.Normalize(lookat - position);
    rotation.Right = Vector3.Normalize(Vector3.Cross(rotation.Forward, Vector3.Up));
    rotation.Up = Vector3.Normalize(Vector3.Cross(rotation.Right, rotation.Forward));

    return rotation;
}
indeed005
  • 408
  • 3
  • 12