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

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

问题描述

在我的cocos2d游戏中,我有我的播放器精灵,我想让他沿倾斜我的iPhone的方向移动。我可以处理这个,我不能解决的最难的一点是:



如何使我的sprite旋转指向我倾斜的方向?这在应用商店的Tilt to Live游戏中表现得很好。我想要的控制就像这样。



我的sprite(对于不熟悉cocos2d的人)有一个旋转值,如果这有帮助。



感谢。

解决方案

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



将iPad保持在你的前面,让LR成为左/右倾斜,TA朝向你/远离你倾斜。所以LR从-90到90,TA从-90到90.(TA阴性倾向你的腹部。)



在屏幕上显示这些数字,移动设备,所以你确定你有权利开始。



解决方法会像顺时针方向的钟针,距离你12英里。



通过此决策链:



如果LR和TA都为零,则机器是平的。



如果LR是平的(0),答案是0或180,取决于TA的符号。



如果TA是平的(0),答案是90或270,取决于LR的符号。



否则:



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



// NB,应该从-90到+90)



if(LR> 0)finalResult = 90 - adjustmentAngle b
$ b

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



它!希望它帮助!



IMO ...... 一定要顺利过渡结果,以获得良好的感觉。





设置角度...



em>我目前不确定的唯一的事情(关于你自己的想法)是怎么应用它到我的播放器?我只是使播放器旋转值等于adjustAngle?在Josh,是简单地设置旋转到您计算使用上述的最终角度!幸运的是,这很简单。



如果你必须在度和弧度之间转换回来,只需粘贴这些代码,每个人都使用:

  #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 =(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



BTW,你可以看到,你可以得到一个粗略的结果, ,而不是两个正弦的比例,在'adjustAngle'表达式...但无论如何,现在不用担心。



最后! h1>

重要读者应该注意到,惊人的新 Core Motion 系统会为您处理很多这些,具体取决于您需要。看看!!!!!



希望它有帮助!


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.

解决方案

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!

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

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