# Rotate around the centre of the screen

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)

• Have you tried rotating the camera around its Z axis? It's the axis that points forward. The center of the screen is at the Z axis of the camera. – MichaelHouse Aug 16 '13 at 14:13
• That's what I'm doing ins't it? – Dan Scott Aug 16 '13 at 15:31
• Looks that way. You didn't really say what's not working about it. – MichaelHouse Aug 16 '13 at 15:32
• Well, it rotates. It just that I think it may be rotating at one of the corner of the screen because when I rotate left or right 90 degrees the level will be at the left or right of the screen, and the player is off the screen. This is what it's currently doing: youtube.com/watch?v=_f2bpqM5uTo&feature=youtu.be This is what I want: youtu.be/P_w8f792Oqs?t=9m3s To me it looks like the level has rotated, not the camera(even though it's in trials evolution) – Dan Scott Aug 16 '13 at 15:46

Here's some code I use to generate a matrix for my camera's transormation:

Matrix mTranslation, mRotate, mOriginAfterScale, mScale;

mTranslation = Matrix.CreateTranslation(-_position.X, -_position.Y, 0);
mRotate = Matrix.CreateRotationZ(_rotation);
mOriginAfterScale = Matrix.CreateTranslation(_origin.X, _origin.Y, 0);
mScale = Matrix.CreateScale(_zoom);

_transform = Matrix.Identity * mTranslation * mRotate * mOriginAfterScale * mScale;


_origin is a Vector2 which is half the values of the width and then height. Then you need to divide it by the zoom, i.e:

Vector2 _origin = new Vector2(screenWidth, screenHeight) / (2 * _zoom);


_position is the position you want your camera to be centered on.

I Build a class to return a vector that moves between to points by rotating around a point

    class Movement
{
Vector2 Postion;
static float slices = (float)(2 * Math.PI) / 16; // circle split into 16 bits
float Rotation;
bool goLeft;
public Movement()
{
Rotation = slices;
}
/// <summary>
/// it outputs Vector2, it needs  Vector2 centerPoint and GameTime gameTime
/// </summary>
public Vector2 Update(Vector2 centerPoint, GameTime gameTime)
{
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; // movement based on cpu chip
float min = slices * 2; // min point
float max = slices * 6; // max point
float distance  = 260; // distance away from point
if (goLeft)
{
Rotation -= 0.7f * elapsed;
if (Rotation < min) { goLeft = false; }
}
else
{
Rotation += 0.7f * elapsed;
if (Rotation > max) { goLeft = true; }
}

this.Postion.X = ((float)-Math.Cos((double)Rotation) * (distance)) + centerPoint.X;
this.Postion.Y = ((float)-Math.Sin((double)Rotation) * (distance)) + centerPoint.Y;

return Postion;
}
}


hope this helps