Converting PyGame 2 axis joystick float to 360 angle

2 replies · 2K views · last reply
archieab
Messages
2
Reaction score
0
I would like to convert my joystick's two axis float (horizontal and vertical) to a 360 angle that can be used to set a players direction in my game. From my research, I've found the best route to take is using Atan2.

Variables:
Code:
self.rot = 0
horaxis = joy1.get_axis(0)
veraxis = joy1.get_axis(1)

Demonstration Diagram: you are correct, it is drawn in MS Paint
Diagram of a circular dial with a green triangle pointing up, a red dot at the centre, and a blue triangle at lower right; coloured text labels say the green position sets self.rot to 0, the red position keeps rotation unchanged, and the blue position sets self.rot to 135.


Absolutely any suggestions/snippets for the code would be amazing!
 
Attachments
  • Diagram of a circular dial with a green triangle pointing up, a red dot at the centre, and a blue triangle at lower right; coloured text labels say the green position sets self.rot to 0, the red position keeps rotation unchanged, and the blue position sets self.rot to 135.
    Demo.png
    5.8 KB · Views: 580
archieab said:
From my research, I've found the best route to take is using Atan2.
So what is unclear? The atan2 function gets the x and y displacement as input, you have both.
The center could be checked with an additional if condition.
 
mfb said:
So what is unclear? The atan2 function gets the x and y displacement as input, you have both.
The center could be checked with an additional if condition.
Managed to solve the issue doing some extra research. Thanks for the reply.