#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# How do i translate joystick values to degree of arc?

edited April 2014

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!

Tagged:

• W/ my shameful trig knowledge, most I coulda come up w/ was this 1: X_X

``````//forum.processing.org/two/discussion/4136/
//how-do-i-translate-joystick-values-to-degree-of-arc

float x = .88, y = .88;

float radX = map(x, -1, 1, -HALF_PI, HALF_PI);
float radY = map(y, -1, 1, -HALF_PI, HALF_PI);

println("x = " + x + ", y = " + y);

if (ang < 0)  ang += 360;

println("\nang = " + ang);

exit();
``````
• 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) and `compAngle` 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.

``````float x, y;
float myAngle, compAngle;
boolean useAngle;
float tolerance = 0;

float ox, oy;

void setup() {
size(400, 400);
ox = width/2;
oy = height/2;
textSize(16);
}

// Coovert mouse position to simulate legal controller values
// and calculate angle
public void mouseMoved() {
x = (mouseX - ox)/width;
y = -(mouseY -oy)/height;
// Simulate tolerance setting for slider input control
if (tolerance > 0) {
if (abs(x) <= tolerance) x = 0;
if (abs(y) <= tolerance) y = 0;
}
// Now calculate angles
calcMyAngle();
calcCompAngle();
}

public void calcMyAngle() {
useAngle = (x != 0 || y != 0);
myAngle = atan2(-y, x);
if (myAngle < 0)  myAngle += TWO_PI;
myAngle = (myAngle + HALF_PI) % TWO_PI;
}

public void calcCompAngle() {
useAngle = (x != 0 || y != 0);
compAngle = atan2(-y, x);
if (compAngle < 0)  compAngle += TWO_PI;
}

public void draw() {
background(255);
stroke(200, 200, 0);
line(ox, oy, mouseX, mouseY);
fill(0);
String xy = "XY = [" + x +", " + y +"]";
text(xy, 10, 20);
text("My angle   = " + degrees(myAngle), 10, 40);
text("Comp angle = " + degrees(compAngle), 10, 60);
}
``````
• 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.