3
\$\begingroup\$

I noticed that when writing a camera for my 2D game that the camera rotational value of -6 and 6 is equivalent to 0.

That is, -6 = -12 = -18 = -24 = 6 = 12 = 18 = 24.

I was wondering if it was considered good practice to always keep the rotational value between -6 and 6, like so:

    public float Rotation
    {
        get
        {
            return rotation;
        }
        set
        {
            rotation = value;

            if (rotation <= -6 || rotation >= 6) rotation = 0;
        }
    }

Does it matter either way?

\$\endgroup\$

1 Answer 1

6
\$\begingroup\$

In XNA, the easiest way to do this is to just have MathHelper do the angle wrap-around for you all by itself, using code like this:

rotation = MathHelper.WrapAngle( rotation );

Here's what's going on, and how it works under the hood:

Your rotation value is expressed in radians. (Radians are like degrees, but instead of being in a range of [0 .. 360], they exist in a range of [0 .. 2*PI] or [-PI .. PI], where PI is an irrational numerical constant approximately equal to 3.1415927.

To answer your question: Yes, it's usually considered good practice to keep angle values within your selected bounds. As you'd expect, an angle of 0 is identical to an angle of 2 * PI which is equal to an angle of 200,000 * Pi; they all represent exactly the same angle. But because of how floating point numbers are represented in a computer, very large numbers become less precise, so it's usually considered a good practice to keep them close to zero when it's practical to do so.

In XNA, PI is provided to you in MathHelper.Pi, and 2.0 * PI is provided in MathHelper.TwoPi. So a straight-forward way to unwind your angle into the [-PI .. PI] range would be:

while ( rotation < -MathHelper.Pi )
{
  rotation += MathHelper.TwoPi;
}
while ( rotation > MathHelper.Pi )
{
  rotation -= MathHelper.TwoPi;
}

If you prefer the [0 .. 2.0 * PI] range, then you could adjust the parameters in the 'while' loops above, to use those ranges instead.

In practice, of course, you actually want to use the MathHelper.WrapAngle function that I showed at the start of this answer. But this is the basic idea of what that function does internally.

\$\endgroup\$
1
  • \$\begingroup\$ This is just the response I was looking for! Thanks so much. \$\endgroup\$ Feb 19, 2012 at 0:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .