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?
Answer
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.
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.
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.
No comments:
Post a Comment