Making a Camera look at a target Vector

Posted by Peteyslatts on Game Development See other posts from Game Development or by Peteyslatts
Published on 2012-12-09T23:10:18Z Indexed on 2012/12/09 23:45 UTC
Read the original article Hit count: 262

Filed under:
|

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);
        }
    }

© Game Development or respective owner

Related posts about XNA

Related posts about camera