Rotate around the centre of the screen
- by Dan Scott
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)