### Author Topic: Need some help calculating angles of an onscreen joystick  (Read 2334 times)

#### Miridian

• byte
• Posts: 15
##### Need some help calculating angles of an onscreen joystick
« on: January 10, 2011, 09:18:37 pm »
Hi, I have started writing my first java project a few days ago.
It is a small game where I am using an onscreen joystick to turn the player.
I already have the joystick and the player but I cant find out how to turn the player by the angle of the joystick.

Code:

Code: [Select]
//get the angle
//initx,inity is the start point of the joystick
//touchingPoint is the point where the finger is touching the screen.
angle = Math.atan2(touchingPoint.y - inity,touchingPoint.x - initx);

//rotate the player
player_mesh.rotateY((float) (jpctaetest.controls.getAngle()/10)* ft);

I think it am using the wrong way for the angle because my player mesh is rotating slow when the joystick is on the right side and very fast when it is on the left
« Last Edit: January 10, 2011, 09:26:42 pm by Miridian »

#### Thomas.

• double
• Posts: 833
##### Re: Need some help calculating angles of an onscreen joystick
« Reply #1 on: January 10, 2011, 09:51:06 pm »
I used also virtual joystick in my game... I rotate SimpleVector and camera by joystick and move camera by this SimpleVector...
cam.rotateCameraAxis(cam.getYAxis(), -rotateX);
camDir.rotateY(-rotateX);

But it is very alpha version without textures, ...
« Last Edit: January 10, 2011, 09:55:04 pm by Thomas. »

#### Miridian

• byte
• Posts: 15
##### Re: Need some help calculating angles of an onscreen joystick
« Reply #2 on: January 10, 2011, 10:08:29 pm »
Yes it is the same what i am trying to get for my game.
My object is rotating but in the wrong way and speed

#### Thomas.

• double
• Posts: 833
##### Re: Need some help calculating angles of an onscreen joystick
« Reply #3 on: January 10, 2011, 10:17:11 pm »
You need some intelligent value from joystick, for example -128 to 128 from X and Y axises, where 0 is center...

#### Miridian

• byte
• Posts: 15
##### Re: Need some help calculating angles of an onscreen joystick
« Reply #4 on: January 10, 2011, 10:29:16 pm »
I think I have found a dirty solution for my problem

Code: [Select]
float ft = (float) ticks;
double test = jpctaetest.controls.getAngle();
if (!(Math.toDegrees(test) == 0)) {
if (Math.toDegrees(test) < -90 || Math.toDegrees(test) > 90) {
} else {
player_mesh.rotateY((float) (Math.toRadians(1) * -1) * ft);
}
}

It works but if someone has a better solution I would be very happy

#### Thomas.

• double
• Posts: 833
##### Re: Need some help calculating angles of an onscreen joystick
« Reply #5 on: January 10, 2011, 10:33:28 pm »
you want to limit up/down angle?