CodeGuy
CodeGuy

Reputation: 28905

Tilt location iOS app

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.

Upvotes: 2

Views: 2689

Answers (2)

Kay
Kay

Reputation: 13156

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)

Upvotes: 2

PengOne
PengOne

Reputation: 48398

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.

Upvotes: 2

Related Questions