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

Offline Miridian

  • byte
  • *
  • Posts: 15
    • View Profile
    • My Blog :)
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  ???
please help  :)
« Last Edit: January 10, 2011, 09:26:42 pm by Miridian »

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
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. »

Offline Miridian

  • byte
  • *
  • Posts: 15
    • View Profile
    • My Blog :)
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.
Can you please help me to get a correct value from the joystick?
My object is rotating but in the wrong way and speed   :D

 

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
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...

Offline Miridian

  • byte
  • *
  • Posts: 15
    • View Profile
    • My Blog :)
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) {
player_mesh.rotateY((float) (Math.toRadians(1)) * ft);
} else {
player_mesh.rotateY((float) (Math.toRadians(1) * -1) * ft);
}
}

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

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
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?
PS: maybe something from this help you http://www.jpct.net/wiki/index.php/Main_Page
« Last Edit: January 10, 2011, 10:40:11 pm by Thomas. »