Sunday, January 9, 2011

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.

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

    Ilya : Awesome explanation. Now I picture this as more of a function where you input some vectors and get a new representation of that vector. A scale in my head is "what if I shrunk down those basis vectors in that matrix" so now I would get a shrunken image because those basis vectors are shrunk down as if I did that to the local space's x and y coordinates. So everything is the same in local space and this is more of a representation rather than a change.
    Chewy Gumball : Exactly, there is a transformation function from local to world space and applying these transformations only affects this function, not the object itself.
  • Think of it as rotating an entire coordinate system from one basis to another. That matrix can take any tuple in (R,R) and it would have a point in the new coordinate system.

    If I have a rotation matrix like this:

    
       0 -1
       1  0
    

    I pass vectors from my starting space, and they get turned into vectors in the finishing space. this include the axes [1,0] and [0,1] which get turned into [0,1], and [-1,0] (i.e. it gets rotated counter-clockwise 90 degrees.

    You may have noticed that the rotated axes are the columns of the matrix. In fact, the columns of a matrix represent the axes of whatever coordinate system you're transforming your numbers into by definition. That's because a coordinate system is just a way of representing a point in space in terms of its projection onto a bunch of independent vectors (blah blah).

    Hope this helps.

    Ilya : Yup, now I get it. Those basis vectors act as "template" axis for you to mess around with, as if you were actually committing real changes to the local x and y axis. Whatever output you get is what would have occurred in the local x and y axis (whatever you define your basis vectors to be) and the output goes into the world matrix. Thanks.

0 comments:

Post a Comment