2

I want to write an app that gives the degrees of position from some coordinate (bottom of the phone).

For example... If I'm holding the phone at a 45 degree angle, I want to display: 45 degrees on the screen. If the user holds the phone at 45 degrees and rotates the phone around an axis going from the ear piece to the home button, I want to display that angle (between 0-180degrees).

I've implemented the accelerometer and I get the x, y, z values, however, how do I convert them? I know they are in G's (1G, 0.9G, -0.5G on the respective axis), but what's the conversion? Am I even going on the correct track? Should I be using the gyroscope instead?

Thanks.

El Guapo
  • 5,581
  • 7
  • 54
  • 82

2 Answers2

3

This question has an example. You can use atan2(y, x) and convert from radians to degrees with * (180/M_PI).

For any real arguments x and y not both equal to zero, atan2(y, x) is the angle in radians between the positive x-axis of a plane and the point given by the coordinates (x, y) on it.

- Wikipedia article on atan2

Community
  • 1
  • 1
0

If you can rely on gyroscope support I'd recommend to use it, because you can get the (Euler) angles directly without any calculations. See iOS - gyroscope sample and follow the links inside.

Don't use UIAccelerometer because it will be deprecated soon. The newer CoreMotion framework is always the better choice, even for old devices.

Community
  • 1
  • 1
Kay
  • 12,918
  • 4
  • 55
  • 77