加速度计检测和精灵旋转 [英] Accelerometer detection and sprite rotation

查看:99
本文介绍了加速度计检测和精灵旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用加速度计移动播放器精灵.

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.

例如如果播放器将设备倾斜很多,则精灵应旋转90度而不是旋转45度,以便在一个方向上快速倾斜.

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.

如何正确执行此操作?

推荐答案

对于加速度计,仅检测机会较差.您可能会看这个问题.如果您不需要依靠较旧的iPhone版本(< 4)或iPad,则应改用陀螺仪.以核心运动API并从WWDC 2010的茶壶示例开始-您可以找到它

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

这篇关于加速度计检测和精灵旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆