I've read all kinds of various methods to determine the angle between two vectors and I'm really confused so I need some help to understand what I need to do.
Below is my First-person camera code
public class FPSCamera : Engine3DObject
{
public Matrix View { get; private set; }
public Matrix Projeciton { get; private set; }
private Quaternion rotation;
private float yaw;
private float pitch;
private bool isViewDirty;
private bool isRotationDirty;
public BoundingFrustum Frustum { get; private set; }
public bool Changed { get; private set; }
public Vector3 ForwardVector
{
get
{
return this.View.Forward;
}
}
public FPSCamera()
: base()
{
this.Position = Vector3.Zero;
this.rotation = Quaternion.Identity;
this.yaw = this.pitch = 0;
this.isViewDirty = true;
this.isRotationDirty = false;
}
public void SetPosition(Vector3 position)
{
this.Position = position;
this.isViewDirty = true;
this.Update(null);
}
public void Initialize(float aspectRatio)
{
this.Projeciton = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio, 1f, 1000f);
}
public void Update(GameTime gameTime)
{
this.Changed = false;
if (isRotationDirty)
{
if (yaw > MathHelper.TwoPi)
{
yaw = yaw - MathHelper.TwoPi;
}
if (yaw < -MathHelper.TwoPi)
{
yaw = yaw + MathHelper.TwoPi;
}
if (pitch > MathHelper.TwoPi)
{
pitch = pitch - MathHelper.TwoPi;
}
if (pitch < -MathHelper.TwoPi)
{
pitch = pitch + MathHelper.TwoPi;
}
this.rotation = Quaternion.CreateFromYawPitchRoll(yaw, pitch, 0);
this.isRotationDirty = false;
this.isViewDirty = true;
this.Changed = true;
}
if (isViewDirty)
{
Vector3 up = Vector3.Transform(Vector3.Up, rotation);
Vector3 target = Vector3.Transform(Vector3.Forward, rotation) + Position;
this.View = Matrix.CreateLookAt(this.Position, target, up);
this.isViewDirty = false;
if (this.Frustum == null)
{
this.Frustum = new BoundingFrustum(this.View * this.Projeciton);
}
else
{
this.Frustum.Matrix = (this.View * this.Projeciton);
}
this.Changed = true;
}
}
public void Move(Vector3 distance)
{
this.Position += Vector3.Transform(distance, rotation);
this.isViewDirty = true;
}
public void Rotate(float yaw, float pitch)
{
this.yaw += yaw;
this.pitch += pitch;
this.isRotationDirty = true;
}
public void LookAt(Vector3 lookAt)
{
}
}
The "lookat" method is blank because I'm trying to figure out how to do it. I move the camera so it's position is (500,500,500) and need it to look at (0,0,0) but I can't figure out how to get the yaw and pitch between them so I can set the rotation correctly. I've read about normalizing the vectors and using the cross and dot product, but you can't normalize (0,0,0) as it has no direction so I'm a bit lost as to what to do. Any help would be appreciated.