Reputation: 151
Im working on a project right now and i need to make a function that finds the vector direction for a bullet. My current code is off and i cant seem to find the reason why.
float AngleX = pMouse->X() - This->DirectionX();
float AngleY = pMouse->Y() - This->DirectionY();
Upvotes: 0
Views: 2201
Reputation: 339906
The best function for finding angles from (x, y) offsets is atan2(dy, dx)
, where dy
and dx
are the delta components in each direction.
Note that the result will be in radians, and that on some graphics systems the y
axis goes down instead of up!
The particularly nice feature of atan2
is that it'll always give you the result in the full range of -π .. π
which you can't get with a single acos
or asin
operation. The resulting angle will be the angle of the given line relative to the positive X axis, in an anti-clockwise direction.
Upvotes: 6