Touch Screen Joystick Algorithm

I would like to simulate an analog joystick on a touch screen like this picture.

Final Fantasy 3 for Android

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

Locations image

+5
source share
1 answer

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

I take the right Cartesian coordinate with the x axis in the direction to the right of the image. Otherwise, you may need to multiply (-1) to adjust the direction.

+1
source

All Articles