Converting PyGame 2 axis joystick float to 360 angle

AI Thread Summary
To convert joystick axis input into a 360-degree angle for player direction in a game, the Atan2 function is recommended. The joystick's horizontal and vertical axes are accessed using joy1.get_axis(0) and joy1.get_axis(1), respectively. The Atan2 function takes these x and y displacements as inputs to calculate the angle. An additional condition can be implemented to check for the center position of the joystick to enhance accuracy. The original poster successfully resolved their issue after further research.
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
Demo.png


Absolutely any suggestions/snippets for the code would be amazing!
 

Attachments

  • Demo.png
    Demo.png
    5.8 KB · Views: 511
Technology news on Phys.org
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top