Cyril ALFARO
Cyril ALFARO

Reputation: 1681

Calcul new coords of camera after a 90 degres rotation in a isometric 2D projection

I made a 2D isometric renderer. It works fine but now I want to show my scene from 4 differents point of view (NO NW SE SW) but, on a 90° rotation, my camera cannot keep the center of my scene on screen.

What's working :

What's do not working :

What I know :

camera.Position -= new Vector2(2 * camera.Position.X + camera.Width, 2 * camera.Position.Y + camera.Height);

Illustration sceen from SE point of view enter image description here enter image description here

Upvotes: 2

Views: 829

Answers (1)

If the center of your map is origo (0,0,0), this gets easy:

First you store your default camera position in a Vector3 CameraOffset, then you calculate position using a rotation-matrix. 90* in redians is half a Pi, so we will use PiOverTwo. We will also use an enum to decide what direction to be pointing, so you can say

Camera.Orientation = Orientation.East;

and the camera should fix itself :)

public enum Orientation
{
    North, East, South, West
}

in camera:

public Vector3 Position { get; protected set; }

Vector3 _CameraOffset = new Vector3(0, 20, 20);
public Vector3 CameraOffset
{
    get
    {
        return _Orientation;
    }

    set
    {
        _Orientation = value;
        UpdateOrientation();
    }
}

Orientation _Orientation = Orientation.North;
public Orientation Orientation
{
    get
    {
        return _Orientation;
    }

    set
    {
        _Orientation = value;
        UpdateOrientation();
    }
}

private void UpdateOrientation()
{
    Position = Vector3.Transform(CameraOffset, Matrix.CreateRotationY(MathHelper.PiOverTwo * (int)Orientation));
}

If you want a gliding movement between positions, I think I can help too ;)

If your camera does not focus on Vector3.Zero and should not rotate around it, you just need to change:

Position = Vector3.Transform(CameraOffset, Matrix.CreateRotationY(MathHelper.PiOverTwo * (int)Orientation));

to:

Position = Vector3.Transform(CameraOffset, Matrix.CreateRotationY(MathHelper.PiOverTwo * (int)Orientation) * Matrix.CreateTranslation(FocusPoint));

Here, FocusPoint is the point in 3D that you rotate around (your worlds center). And now you also know how to let your camera move around, if you call UpdateOrientation() in your Camera.Update() ;)

EDIT; So sorry, totally missed the point that you use 2D. I'll be back later to see if I can help :P

Upvotes: 1

Related Questions