Steven Rogers
Steven Rogers

Reputation: 2004

How do i get the angle from a joystick?

I am using LWJGL's controller class to connect my controller and play around. I wanted to make a game where the player will face the direction the joystick is pointed and they press 'A' to fire. To rotate the player sprite i need to know the angle. I know how to get the input and the joystick inputs are in two floats, X and Y locations from -1.0 to 1.0. How would i be able to use these two numbers to find the angle that the joystick is pointing?

Upvotes: 2

Views: 3502

Answers (1)

Mike Dunlavey
Mike Dunlavey

Reputation: 40669

Use the arc tangent function. Something like Math.Atan2(Y, X), that gives you an angle in radians. Then you multiply by 57.... to get degrees. Don't call it if both X and Y are zero.

Upvotes: 8

Related Questions