Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

In XNA, you can see that to draw a rotated sprite with SpriteBatch, you'll need a float describing the angle in radians.

I'm used to making games in OpenGL. I just want a rapid prototyping environment in XNA, but I'm a little lost. It's my preference to always use Vectors to store my rotation, to do vector math, et cetera.

So my question is: in XNA, is better to keep a vector for your rotations, and each frame transform to a float angle, or just keep the float angle itself? If I prefer the vector, what would be my drawbacks compared to just using a float?

share|improve this question

4 Answers 4

up vote 6 down vote accepted

You can always convert from vector to angle or vice-versa. You might want to run some tests and profile the running code to see which direction of conversion is faster, if that makes a difference.

Someone else here said you can't add or subtract a fixed angle from a vector. You actually can, of course, using matrix multiplication:

Addition of angle θ

x' = x cos θ - y sin θ
y' = y cos θ + x sin θ

Subtraction of angle θ

x' = x cos θ + y sin θ
y' = y cos θ - x sin θ

Whether or not that is faster than updating θ and rederiving x and y from θ is something you would have to execute in an actual program to know for sure, but in the case where θ is constant, it's very likely going to be faster.

That said, keeping angles as vectors will subject them to magnitude drift due to accumulated floating-point arithmentic rounding error which occurs during repeated iterations. So depending on how your library works, you may need to normalize your direction vector after each step or after every say 1000 steps. You don't want it to shrink to zero after a billion iterations, or grow to infinity. So the question is: Does your library automatically normalize your angle vector, or does it assume a magnitude of 1?

If you can afford the memory, it might be advantageous to store the angle both ways, updating both together, and then at any instant use whichever form is most convenient in your code.

If you can afford the CPU cycles, then in my experience it's easiest just to store the angle as a single value. Will you be applying angular acceleration to your angular velocity? If so, then that is another benefit to storing the information as an angle rather than a 2D vector.

share|improve this answer
1  
wow, much more details, new accepted answers, thanks! –  Gustavo Maciel Jan 23 '12 at 19:24

Ideally, it is better to be using quaternions to represent rotations. Vector based rotations have the problem of getting to something called "gimbal lock". However Quaternions are not susceptible to that. Here are more details: Quaternion and Rotation Primer (Courtesy: Ogre3D's Wiki).

EDIT: It's also easy to convert a quaternion to a rotation matrix for transformation. And also be able to store scaling factors (if it interests you). If you do not require scaling factors to be part of your rotation quaternion, you can always 'normalize' the quaternion.

share|improve this answer
2  
True for 3D, but he's using 2D as indicated by the "2D" tag. –  John McDonald Jan 26 '12 at 19:42
    
Sorry! My bad. I failed to see it! –  Vite Falcon Jan 27 '12 at 17:37
    
Even though its a 2D game he is making, other people need to see the importance/helpfulness of Quaternions. –  Matt Jensen Jan 28 '12 at 17:18

Good for you for using a Vectors. Although angles & trig do work well for 2d, they do not work so well for 3d. If you ever make that transition, and you are already accustom to using vectors and linear algebra instead of angles and floats, your transition will be much smoother and easier.

share|improve this answer

A float is harder to work with, as I have discovered during the last few months. Applying things like physics, forces or flocking algorithms require a Vector to work with. The only thing you can do with a float that you can't do with a Vector, is add or subtract a fixed angle from it. I would use Vectors and calculate the angle as needed, even every frame if that's what was required.

share|improve this answer
    
Going with vectors, it seems that it's the best alternative. –  Gustavo Maciel Jan 23 '12 at 3:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.