Finding an angle from 3 points

I am making a game and I am trying to rotate an image so that it is always pointing at the player. I have two lines, the first point of both of them is on the image and the second point of one line is on the last position of the player, and the second point of the other one is on the current position of the player. To rotate the image I need to get the angle between the two lines. Does anyone know how I can get that angle with only the points from the lines?
Basic trigonometry and algebra.

I think you are doing it the hard way, though, by bothering with the previous-position line. Typically the image rotation is specified in radians from the original, unrotated position. Just calculate the current angle to the player and plug in the new angle.

Otherwise, just find the difference between the new angle and the old angle.


To find the angle, remember the arctangent function.
Given:

   ^ y
   |
   |   *
   |  /|
   | / |           a = atan2( y, x )
   |/a |         
---+----------> x
   |
   |

Keep in mind that you must make proper adjustments for each quadrant.

http://www.cplusplus.com/reference/cmath/atan2/

Also, don't forget to link with the C math library.

Good luck!
Keep in mind that you must make proper adjustments for each quadrant.


Isn't atan2 quadrant aware?
Oops! I forgot that. (I suppose I should have looked at the documentation I linked...)

I think it is because I typically prefer an angle in 0..2pi.
Is this 2D or 3D? The 2D is simple, as already shown. A more general solution does use vector math.
I had assumed 2D, since 3D stuff usually provides the functionality to do this...

@karma1111
2D or 3D?

What graphics framework are you using? (SDL, SFML, OpenGL, DirectX, etc?)
Last edited on
2D or 3D, the lengths of the sides of the triangle is all that is required.
A triangle is planar and the law of cosines would apply.
http://en.wikipedia.org/wiki/Law_of_cosines
Dioas' answer worked. Thanks for the help.
Topic archived. No new replies allowed.