views:

42

answers:

1
+1  Q: 

XNA and Matrices

So thanks to many of the replies from this board, I have a much better understanding of some of what's under the hood but I just need this little bit more to get a firm understanding. So I am reading through part of Riemer's tutorials here:

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

I'll use the carriage example as it's fairly simple.

So the series of transformations for the carriage is this:

1: Matrix.Identity;
2: Matrix.CreateTranslation(xPos, yPos, 0)
3: Matrix.CreateScale(playerScaling)
4: Matrix.CreateTranslation(0, -carriage.Height, 0)

So my questions are these:

Riemer states:

1: If we would render the image just like it is (or: with the Identity transformation), it would be rendered in its original size in the top-left corner of the screen.

  1. I don't get in what context this is applicable. Is he just referring to the fact that before the draw, all images start at the world origin of 0,0? Or is he saying that when these transformations take effect, the origin of the object will now be placed at the upper-left and translated back to the world origin?

2: First, the carriage image is moved so its top-left point is at the position specified as second argument in the SpriteBatch.Draw method.

  1. Ok, so is he saying this is what happens under the hood for what happens during the draw method or that he is just passing the same parameter as what you send to the draw method?

4: Finally, and this is the most challenging step: the image is moved over the Y axis, since in our SpriteBatch.Draw method we’ve specified (0, carriageTexture.Height) as origin. Very important: it is moved over its own Y axis, which has been scaled down. So instead of being moved over 39 screen pixels, the carriages will be moved vertically over 39*0.4=16 pixels pixels (since carriageTexture.Height = 39 and playerScaling = 0.4).

  1. Ok, so we've been positioning things using these matrices in our world/screen space. When you scale something down, I don't get why it will now start moving in accordance to the object's local space. For example, our first translation matrix moved according to the world/screen space but now he states "it is moved over its own Y axis".

  2. Why is the identity matrix optional?

Thus, I am having trouble connecting back the draw method with these matrix multiplications, what the deal with the object going to the world origin is and why the create translation shifted its behavior.

+1  A: 

1.1: Images are drawn at the upper-left hand corner by default on this system. Not all systems are like that, so it's worth pointing that out in the documentation. So if none of the transformations are applied (or only the identity one, which doesn't do anything), it will draw in the upper-left hand corner.

2.1: Once you've applied the transformation in line 2 it will translate (move) the image xPos, yPos across the screen. When exactly it happens isn't that important, but it'll probably be applied by the graphics card at draw time.

4.1: No, you've always been positioning by local space. It's just that until line 3, local space has been the same size as global space. Now local space is scaled, so translations will be scaled as well.

4.2: Most systems initialize matrices to the identity matrix. The identity matrix by definition doesn't do anything (if you multiply it with matrix A you'll get matrix A again). So if you omit it, it doesn't matter. It is useful for being explicit or for resetting a matrix later.

samkass
So then is the draw method then applying transformations for me under the hood when it scales/rotates and translates? If I scale first using a matrix and then in draw specify a position, is draw going to move based on the now scaled local space or the screen/world space?
Ilya