2
\$\begingroup\$

My game is supposed to have an object that changes it sprite depending on which way the mouse faces. programming this has become a challenge, because I want to calculate the angle relative to the center of the screen using the X axis.

To explain what I mean, pretend that under a sprite in the center of a screen is a protractor.

enter image description here

I want to measure the angle that's formed between the cursor and the center. If I use the X axis as a base, I should get an angle between 180° and -180°.

How would I achieve this? I've tried vector.angle(reference-vector), but that doesn't seem to give the desired result.

\$\endgroup\$
2
  • \$\begingroup\$ Can you try to describe what results with provided example are? According to documentation it really should be something along the lines you posted (my bet is on (reference-vector).angle(new Vector2(1.0, 0.0)/*x axis*/)). \$\endgroup\$
    – wondra
    Commented Jun 12, 2017 at 17:51
  • \$\begingroup\$ @wondra I put this method between system.out.println(), and I got what I thought at first to be a degree. However, as I examined closely, the value would change if I moved the cursor from right to left along the negative X axis, something it should only do on the positive X axis. I guess what I mean is that the value should have stayed 0, but decremented one time for every pixel the mouse moved. \$\endgroup\$
    – Plug
    Commented Jun 12, 2017 at 21:54

2 Answers 2

2
\$\begingroup\$

You can get the angle in radians using the Math.atan2 method shown below:

double angleRadians = Math.atan2(mousey-spriteCentery,mousex-spriteCenterx);

You can then convert this using MathUtils

float angleDegrees = angleRadians * MathUtils.radiansToDegrees;

Edit: Updated to reflect suggestion by DMGregory for correct axis direction.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ You probably want mouse - center, rather than center - mouse, unless one or both coordinate axes are reversed. If measuring the angle from the positive x axis, then the ys should be the first argument, and xs the second. \$\endgroup\$
    – DMGregory
    Commented Jun 13, 2017 at 13:03
0
\$\begingroup\$

I assume you're basically trying to make the sprite face a different diagonal based on which quadrant of the screen your mouse is in. Since you likely don't require the specific angle but rather simply which quadrant the mouse is in simply get the vector between the player and the mouse.

  • If both x and y are positive the mouse is in the upper right quadrant.
  • If x is negative, and y is positive the mouse is in the upper left.
  • If both are negative the mouse is in the bottom left.
  • if x is positive and y is negative it's in the bottom right.

Using the information on which quadrant the mouse is in you can just use that to update the state of your sprite.

\$\endgroup\$
4
  • \$\begingroup\$ While this will work with less sophisticated sprite sets (for example, one sprite for each diagonal direction, and one sprite for each cardinal direction), my sprite set has Twenty sprites, thus requiring something like what is described above to be done. \$\endgroup\$
    – Plug
    Commented Jun 12, 2017 at 21:47
  • \$\begingroup\$ If that is the case, what might the results of the vector.angle() function be giving you if not the correct results? \$\endgroup\$
    – Taitu
    Commented Jun 12, 2017 at 21:52
  • \$\begingroup\$ That's described in the comment I just made above. \$\endgroup\$
    – Plug
    Commented Jun 12, 2017 at 21:55
  • \$\begingroup\$ Looking more into the .angle() function, it seems like the function already compares the sent in vector to the value of the vector that called the function. In your question you mentioned that you are subtracting the player position from the mouse location and sending that into the function call. Perhaps try calling the function from the player and only sending in the mouse location vector. The exact implementation for the .angle() function can be found here: github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/… \$\endgroup\$
    – Taitu
    Commented Jun 12, 2017 at 22:15

You must log in to answer this question.

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