Search code examples
pythongeometrylinepygame

How do I find the angle between 2 points in pygame?


I am writing a game in Python with Pygame.
The co-ords (of my display window) are
( 0 , 0 ) at the top left and
(640,480) at the bottom right.

The angle is
when pointing up,
90° when pointing to the right.

I have a player sprite with a centre position and I want the turret on a gun to point towards the player. How do I do it?
Say,
x1,y1 are the turret co-ords
x2,y2 are the player co-ords
a is the angle's measure


Solution

  • OK, using a combination of your answers and some other websites I have found the working code:

    dx,dy = x2-x1,y2-y1
    
    rads = math.atan2(dx/dy)
    degs = math.degrees(rads)
    

    The rest of my code isn't fussy about a negative value of degs; anyway, it works now and I'd like to say thanks for your help.