Joystick/Thumbstick to rotation?

Started by
2 comments, last by Mark Tanner 20 years, 7 months ago
Hi, what I want to do is to convert a joystick or thumbstick into a 0-360 degree rotation value. For example, if the user presses straight up = 0 degrees, right = 90 degrees, left = 270 degrees, etc. The analog sticks return values are: -1 to 1 on x axis (left-right) and -1 to 1 on y (up - down, so it''s inverted which is common). and of course 0 if centered.. Can anyone provide some code? Many thanks! Mark
Advertisement
Its quite simple. Your rotation angle in radians can be taken directly using the atan2 function (in c/c++, and probably Java). Then just multiply by 180/PI to get degrees:

Normally, atan2 would be called as atan2(y, x). But since 90 degrees is to the right and y is flipped you have to do this:

rotation_angle = 180.0 * atan2(x, -y) / 3.14159265

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
You''re right, and it works! Many thanks,

Mark
Also of interest: converting cartesian coordinates to polar coordinates.

Also: remember to take account for the fact that a stick moved diagnoally doesn''t move further than a stick moved vertically or horizontally [depending on the API].

Also #2: bear in mind the difference between linear and logarithmic control inputs.

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

This topic is closed to new replies.

Advertisement