views:

36

answers:

2

I'm using XNA but it doesn't matter too much for this example. So let's say I have a sprite. I then apply a scaling matrix before anything. Is the scaling matrix applied scaling the local axis of the sprite or just moving the points down? In other words, is applying a scaling matrix of 0.5f in the world space to my sprite at the world origin scaling down the local axis of the sprite or just all the points that make up that sprite by half?

The same kind of applies to a translation and then scaling. In my head, I picture a translation matrix of 30,30 as moving the sprite's local origin to 30,30 and as a result, the sprite's local axis to 30,30. Then, scaling by 0.5f would scale back the local axis but I don't see why the origin of the sprite would now be at 15,15.

This confusion compounds the fact that is you perform a translation of 1 to the right on the x-axis in the world, you are now moving based on the scale which you applied (so you would only move .5 in the world). This leads me to believe that the scale is applied to the object's own axis.

Btw, if you guys talk about the origin in your followups, could you state which origin you are referring to?

Thanks

+1  A: 

Normally a sprite is defined by it's vertices (points). Applying a scaling matrix to a sprite will transform the vertices (points) of the sprite.

A scale matrix always assumes (0, 0) is the origin of the scale transform. So if you scale a sprite centered at (30, 30) all points will stretch away from the (0, 0) point. If it helps, imagine the sprite as a small dot on a circle around the (0, 0) point with that entire circle being scaled.

If you want to scale a sprite at (30, 30) from the center of the sprite, you have to translate the center of the sprite to (0, 0) first, then translate the sprite back out to (30, 30) after the scale has been performed.

So that would be:

   Translate(-30, -30)
   Scale(0.5)
   Translate(30, 30)
Empyrean
I think I kind of get it now if I think about it a certain way. At the beginning, my local and world axis are the same so a translation in the x direction of 1 is a translation of 1 since both the world and local space are of the same scale. Applying a scale of 0.5f will essentially shrink the pixel width of my local space thus the points are shrunk back and the world coordinate result is a coordinate which is cut in half. Now if I were to translate this sprite, what happens is a translation occurs along the local space's scaled down axis and we can identify the world coordinates.
Ilya
The last sentence doesn't sound right. In your question you talk about translating then scaling the sprite. Here you are talking about scaling then translating. Those are two very different operations. If you scale(0.5) then translate (30, 30) you end up with a sprite half the size at point (30, 30). If you translate (30, 30) then scale (0.5) you end up with a sprite half the size at point (15, 15). The point is the scale always works from point (0, 0). If the center of the sprite is not at (0, 0) when you do the scale, it will be an off center scale.
Empyrean
A: 

To expand on Empyrean's answer, 3D worlds usually have at least four coordinate systems, each with its own local origin:

  • Object Space
  • World Space
  • Camera Space
  • View Space (2D!)

with three transformations:

  • Object to World
  • World to Camera
  • Camera to View

You can create new coordinate systems, for example 'Model Space', with the transformation 'Model to Object'. Using this, you get a series of steps:

Model -> scale -> Object Object -> rotate -> translate -> World World -> rotate -> translate -> Camera Camera -> perspective -> View

In OpenGL you would push the matrices in the reverse order listed above, so the Model->Object transformation is the last to be pushed, and OpenGL should render the object correctly. I would assume XNA / DirectX has a similar system.

Getting more complex, Model Space can have a hierarchy of translations, scales and rotations in a tree to produce a skeletal system which can then be used to deform the model mesh. This is usually called Skinning.

So, to answer the question, depending on which transformation you apply a rotation transformation, for example, you will get different results. In the Model->Object transformation, the model will rotate about the object's origin. In the Object->World transformation, the object will rotate about the world's origin.

Skizz