0
\$\begingroup\$

I am trying to make my top down character rotate according to degrees between its position and the mouse. I'm using this code:

float xDiff = mouseState.X - (graphics.PreferredBackBufferWidth / 2);
            float yDiff = mouseState.Y - (graphics.PreferredBackBufferHeight / 2);

float angle = (float)(Math.Atan2(yDiff, xDiff)) * 180 / Math.PI); 

But it's not working. At the place where I should get somewhere about 0 degrees - I get about -75.

\$\endgroup\$
7
  • \$\begingroup\$ Normalise ( xDiff, yDiff ) before you convert to degrees. \$\endgroup\$
    – Ben
    Nov 3, 2014 at 6:23
  • \$\begingroup\$ Can you tell me the code for it? I can't find function like that in c#. \$\endgroup\$
    – user54580
    Nov 3, 2014 at 6:31
  • \$\begingroup\$ @user54580 What are the values of xDiff, yDiff and angle when it gives the wrong result? \$\endgroup\$ Nov 3, 2014 at 9:44
  • \$\begingroup\$ @KellyThomas While the angle is -90, the xDiff == -2 and yDiff == -145. Something's not right at all here... \$\endgroup\$
    – user54580
    Nov 3, 2014 at 11:50
  • \$\begingroup\$ I'm really sorry I know this isn't a long and clear answer but I can't comment here. Have you read this ? maybe this can help you : stackoverflow.com/questions/21174767/… \$\endgroup\$
    – War-sloop
    Nov 5, 2014 at 20:40

1 Answer 1

1
\$\begingroup\$

enter image description here

If you look at a circle with the angles marked you will see that 270 degrees corresponds with (0,-1) i.e. straight down.

By convention degrees are marked as acceding in a counter-clockwise direction. They do however form a loop and as such 270 degrees (purple arrow) can also be expressed as -90 degrees (orange arrow) i.e. a quarter turn in the opposite direction.

The coordinates that you provided (-2, -145) correspond with both 269.21 and -90.79 degrees depending on how you choose to express this angle..

The documentation for Atan2() for .Net 4.5 states that it will return value is the range -π <= θ <= π, after converting to degrees this is -180 <= θ <= 180.

To further explain the expected results here is a table with the results of the following formula with the corresponding values of x and y.

angle = Math.Atan2(y, x) * 180 / Math.PI;

         x    -1       0       1
   y   +------------------------
   1   |     135      90      45
   0   |     180       0       0
  -1   |    -135     -90     -45

If you prefer to have your angles expressed in the range 0 <= θ <= 360 they are easy to convert:

while (angle > 360) {
    angle -= 360;
}
while (angle < 0) {
    angle += 360;
}
\$\endgroup\$
9
  • \$\begingroup\$ It almost works as it should. One problem that I have is that instead of 270 degrees I get - 90 and instead of 90 I get - 270. The angle is rotated for some reasons... Any ideas? \$\endgroup\$
    – user54580
    Nov 3, 2014 at 16:07
  • \$\begingroup\$ Is your order of operations backwards vs your x and y? \$\endgroup\$
    – Shroeder
    Nov 3, 2014 at 17:43
  • \$\begingroup\$ Shroeder, I tried to change the order, and it worked, but now I noticed that instead of 0 I get - 180 and instead of 180 I get - 0. How can I fix that? \$\endgroup\$
    – user54580
    Nov 3, 2014 at 18:09
  • \$\begingroup\$ @user54580 Please see edit clarifying what to expect from .net 4.5 implementation of Atan2(). It is unusual that you are receiving -270. What implementation of the math libraries are you using? .Net or Mono? Which version? Despite appearing to be out of range -270 is likely "correct" and can be translated to your desired range using code similar to the snippet above. \$\endgroup\$ Nov 3, 2014 at 23:39
  • \$\begingroup\$ Kelly Thomas, I use .Net 4.0. I can't use a newer version. I did as Shroeder told me and I reordered my operations from previous to float xDiff = (graphics.PreferredBackBufferWidth / 2) - mouseState.X; float yDiff = (graphics.PreferredBackBufferHeight / 2) - mouseState.Y; And now it works, just instead of 0 now I get 180 degrees. Degrees work vertically, but not horizontally. \$\endgroup\$
    – user54580
    Nov 4, 2014 at 0:35

You must log in to answer this question.

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