指向倾斜方向 - iPhone

发布于 2024-10-19 12:18:48 字数 212 浏览 2 评论 0原文

在我的 cocos2d 游戏中,我有我的玩家精灵,我想让他沿着我倾斜 iPhone 的方向移动。我可以处理这个问题,我无法解决的最难的部分是:

如何使我的精灵旋转以指向我倾斜的方向?这一点在应用商店的“Tilt to Live”游戏中得到了很好的体现。我想要这样的控制。

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

谢谢。

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:

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.

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

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

上课铃就是安魂曲 2024-10-26 12:18:48

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

将 iPad 放在您面前,让 LR 向左/向右倾斜,TA 向您/远离您倾斜。所以 LR 从 -90 到 90,TA 从 -90 到 90。(TA 负值倾向于你的腹部。)

在屏幕上显示这两个数字,然后移动设备,这样你就可以确定你有权利开始和。在它起作用之前你将无法做任何事情。

解决方案角度将像时钟指针一样,顺时针旋转,距离你的距离为12。

遍历这个决策链:

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

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

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

否则:

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

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

if ( LR > 0 ) FinalResult = 90 - adjustmentAngle

if ( LR < 0 ), FinalResult = 270 + adjustmentAngle

我想这样就可以了!希望有帮助!

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

设置角度...

“目前我唯一不确定的事情(关于你自己的想法)是如何将其应用到我的播放器?我是否仅仅使播放器旋转值等于 adjustmentAngle?” .. 嗨,乔什,是的,只需将旋转设置为您使用上面计算的最终角度即可!幸运的是,事情就是这么简单。

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

#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,这是您可能想要添加书签的非常方便的图表:

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

从加速度计转换为角度...

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

非常正确,抱歉我忘了提及。这是一个日常问题...

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) )

您必须通过在屏幕上写下三个角度然后移动它来测试它,换句话说,在继续之前,对您编写的部分进行“物理单元测试”!

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

顺便说一句,正如您可能看到的那样,您可以获得粗略的结果通过在“调整角度”表达式中简单地采用原始 x 与原始 y 值的比率,而不是两个正弦的比率……但无论如何,现在不要担心这个问题。

最后!

重要读者应该注意到,令人惊叹的新Core Motion系统可以根据您的需求为您处理很多事情。一探究竟!!!!!

希望有帮助!

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

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.

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

Go through this decision chain:

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

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

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

Otherwise:

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

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

if ( LR > 0 ) finalResult = 90 - adjustmentAngle

if ( LR < 0 ), finalResult = 270 + adjustmentAngle

I think that will do it! Hope it helps!

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

.

setting the angle...

"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;}

.

where are the axes?...

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

.

converting from accelerometer to angles...

"the accelerometer doesn't provide the raw data in angles. How do get from the raw data"

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!

here is one of many answers from SO: UIAccelerationValue angle

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.

And finally!

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!!!!!

Hope it helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文