Wednesday, March 25, 2015

mathematics - How to think about 2D scaling/rotation transformations


This is a kind of embarassing question to me since I'm getting more in-depth with XNA but some times my way of thinking about things in my head contradicts an example and I need to re-think it to make it work.


So let's say I am just working with a sprite that is 10 wide and 10 high.


Scaling:


When I apply a scaling matrix, am I just shifting the points back or scaling the X-axis and Y-axis in the LOCAL coordinate space of that sprite? I ask because on this page:


http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series2D/Coll_Detection_Matrices.php


and


http://www.riemers.net/eng/ExtraReading/matrices_geometrical.php


It shows the X and Y of the local axis the sprite is in being scaled down. So in my head, I think that a scaling means that you scale down the local axis space where the sprite is located which makes it smaller.



Rotation:


When I do a rotation matrix, am I rotating the local x and y axis of the sprite around the world origin or rather do I think about it as rotating the local coordinate space entirely around the world origin?


Translation:


This to me is just moving that scaled image with those smaller x and y axis to some other point in the world.



Answer



For me, the easiest way to think about this is to remember that each coordinate space can be expressed in world coordinates by a vector from the world origin to the local origin which I just call the position vector and two (in 2D or three in 3D) basis vectors. Any translation moves only the head of the position vector (the tail is always anchored at the world origin). Rotation and scaling can affect all three (or four) vectors depending on the specific rotation or scale as well as the order you have applied transformations already.


This, along with my knowledge that these transforms don't change the object in local space (a point at (1,0,0) is always at (1,0,0) in local space, no matter what the basis vectors are in world space), only its appearance in world space, allow me to visualize these transforms with relative ease.


In essence you are thinking the right thing, but you must be aware that these transforms don't actually affect the local coordinate space of an object, only its mapping into world space.


For example:


I have a an object defined by {(0,0), (1,0), (1,1), (0,1)} in local space. The Position vector is (0,0), and the basis vectors are (1,0) and (0,1). In other words, my transform matrix is just an identity matrix and the object looks like a square with bottom left corner at the origin both in the world view and in a local view.



If I were to apply a translation, say by the vector (2,3), only the position vector changes. My object is still defined as {(0,0), (1,0), (1,1), (0,1)} in local space, but the object in world space has been moved up and over. The object still looks like a square in both views however.


If I were to then apply a scale, say by (1/2, 1/3), the position vector would be changed to (1,1) and the basis vectors would be changed to (1/2, 0) and (0,1/3). Note that my object is STILL defined as {(0,0), (1,0), (1,1), (0,1)} in local space, but if I view it in world space it is a rectangle that has been shifted up and to the right, not a square with bottom left corner at the origin as it is defined in local space.


Rotations are very similar, but the changes in the basis vectors are not as easy to calculate in my head so I shall do a very simple example with a 45 degree rotation counter clockwise around the origin. The position vector would be changed to (0,sqrt(2)), while the basis vectors would be changed to ~(-0.35, 0.35) and ~(0.24,0.24). The object would now be a slanted diamond moved up in world space.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...