XNA: Camera's Rotation and Translation matrices seem to interfere with each other
Posted
by
Danjen
on Game Development
See other posts from Game Development
or by Danjen
Published on 2014-08-25T12:24:48Z
Indexed on
2014/08/25
16:29 UTC
Read the original article
Hit count: 5690
I've been following the guide here for how to create a custom 2D camera in XNA. It works great, I've implemented it before, but for some reason, the matrix math is throwing me off.
public sealed class Camera2D
{
public Vector2 Origin { get; set; }
public Vector2 Position { get; set; }
public float Scale { get; set; }
public float Rotation { get; set; }
}
It might be easier to just show you a picture of my problem: http://i.imgur.com/H1l6LEx.png
What I want to do is allow the camera to pivot around any given point. Right now, I have the rotations mapped to my shoulder buttons on a gamepad, and if I press them, it should rotate around the point the camera is currently looking at. Then, I use the left stick to move the camera around. The problem is that after it's been rotated, pressing "up" results in it being used relative to the rotation, creating the image above.
I understand that matrices have to be applied in a certain order, and that I have to offset the thing to be rotated around the world origin and move it back, but it just won't work!
public Matrix GetTransformationMatrix()
{
Matrix mRotate = Matrix.Identity *
Matrix.CreateTranslation(-Origin.X, -Origin.Y, 0.00f) * // Move origin to world center
Matrix.CreateRotationZ(MathHelper.ToRadians(Rotation)) * // Apply rotation
Matrix.CreateTranslation(+Origin.X, +Origin.Y, 0.00f); // Undo the move operation
Matrix mTranslate = Matrix.Identity *
Matrix.CreateTranslation(-Position.X, Position.Y, 0.00f); // Apply the actual translation
return mRotate * mTranslate;
}
So to recap, it seems I can have it rotate around an arbitrary point and lose the ability to have "up" move the camera straight up, or I can rotate it around the world origin and have the camera move properly, but not both.
© Game Development or respective owner