Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have followed the tutorial at http://www.david-amador.com/2009/10/xna-camera-2d-with-zoom-and-rotation/ to implement my 2D camera and everything works just fine. However this leaves me confused. From that implementation i see that first there is the translation applied, then rotation, then scale and then again translation to "offset" the camera.

However, in every book i read, there is one core principle to keep in mind when working with matrix translations, and that is the order of the application of transformations. And it is different: Scale, Rotation, Translate.

What am i not getting?

share|improve this question
3  
it works as expected, because it's doing the translation, then the rotation, then the scale. right order. The last translation is just to make the camera focus on the center of the screen, instead of the top left corner. –  Gustavo Maciel May 6 '12 at 18:08
    
@GustavoMaciel However when i look at this example toymaker.info/Games/XNA/html/xna_matrix.html it uses a different combinatoin, and that might be confusing. –  UrbanEsc May 7 '12 at 7:32
1  
Recommended order for XNA is SRT. –  David Lively May 7 '12 at 17:35
    
@DavidLively the source of the confusion might be exactly that, since you position an object via SRT, but positioning the 2D camera requires TRS, if you will –  UrbanEsc May 9 '12 at 8:58
    
@UrbanEsc ah - you're right of course. View matrix vs world matrix. –  David Lively May 9 '12 at 16:04
add comment

2 Answers 2

up vote 3 down vote accepted

Short Answer

SRT is the usual order for a world matrix. The camera uses a view matrix which works by taking the entire world and transforming it in the opposite direction from where the camera is located. Since this opperation is the opposite of a world matrix, the multiplication order needs to be reversed as well as its arguments.

Long Answer

The SRT order mentioned in the books is the correct order to create a transformation matrix to position an object in the world, or in other words, a world matrix.

Your 2D camera class on the other hand, is creating a view matrix which works almost in the opposite way. To be more specific, it moves the entire world in the opposite direction as the camera, giving the illusion that the camera is the new origin of the world.

So for instance, if you have a camera that's positioned at (50, 0) and rotated 45º to the right, the way to simulate that is to perform the inverse transformation on the world, i.e. translate everything in the world by (-50, 0) so that the camera becomes positioned at (0,0) and then rotate it 45º to the left to give the illusion that the camera is rotated to the right.

The conclusion is, since you're performing the inverse operation as a world matrix, the multiplication order needs to be reversed too.

With Pictures

Not sure if it helps, but I tried making a few pictures to demonstrate why order matters, and why there's a correct order (they didn't turn out too well though..).

First for world matrices. Imagine a square object located at (50,0), rotated 45 degrees to the left, and double its normal size. Since transformations are always relative to the origin the image below should make it clear why SRT gives the correct results, while TRS does not.

enter image description here

Now, as I explained earlier the view matrix works by doing the opposite of the world matrix. Look at the image below and imagine that the red square outline is the camera. The job of the view matrix is to transformeverything so that the camera is placed at the origin, while maintaining all the relative proportions and positions between the camera and the world. The example shown is the correct transformation order for a view matrix.

enter image description here

As you can see, TRS works in this case, and places the camera at the origin. Now if you try doing it SRT instead, while using the same parameters, the camera will end up far away from the origin which is not what we want.

share|improve this answer
1  
+1 For a very clear and constructive definition –  UrbanEsc May 9 '12 at 12:24
add comment

After some poking around, i found an answer to a different question, that discusses various matrix operation orders and what they are used for. Though they lack explicit detail they are a good reference

Transformation order when dealing with Meshes

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.