Rotate around the centre of the screen

Posted by Dan Scott on Game Development See other posts from Game Development or by Dan Scott
Published on 2013-08-16T14:00:14Z Indexed on 2013/10/21 16:11 UTC
Read the original article Hit count: 353

Filed under:
|
|
|

I want my camera to rotate around the centre of screen and I'm not sure how to achieve that. I have a rotation in the camera but I'm not sure what its rotating around. (I think it might be rotating around the position.X of camera, not sure) If you look at these two images: http://imgur.com/E9qoAM7,5qzyhGD#0

http://imgur.com/E9qoAM7,5qzyhGD#1

The first one shows how the camera is normally, and the second shows how I want the level to look when I would rotate the camera 90 degrees left or right. My camera:

public class Camera
{
    private Matrix transform;
    public Matrix Transform 
    {
        get { return transform; }
    }

    private Vector2 position;
    public Vector2 Position 
    {
        get { return position; }
        set { position = value; }
    }

    private float rotation;
    public float Rotation 
    {
        get { return rotation; }
        set { rotation = value; }
    }

    private Viewport viewPort;

    public Camera(Viewport newView) 
    {
        viewPort = newView;
    }

    public void Update(Player player) 
    {

        position.X = player.PlayerPos.X + (player.PlayerRect.Width / 2) - viewPort.Width / 4;

        if (position.X < 0)
            position.X = 0;

        transform = Matrix.CreateTranslation(new Vector3(-position, 0)) *
            Matrix.CreateRotationZ(Rotation);

        if (Keyboard.GetState().IsKeyDown(Keys.D)) 
        {
            rotation += 0.01f;
        }
        if (Keyboard.GetState().IsKeyDown(Keys.A))
        {
            rotation -= 0.01f;
        }
    }
}

(I'm assuming you would need to rotate around the centre of the screen to achieve this)

© Game Development or respective owner

Related posts about XNA

Related posts about c#