[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] [Pygame] Joystick Inputs



Inverse Tangent might be slightly easier since you don't need the hypotenuse in the calculation.  I've not used it before so if anyone has a slicker way of doing it, please chime in.

#x_axis is the position of the horizontal joystick
#y_axis is the position of the vertical joystick

angle_radians = math.atan2(y_axis,x_axis)
angle_degrees = math.degrees(math.atan2(y_axis,x_axis))

Either way, the output will be from -180 degrees to +180 degrees (or the radian equivalents) rather than 0 to 360, so you'll potentially have to watch out for that in your logic.

See the documentation at http://docs.python.org/library/math.html for why atan2(y,x) is probably more useful than just atan(x).


-Lee-

On Tue, Dec 6, 2011 at 10:44 PM, Andrew Godfroy <killerrin@xxxxxxxxxxx> wrote:
Hey Lee,

That was actually alot more helpful than what I have been able to find over my past days of searching! Thank you very much. I may actually be able to move forward with my ambitious school project now Smile
 
Now all I have to figure out is how to find what angle that the Thumbstick is pointing so that I can rotate my player sprite according to which way that the player is moving. I talked to my math teacher about it, and she agreed that I should try using the Inverse of Sin to find the angle. Hopefully that works, because if it doesn’t; I'll be back at square one again.
 
... But if anyone has had any experience with getting the angle for a rotation based off of a Joystick Thumbstick, or Mouse Position, it would be awesome if you could share how you found it out so I could use it as a reference.
 
From: Lee Buckingham
Sent: Tuesday, December 06, 2011 12:53 AM
Subject: Re: [pygame] [Pygame] Joystick Inputs
 
Hi Andrew,

For buttons I use this:

if event.type == pygame.JOYBUTTONDOWN:
    if event.button == "whatever...":
        etc....

if event.type == pygame.JOYBUTTONUP:
    and so on...


For analog control and d-pads you have to check for axis position each time through the game loop.  I do it all at once before the player objects update themselves

Like this:

horizontal_axis = self.joysticks[player_number].get_axis(0)
vertical_axis = self.joysticks[player_number].get_axis(1)

Then change the x and y speed for the player with "player_number" to whatever the axis positions are (times a constant, if necessary).  Of course, the player_number has to correspond to the joystick collection indices pretty well for this to work.  You can eliminate that part of it by replacing "player_number" with "0" and just make sure to check that the joystick count is >0 before you call .get_axis

Hope this helps a little!

-Lee-

On Thu, Dec 1, 2011 at 5:51 PM, Andrew Godfroy <killerrin@xxxxxxxxxxx> wrote:
Alright, I just checked over my question and I noticed I wasn’t exactly clear what I was looking for...

What I am asking is how do I work the event handler for checking joystick commands such as if the Left Thumbstick-[0] or [1]- moves, or if the A button-[0]- is pressed
Does anyone know how that works?