Saturday, March 18, 2017

xna - Making a Camera look at a target Vector


I have a camera that works as long as its stationary. Now I'm trying to create a child class of that camera class that will look at its target.



The new addition to the class is a method called SetTarget(). The method takes in a Vector3 target. The camera wont move but I need it to rotate to look at the target. If I just set the target, and then call CreateLookAt() (which takes in position, target, and up), when the object gets far enough away and underneath the camera, it suddenly flips right side up. So I need to transform the up vector, which currently always stays at Vector3.Up. I feel like this has something to do with taking the angle between the old direction vector and the new one (which I know can be expressed by target - position).


I feel like this is all really vague, so here's the code for my base camera class:


public class BasicCamera : Microsoft.Xna.Framework.GameComponent
{
public Matrix view { get; protected set; }
public Matrix projection { get; protected set; }

public Vector3 position { get; protected set; }
public Vector3 direction { get; protected set; }
public Vector3 up { get; protected set; }

public Vector3 side
{
get { return Vector3.Cross(up, direction); }
protected set { }
}

public BasicCamera(Game game, Vector3 position, Vector3 target, Vector3 up)
: base(game)
{
this.position = position;

this.direction = target - position;
this.up = up;

CreateLookAt();

projection = Matrix.CreatePerspectiveFieldOfView(
MathHelper.PiOver4,
(float)Game.Window.ClientBounds.Width /
(float)Game.Window.ClientBounds.Height,
1, 500);

}

public override void Update(GameTime gameTime)
{
// TODO: Add your update code here
CreateLookAt();

base.Update(gameTime);
}
}


And this is the code for the class that extends the above class to look at its target.


class TargetedCamera : BasicCamera
{
public Vector3 target { get; protected set; }

public TargetedCamera(Game game,
Vector3 position,
Vector3 target,
Vector3 up)

: base(game, position, target, up)
{
this.target = target;
}

public void SetTarget(Vector3 target)
{
direction = target - position;

}


protected override void CreateLookAt()
{
view = Matrix.CreateLookAt(position, target, up);
}
}


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...