-1

There's a player. While moving cursor Id like to get angle between player and cursor. My code for this is :

public void mouseMoved(MouseEvent ev) {
    float angle = (float) Math.toDegrees(Math.atan2(ev.getX() - player.getCenterX(),
            ev.getY() - player.getCenterY()));
    if (angle < 0) {
        angle += 360;
    }
    System.out.println(angle);
}

But it doesnt return correct values. How to modify it to return correct values?

user2102972
  • 251
  • 2
  • 6
  • 13
  • Presumably, you're talking about the angle between two vectors, right? –  Mar 09 '13 at 22:18
  • 3
    So where's your reference point? – cHao Mar 09 '13 at 22:19
  • One point is mouse cursor point and second is player point. From both I get coordinates – user2102972 Mar 09 '13 at 22:19
  • Then why are you using `atan2`? Just [take the `arccos` of the dot product divided by the product of the lengths.](http://en.wikipedia.org/wiki/Angle#Dot_product_and_generalisation) –  Mar 09 '13 at 22:20
  • From those two points, you get a line segment. It has a slope, but not an angle, because you need two intersecting lines/segments to have an angle between them. – cHao Mar 09 '13 at 22:21
  • I don't fully understand. Could some post code of it? – user2102972 Mar 09 '13 at 22:22

1 Answers1

1

This worked for me:

float angle = (float) Math.toDegrees(Math.atan2(-(ev.getX() - player.getCenterX()), ev.getY() - player.getCenterY()));
angle += 90;
Wolfii
  • 355
  • 3
  • 9