加速计检测和精灵旋转
我想使用加速度计来移动我的玩家精灵。
如果精灵直线前进并且玩家向左倾斜一点,则精灵应该向左旋转一点,向右也是如此。
我还想检测玩家倾斜设备的程度并相应地转动精灵。
例如,如果玩家大幅倾斜设备,则精灵应旋转 90 度,而不是 45 度,以便在某个方向上快速倾斜。
一个人如何做到这一点。检测设备在任何方向上的移动,对于较小的移动,精灵应旋转得较少,对于较大的旋转,精灵应旋转较多。
我做了一些实验,但没有得到结果。有时它适用于顺时针旋转向上、向右和向下移动,但不适用于向左移动。
这背后的数学原理是什么?一个例子是设备检测其方向并旋转屏幕的方式。
如何正确地做到这一点?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于加速度计而言,仅检测机会很差。您可以看看这个问题。如果您不需要依赖较旧的 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