We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everyone!
I guess this might be sort of a math question, but hopefully some one knows how to solve this!
I'm using a ps3-controller and i want to translate the data i get from the analog sticks in to degree of arc (0° to 360° degrees). The data i am receiving now using the stick.getX() and stick.getY() command is this:
Stick resting: x 0.0 y 0.0
Stick pointing up: x 0.0, y 1.0
Stick pointing down: x 0.0, y -1.0
Stick pointing right: x 1.0, y 0.0
Stick pointing left: x -1.0, y -0.0
Stick pointing right and up: x 0.88..., y 0.88...
... and everything in between.
I would be really happy and thankful if someone could help me to translate this data in to numbers between 1 and 360. Ideally the different stick directions would result in this data:
Stick resting: 0
Stick pointing up: 360
Stick pointing down: 180
Stick pointing right: 90
Stick pointing left: 270
Stick pointing right and up: 45
Anyone got an idea?
All the best!
Answers
W/ my shameful trig knowledge, most I coulda come up w/ was this 1: X_X
You don't say what you plan to do with the angles obtained but I would like to make some points.
Outside of computing and maths angles are measured in degrees starting at 0 degrees (north) and increasing clockwise to 360 degrees.
In maths angles are measured in radians starting at 0 radians (east) and increasing anti-clockwise to 2 Pi radians.
In computer graphics the y axis is inverted (so y increases down the screen) so computer angles are measured in radians starting at 0 radians (east) and increasing clockwise to 2 Pi radians.
If you are going to use angles in your sketches then it would be worthwhile getting used to how the computer does it.
In the following sketch the mouse position is mapped to simulate the x,y values expected from your controller and as the mouse moves it calculates
myAngle
(the one you asked for) andcompAngle
using the computer system for angles.The other problem is that you need to know when the stick is at rest. When x and y is zero then the angle is also 0. You will almost certainly need to differentiate between the stick at rest and the stick at the zero degree position. So in this sketch the useAngle variable is false when the stick is at rest, otherwise it would be true.
One final point, with most analog sticks (e.g. on XBOX, PS3 controllers) it is almost impossible for both x and y to be zero even when the stick is at rest. If you are using the ProControll library you might set the slider tolerance to 0.1 this will mean any value >=-0.1 and <=0.1 will be reported as 0. You can simulate this in the program by change the value in tolerance.
Thank you guys very very much! I will try this code out.
quark: I am using the left analog stick to move a space ship in a twin stick-shooter. But i also want to make the spaceship face the direction of with it's heading. This is why i want to translate the x and y-values of the analog stick in to degree of arc. When i push the stick to the left i want the ship both going left and to face left, and vice versa, when i push the stick to the right, the ship goes to the right facing right.
Yes i am using the proControlls library! Yeah i noticed that my ship was floating around a bit when the stick was resting. I think i did set the tolerance to 0.3.