1

I'm working on a spritekit game using swift and I implemented a joystick using this library. I'm having a hard time trying to figure out how to calculate the degree of the rotation. the library gives you this information when you move the joystick around

joystick.trackingHandler = { jData in
  // something...
  // jData contains angular && velocity (jData.angular, jData.velocity)
}

I don't need to rotate the player since the game is a 4 directional jrpg style, so i'll just be triggering movement based on a range of degrees.

does anybody have any useful articles or information on turning the velocity returned into a degree?

I've seen people using atan2 but it seems to only accept Double's and the velody returned is of type CGPoint.

Shan Robertson
  • 2,742
  • 3
  • 25
  • 43

1 Answers1

2

The angular value in jData contains the angle in radians.

To convert, use the following code for values between -180 and +180:

let degrees = jData.angular * 360 / (2 * M_PI)

and this for values between 0 and +360:

var radians = jData.angular
radians = radians > 0 ? radians : (2 * M_PI + radians)
let degrees = radians * 360 / (2 * M_PI)

adapted from https://stackoverflow.com/a/1311134/968577

Community
  • 1
  • 1
Gary Makin
  • 3,109
  • 1
  • 19
  • 27