Joystick/Thumbstick to rotation?
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
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.
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.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement