简体   繁体   English

使用陀螺仪数据旋转SpriteKit精灵的z旋转

[英]Rotating a SpriteKit sprite’s z rotation with gyroscope data

I have the setup for core motion implemented, but can't figure out how to keep the rotation of my sprite so it follows the rotation of the iPhone (on the z axis). 我已经实现了用于核心运动的设置,但无法弄清楚如何保持我的精灵的旋转,使其跟随iPhone的旋转(在z轴上)。

updates code: 更新代码:

[self.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
                                            withHandler:^(CMDeviceMotion *motion, NSError *error)
{
    mySprite.zRotation = ?;
}];

Any ideas? 有任何想法吗?

CMAttitude is the class you're looking for. CMAttitude是您要寻找的课程。 When you're update block fires, you access the devices attitude though the attitude property on the CMDeviceMotion instance. 当您进行更新时,块触发将通过CMDeviceMotion实例上的态度属性来访问设备态度。 From attitude, you can directly ask the device for all kinds of interesting info about its rotation, but for your use case, you probably only need to ask for yaw. 从态度上讲,您可以直接向设备询问有关其旋转的各种有趣信息,但是对于您的用例,您可能只需要询问偏航即可。

And assuming of course that you don't need these values transformed in any way, you can pump them straight into the sprite's zRotation, and you should receive the expected results. 当然,假设您不需要以任何方式转换这些值,则可以将它们直接泵入精灵的zRotation中,并且应该会收到预期的结果。

[self.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
                                        withHandler:^(CMDeviceMotion *motion, NSError *error) {
                                            mySprite.zRotation = motion.attitude.yaw;
                                        }];

在此处输入图片说明 Image from http://uqtimes.blogspot.com/2012/05/3-coremotion.html 图片来自http://uqtimes.blogspot.com/2012/05/3-coremotion.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM