5

I would like to simulate an analog joystick on the touchable screen like this picture.

Final Fantasy 3 for Android

When the center of the stick area is set to (cx, cy), how should I calculate X-direction ratio and Y-direction ratio(-1.0~1.0) from the touched position(tx, ty)?

Locations image

Tank2005
  • 899
  • 1
  • 14
  • 32

1 Answers1

2

The x direction ratio is (tx-cx)/r, where r is the radius magnitude of the circle. Similarly, the y direction ratio is (ty-cy)/r.

I am assuming a right-handed cartesian coordinate with x axis in the direction of the right of the picture. Otherwise, you may need to multiply a (-1) to adjust the direction.

Navid Rezaei
  • 1,003
  • 11
  • 22
  • 1
    Note that this solution produces a square-gated joystick, where, to achieve the maximum X and Y values simultaneously, one must move their finger outside of the circle. – lazd Jun 13 '14 at 22:05