2D Camera Transformation

Started by
15 comments, last by mwkenna 11 years, 4 months ago
Exactly, I have tried applying the rotation before the transform but it will always rotate around (0,0). I want to always rotate around the current screen center (camera.position.x + viewport.width * 0.5, camera.position.y + viewport.height * 0.5) if that makes any sense?

So the camera movement will go according to screen axis, the rotation will always happen at the current camera focus (center of the screen).

Thanks,
Mark.
Advertisement

I still think you are getting a bit confused with view and projection matrices, so I've put a bit of an example together for you that demonstrates how I would normally perform camera rotation in a 2D game:

> http://aimee.xpod.be/CameraRotationExample2D.rar

Note that in this example I disregard the SpriteBatch class to demonstrate another way of drawing sprites (by using the centre of the sprite instead of the top left as origin) which I believe will help you understand things a bit more. Also I create an orthographic projection matrix which means you don't need to figure out where the centre of the screen is because it is placed always at (0,0,0).

Again hope this helps, and if you still get stuck, let me know.

Aimee

We are now on Tumblr, so please come and check out our site!

http://xpod-games.com

Well, the quicker way I can think of is to move the camera.X and Y in a trignometric fashion, so that it moves left and right even when rotated.

Check out my new blog: Morphexe

Thanks Aimee - the solution seems to do what, I will be taking apart the code in detail and learning what each part does.

Morphex - Do you think you could also post that sample code with the trigonometric positioning - I'm guessing that's still using the spritebatch right?

Thanks,

Mark.

Hi Aimee

I've been looking over your code (which is really well put together) but it's made me try to jump too far forward in my learning process.

Is it possible to alter the code so that it still uses the SpriteBatch which, as I understand it, does not require the use of a projection matrix at all?

Thanks,

Mark.

Hi Mark, No matter what mechanism you use in XNA, you will always require a projection matrix to draw things. If no projection matrix is used then you can't properly place things onto the screen because like I have mentioned before that is the job of the projection matrix.

The SpriteBatch sets up a lot of things for you behind the scenes (including a projection matrix), however the question you started this thread with requires a more comprehensive understanding of how matrices work. In my example I did not use a SpriteBatch because using one actually makes things more complicated, the projection matrix by default sets the origin into the top left corner, which makes rotating the camera needlessly more difficult than it needs to be.

I promise you that if you take a bit more time to try and understand what I've said over the last few replies that you'll see why I've gone into so much depth, It's not just as simple as dropping a SpriteBatch in there, messing about with a matrix and expecting everything to be perfect.

Slow down, learn the mathematics, be patient and don't try to force things. A lot of people never get anywhere because they "want to know how to do stuff RIGHT NOW and sod everything that looks to difficult". It's likely there is a smart programmer here who'd just throw you a quick answer that'd see you off again using the SpriteBatch, but you would never learn anything, and you'd be right back here in a few weeks asking us to fix a problem with the sprite batch that you don't know how to fix because something went wrong and you never learnt the fundamentals in the first place.

Aimee

We are now on Tumblr, so please come and check out our site!

http://xpod-games.com

Hi Aimee

Thanks for the advice, I am actually trying to slow it down a notch. I'm working through a book which only deals with the SpriteBatch (for 2D) - I am on transformations and how to model a simple camera, but the rotation/translation in the camera seemed wrong to me (non-axis aligned) so I thought I would post to see if there was a way to "fix" it.

Thanks for all your replies.

Mark.

This topic is closed to new replies.

Advertisement