简体   繁体   中英

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

updates code:

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

Any ideas?

CMAttitude is the class you're looking for. When you're update block fires, you access the devices attitude though the attitude property on the CMDeviceMotion instance. 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.

[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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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