How to identify phone tilt direction

Hi everyone, I'm trying to figure out how to identify if my phone is tilting forward (towards the ground) or backward (towards the ceiling) while in landscape orientation, using accelerometer or gyroscope. At first, I thought the z-axis will show that i.e. +z means phone is tilting forward and -z means phone is tilting backward. However, when I console log z values, there doesn't seem to be a direct relationship with +/- values and which way the phone is tilting. Does anyone know how to determine phone direction using accelerometer or gyroscope values? Or is there another way to go about this? Thank you.

Hello, you'll want to follow Apple's "Getting Processed Device-Motion Data" linked here: https://developer.apple.com/documentation/coremotion/getting_processed_device-motion_data


and pay special attention to this specific line in the above documentatation:

self.motion.startDeviceMotionUpdates(using: .xMagneticNorthZVertical)

The reference to .xMagneticNorthZVertical is one way that the iPhone can set its reference axis,

see the helpful picture linked here for how the iPhone uses x, y, and z planes of rotation: https://developer.apple.com/documentation/coremotion/getting_raw_gyroscope_events


As you ask above, the z-axis when using .xMagneticNorthZVertical woulld actually be if you had your iPhone laying flat on a table and started spinning it like the hands of a clock


What I think you may need in this case is the y value to sense the tilt (using .xMagneticNorthZVertical)


I recommend printing out x, y, and z values in the console log and test this out (if you haven't already) and see how the .xMagneticNorthZVertical reference axis affects the values that print in the console log-- it took me a while to figure out how the iPhone recognizes which way its oriented too! Hope this helps

How to identify phone tilt direction
 
 
Q