Question

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 :

  • I calcul new projection of scene to match the new viewport (x y z in my world).
  • I reorganise part of my scene(chunk) to draw them in a correct order
  • I reorganise 'tiles' of 'chunks' to draw them in a correct order
  • I can keep the correct center with a 180 degres rotation.

What's do not working :

  • I cannot find a correct translation to apply to my camera after a 90 degres rotation.

What I know :

  • To keep the same center on a 180° rotation with my camera I have to do this :

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

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top