2

How can I determine the tilt location in the y direction on my iOS app? First I tried playing with the accelerometer. But the accelerometer values change even if the iPhone isn't tilting. Then I tried playing around with the gyroscope. But that only senses the rate of the tilt, not the position of the tilt.

Here is specifically what I am trying to do:

If you place an iPhone on a table in landscape position with the home button on the top. Then you slowly proceed to flip it in a manner such that the display is now facing down (you see the back of the phone) and the home button is still on the left. That tilt motion that is needed to do that is what I'm trying to measure.

CodeGuy
  • 28,427
  • 76
  • 200
  • 317

2 Answers2

2

The accelerometer is what you want. If your acceleration vector is a = (x, y, z), then the angles between this vector and the three axes are given by:

cos (angleXaxis) = x / sqrt(x^2 + y^2 + z^2)

cos (angleYaxis) = y / sqrt(x^2 + y^2 + z^2)

cos (angleZaxis) = z / sqrt(x^2 + y^2 + z^2)

To get the angles themselves you'll need to use the inverse cos function.

PengOne
  • 48,188
  • 17
  • 130
  • 149
  • All as that does is print out x, y, and z components. Surely, in my question I've seen x, y, and z values if I said accelerometer values change even if the iPhone isn't tilting. – CodeGuy Jul 01 '11 at 21:46
  • In my question I explained exactly which tilt I wanted. And yes, I'd imagine the angle would be useful. But that tutorial doesn't cover this. – CodeGuy Jul 01 '11 at 21:53
  • How so? I did the following: float distanceFactor = sqrt((xx*xx) + (yy*yy) + (zz*zz)); float ax = acos ( xx / distanceFactor ); float ay = acos ( yy / distanceFactor ); float az = acos ( zz / distanceFactor); where xx, yy, and zz are the accelerometer values. But when I tilt the phone in the way I said, both the ax and az angle change.... – CodeGuy Jul 01 '11 at 22:00
  • By how much? One should be changing by more than the others. You aren't doing a precise movement, so it makes sense that more than what you expect is changing. – PengOne Jul 01 '11 at 22:03
  • when I begin the tilt motion and continue until the phone is perpendicular to the table, x angle goes from 1.5 to 0 and z angle goes from 3 to 1.5 – CodeGuy Jul 01 '11 at 22:08
  • and what did you expect to happen instead? – PengOne Jul 01 '11 at 22:10
  • I am expecting to figure out the tilt....and I'm not. Because there are motions I can do with the phone that will change the x angle. There are motions I can do with the phone that will change the z angle. But I don't understand. How do I detect the tilt I specifically described in my question. Please answer that specifically. – CodeGuy Jul 01 '11 at 22:15
  • You have all of that information. Unless you tell me how, specifically, you would like the information outputting, I cannot help you further. Watch the output at the start, in the middle, and at the end of the rotation you are doing. I do not know what more you want. – PengOne Jul 01 '11 at 22:17
  • All as you did is give me three angles. Specifically, how do I know when the phone is being tilted in the manner I described...that's the question. if(...), then phone is being tilted in this manner. – CodeGuy Jul 01 '11 at 22:24
2

You should consider to use the Core Motion API because it provides the values ready to use as Euler angle, quaternion or rotation rate. Have a look at

Accelerometer detection and sprite rotation

iPhone - What does the gyroscope measures? Can I get an absolute degree measurement in all axis?

iphone - core motion (relative rotation)

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