user2391236
user2391236

Reputation:

Responding to tilt of iPhone in Sprite Kit

I have been building a Sprite Kit game for quite some time now. Just recently I have been adding gyro/tilt functionality. Using the CMMotionManager, I've been able to access the numbers surprisingly easily. However, my problem arises as a result of how the acceleration.x values are stored.

You see, the way my game works, when the game starts, the phone quickly calibrates itself to how it's currently being held, and then I respond to changes in the acceleration.x value (holding your phone in landscape orientation, this is equivalent to tilting your screen towards and away from you.) However, laying your phone flat is 1.0 and tilting it straight towards you is 0.0, and then it loops back through those values if you go beyond that. So, if someone is sitting upright and their phone is calibrated at .1, and they tilt their phone .2 downwards, the results will not be what is expected.

Is there any easy way to counteract this?

Upvotes: 1

Views: 518

Answers (1)

Fogmeister
Fogmeister

Reputation: 77631

Why are you trying to make your own system for this? You shouldn't really be using the accelerometer values directly.

There is a class called CMAttitude that contains all the information about the orientation of the device.

This orientation is not taken raw from accelerometer data but uses a combination of the accelerometers, gyroscopes and magnetometer to calculate the current attitude of the device.

From this you can then take the roll, pitch and yaw values and use those instead of having to calculate them yourself.

Class documentation for CMAttitude.

Upvotes: 2

Related Questions