指向倾斜方向 - iPhone [英] Point in Tilt Direction - iPhone

查看:21
本文介绍了指向倾斜方向 - iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 cocos2d 游戏中,我有我的玩家精灵,我想让他朝着我倾斜 iPhone 的方向移动.我可以解决这个问题,最难解决的是:

In my cocos2d game, I have my player sprite and I want to have him move in the direction I tilt my iPhone. I can deal with that, the hardest bit which I can't work out is:

如何让我的精灵旋转以指向我倾斜的方向?这在应用商店的Tilt to Live"游戏中得到了很好的体现.我想要这样的控件.

How do I make my sprite rotate to point in the direction I am tilting? This is represented very well in the 'Tilt to Live' game on the app store. I want controls just like that.

如果有帮助的话,我的精灵(对于那些不熟悉 cocos2d 的人)确实有一个旋转值.

My sprite(for those unfamiliar with cocos2d) does have a rotation value if that helps.

谢谢.

推荐答案

如果你不喜欢上面的,这里有一个更简单的方法来获得合理的结果!

If you don't like the above, here's a simpler way to get a reasonable result!

将 iPad 放在您面前,让 LR 向左/向右倾斜,TA 向您/远离您倾斜.所以LR从-90跑到90,TA从-90跑到90.(TA负向你的肚子.)

Hold the iPad in front of you, let LR be the left / right tilt, TA the towards you / away from you tilt. So LR runs from -90 to 90, TA from -90 to 90. (TA negative is leaning towards your belly.)

在您的屏幕上显示这两个数字,然后四处移动设备,这样您就可以确定自己有权开始.在它起作用之前,您将无法做任何事情.

Display both those numbers on your screen, and move the device around, so you are certain you have that right to begin with. You won't be able to do anything until that is working.

solutionAngle 会像一个时钟指针,顺时针方向,距离你 12.

The solutionAngle will be like a clock hand, clockwise, with 12 distant from you.

通过这个决策链:

如果 LR 和 TA 都为零,则机器是平的.采取适当的行动.

If both LR and TA is zero, the machine is flat. Act appropriately.

如果 LR 是平坦的 (0),则答案是 0 或 180,具体取决于 TA 的符号.

If LR is flat (0), the answer is either 0 or 180, depending on the sign of TA.

如果 TA 是平坦的 (0),则答案是 90 或 270,具体取决于 LR 的符号.

If TA is flat (0), the answer is either 90 or 270, depending on the sign of LR.

否则:

adjustmentAngle = arctan( sin(TA)/sin(LR) )

adjustmentAngle = arctan( sin(TA) / sin(LR) )

//(注意,应该从 -90 到 +90)

// (NB, that should run from -90 to +90)

if ( LR > 0 ) finalResult = 90 - 调整角度

if ( LR > 0 ) finalResult = 90 - adjustmentAngle

if ( LR <0 ), finalResult = 270 + 调整角度

我认为可以做到!希望有帮助!

I think that will do it! Hope it helps!

IMO ...... 一定要随着时间的推移使结果平滑,以获得良好的感觉.

IMO...... be sure to smooth the result over time, for a good feel.

.

"我目前唯一不确定的(关于你自己的想法)是我如何将它应用到我的播放器上?我只是让玩家旋转值等于调整角度吗? .. 嗨乔希,是的,只需将旋转设置为您使用上述计算得出的最终角度!幸好就是这么简单.

"the only thing I am unsure of currently (concerning your own idea) is how do I apply it to my player? Do I merely make the player rotation value equal to the adjustmentAngle?" .. hi Josh, yes simply set the rotation to the final angle you calculate using the above! Fortunately it's that simple.

如果您必须在度数和弧度之间转换后/前,只需粘贴每个人都使用的这些代码行:

If you ever have to convert back/fore between degrees and radians, just paste in these lines of code that everyone uses:

#include <math.h>
static inline float degreestoradians (double degrees) {return degrees * M_PI/180;}
static inline float radianstodegrees (double degrees) {return degrees * 180/M_PI;}

.

PS,这是您可能想要添加书签的非常方便的图表:

PS, here's the incredibly handy diagram you may want to bookmark:

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAcceleration_Class/Reference/UIAcceleration.html

.

加速度计不提供角度的原始数据.如何从原始数据中获取

很对,我忘了说抱歉.这是一个日常问题...

Quite right, I forgot to mention it sorry. This is an everyday problem...

distanceFactor = square root of (x^2 + y^2 + z^2)
angle X axis = acos ( x / distanceFactor )
angle y axis = acos ( y / distanceFactor )
angle z axis = acos ( z / distanceFactor) )

您必须通过在屏幕上写下三个角度然后移动它来进行测试,即物理单元测试".在你继续之前你写的那部分!

You must TEST this by writing the three angles on the screen and then moving it around, in other words "physically unit test" that section you write, before you proceed!

这里是 SO 的众多答案之一:UIAccelerationValue angle

here is one of many answers from SO: UIAccelerationValue angle

顺便说一句,正如您可能看到的,在adjustmentAngle"表达式中,您可以通过简单地取原始 x 与原始 y 值的比率而不是两个正弦的比率来获得粗略的结果......但无论如何暂时不用担心.

BTW as you can probably see, you can get a rough result by taking the ratio of simply the raw x by raw y value, rather than the ratio of the two sines, in the 'adjustmentAngle' expression ... but anyway don't worry about that for now.

重要 读者应该注意,令人惊叹的全新 Core Motion 系统可以根据您的需要为您处理很多此类问题.看看!!!!!!

IMPORTANT Readers should note that the amazing new Core Motion system, handles a lot of this for you, depending on your needs. Check it out!!!!!

希望能帮到你!

这篇关于指向倾斜方向 - iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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