Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS gyroscope to make virtual horizon

Tags:

ios

gyroscope

I'm looking to include a virtual horizon for my app using gyroscope, like this one

I found a lot of documentation on the gyro and i think i use yaw but i don't know how to calibrate with gravity.

Can someone help me please ??

thanks.

like image 230
doc Avatar asked May 06 '12 16:05

doc


1 Answers

I solved my problem with this:

- (void)updateImage: (NSNumber *)rotNum
{
    self.image.transform = CGAffineTransformMakeRotation([rotNum floatValue]);
}

// call the above function to rotate the image in reference to the horizon
[motionManager startDeviceMotionUpdatesToQueue: gyroQueue
                                   withHandler: ^(CMDeviceMotion *motion, NSError *error) {
                                                       [self performSelectorOnMainThread: @selector(updateImage:)
                                                                              withObject: [NSNumber numberWithDouble: atan2(motion.gravity.x, motion.gravity.y)-M_PI]
                                                                           waitUntilDone: NO];
                                               }];
like image 145
doc Avatar answered Sep 27 '22 20:09

doc