What is this rotation behavior in XNA?

I am just starting out at XNA and wondering about rotation. When you multiply a vector by the rotation matrix in XNA, it goes counterclockwise. I understand it.

However, let me give you an example of what I will not receive. Say I'm loading a random art object into a pipeline. Then I create some variable to increase each frame by 2 radians when the update method starts (testRot + = 0.034906585f). The main thing in my confusion is that the object rotates clockwise in this space of the screen. This bothers me because the rotation matrix will rotate the vector counterclockwise.

One more thing, when I indicate where my position vector is, as well as my origin, I understand that I revolve about the origin. Should I assume that the perpendicular axis passes through this source of assets? If so, where does the rotation begin? In other words, am I starting a rotation from the top of the Y axis or the x axis?

+6
xna
source share
2 answers

XNA SpriteBatch works in the Client Space . Where "up" is Y-, not Y + (as in Cartesian space, projection space, and what most people usually choose for their world space). This leads to the appearance of clockwise rotation (not counterclockwise, as it would be in Cartesian space). The actual coordinates that the rotation produces are the same.

Rotations are relative, so they do not โ€œstartโ€ from any given position.

If you use mathematical functions like sin or cos or atan2 , then the absolute angles always start from the X + axis as zero radians, and the positive direction of rotation rotates in the Y + direction.


The order of SpriteBatch operations looks something like this:

  • Sprite starts as a square with the upper left corner at (0,0), its size is the same as the size of its texture (or SourceRectangle ).
  • Move the sprite back to its beginning (thus, its beginning begins with (0,0)).
  • Scale sprite
  • Sprite rotation
  • Translate sprite by its position
  • Apply matrix from SpriteBatch.Begin

This puts the sprite in the client space.

Finally, a matrix is โ€‹โ€‹applied to each batch to transform this client space into the projection space used by the GPU. (The projection space is from (-1, -1) in the lower left corner of the viewport to (1,1) in the upper right corner.)

+11
source share

Since you are new to XNA, let me introduce a library to help you learn. It is called XNA Debug Terminal and is an open source project that allows you to run arbitrary code at runtime. This way you can see if your variables have the value you expect. All this happens on the terminal display on top of your game and without pausing the game. It can be downloaded at http://www.protohacks.net/xna_debug_terminal It's free and very easy to set up, so you really have nothing to lose.

+2
source share

All Articles