# XNA: Camera's Rotation and Translation matrices seem to interfere with each other

Filed under:
|
|
|
##### matrix

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.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

• #### Camera for 2.5D Game

as seen on Game Development - Search for 'Game Development'
I'm hoping someone can explain this to me like I'm 5, because I've been struggling with this for hours and simply cannot understand what I'm doing wrong. I've written a Camera class for my 2.5D game. The intention is to support world and screen spaces like this: The camera is the black thing on… >>> More

• #### Using XNA ContentPipeline to export a file in a machine without full XNA GS

as seen on Game Development - Search for 'Game Development'
My game uses the Content Pipeline to load the spriteSheet at runtime. The artist for the game sends me the modified spritesheet and I do a build in my machine and send him an updated project. So I'm looking for a way to generate the xnb files in his machine (this is the output of the content pipeline)… >>> More

• #### XNA - Strange Texture Rendering Issue Using XNA BasicEffect

as seen on Game Development - Search for 'Game Development'
I have been reading and working through Riemers 3D XNA tutorials to expand my knowledge of XNA from 2D into 3D. Unfortunately I am having rendering issues that I am unable to solve and I need a point in the right direction. I am not expecting the Models to look identical to Blender but there is… >>> More

• #### XNA Notes 009

as seen on Geeks with Blogs - Search for 'Geeks with Blogs'
This past week the MVPs (myself included) were on Microsoft campus for the MVP summit. So I apologize in advance if you did something cool or heard of something cool happening with XNA and XBLIGs and it’s not in my notes. I did my best to stay on top of things, but honestly this community is fast… >>> More

• #### XNA Notes 006

as seen on Geeks with Blogs - Search for 'Geeks with Blogs'
If you used to think the XNA community was small and inactive, hopefully these XNA Notes are opening your eyes. And I honestly feel like I’m still only catching the tail end of everything that’s going on. It’s a large and active community and you can be so mired down in one part of it you miss all… >>> More

• #### Are high powered 3D game engines better at 2D games than engines made for 2D

as seen on Game Development - Search for 'Game Development'
I'm a software engineer that's new to game programming so forgive me if this is a dumb question as I don't know that much about game engines. If I was building a 2D game am I better off going with an engine like Torque that looks like it's built for 2D, or would higher powered engines like Unreal… >>> More

• #### 2D Array of 2D Arrays (C# / XNA) [on hold]

as seen on Game Development - Search for 'Game Development'
I want to create a 2D array that contains many other 2D arrays. The problem is I'm not quite sure what I'm doing but this is the initialization code I have: int[,][,] chunk = new int[64, 64][32, 32]; For some reason Visual Studio doesn't like this and says that it's and 'invalid rank specifier'… >>> More

• #### Need to know the origin and coordinates for 2d texture and 2d/3d vertices in webgl

as seen on Game Development - Search for 'Game Development'
Long story short, I know my coordinates are off and I believe my indices might be off. I'm trying to render a simple 2d rectangle with a texture in webgl here's the code I have for the vbo/ibo: rectVertices.vertices = new Float32Array( [ -0.5, -0.5, // Vertice 1, bottom / left 0.0, … >>> More