Question

I'm trying to make a simple isometric game engine but have some problems with the camera. When i have it like this i can see my model from the front. But i want to see it from an isometric perspective. I tried using a lot of methods but none seem to work. Perhaps I got stuck in the code itself? Can you guys help me with the code perhaps?

public class Camera : PositionedObject
{

    #region Fields
    private Matrix cameraRotation;

    #endregion


    #region Properties
    public Matrix View
    {
        get;
        set;
    }

    public Matrix Projection
    {
        get;
        protected set;
    }

    public Vector3 Target
    {
        get;
        set;
    }
    #endregion

    #region Constructor
    public Camera(Game game, Vector3 position, Vector3 target, Vector3 rotation, bool Orthographic, float near, float far)
        : base(game)
    {      
        Position = position;
        RotationInRadians = rotation;
        Target = target;


        if (Orthographic)
        {
            Projection = Matrix.CreateOrthographic(Game.Window.ClientBounds.Width, Game.Window.ClientBounds.Height,
                near, far);
        }
        else
        {
            Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
                (float)Game.Window.ClientBounds.Width / (float)Game.Window.ClientBounds.Height, near, far);
        }




    }
    #endregion

    #region Public Methods

    public override void Initialize()
    {
        base.Initialize();
        cameraRotation = Matrix.Identity;

    }



    public override void Update(GameTime gameTime)
    {
        base.Update(gameTime);

        cameraRotation = Matrix.CreateFromAxisAngle(cameraRotation.Forward, RotationInRadians.Z)
            * Matrix.CreateFromAxisAngle(cameraRotation.Right, RotationInRadians.X)
            * Matrix.CreateFromAxisAngle(cameraRotation.Up, RotationInRadians.Y);

        Target = Position + cameraRotation.Forward;
        View = Matrix.CreateLookAt(Position, Target, cameraRotation.Up);
    }

    public void Draw(BasicEffect effect)
    {
        effect.View = View;
        effect.Projection = Projection;
    }
    #endregion
}
Was it helpful?

Solution

The easiest way is to calculate the camera position based on the focus point (a point on your ground, or whatever).

//Lets start with looking at origo for now.
Vector3 FocusPoint = Vector3.Zero;

//This tells us where the camera should be, RELATIVE to the point we are watching.
//I set this a little up and a little back
Vector3 CameraOffset = new Vector3(0f, 20f, 20f);

Matrix ViewMatrix
{
    get
    {
        //The Offset is just up and back, we need to rotate it 45*
        var rotatedOffset = Vector3.Transform(CameraOffset, Matrix.CreateRotationY(MathHelper.PiOver2 * 0.5f));

        //Now we can create out viewmatrix. No need to use a transformed "up" unless it's not going to be upside down or something.
        return Matrix.CreateLookAt(rotatedOffset, FocusPoint, Vector3.Up);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top