Accelerometer detection and sprite rotation

2019-09-03 14:20发布

问题:

I would like to use the accelerometer to move my player sprite.

If the sprite is going straight and the player tilts a little to the left, the sprite should rotate a little bit to the left, and same for the right.

I also want to detect how much the player has tilted the device and turn the sprite accordingly.

e.g. If the player tilts the device a lot the sprite should rotate 90 degrees rather than 45 for a quick tilt in a direction.

How does one do this. Detect the device movement in any direction, and for a small movement, the sprite should rotate less and for a larger rotation the sprite should rotate more.

I have experimented a little and dont get the results. Some times it works for clockwise rotations to the up, right and down movements, but not for the left movements.

What is the math behind this. An example would be the way a device detects its orientation and rotates the screen.

How does one do this correctly?

回答1:

For accelerometer only detection chances are bad. You might look look at this question. If you don't need to rely on older iPhone versions (<4) or iPad, you should use the gyroscope instead. Take core motion API and start with teapot example from WWDC 2010 - you can find it here