Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use CoreMotion for getting device orientation in space

I am having a thinking and searching problem here and can't find the good direction to look at... I am looking to develop an algorithm to move in a 360 image (sphere like) by using the device motion.

So if the user point the device in front of him he get the determined origin point of the image. As he moves the device around him the panoramic image moves according to it.

Any idea or source I can look into ?

Thanks and good luck to everyone with Swift :)

like image 796
TanguyAladenise Avatar asked Oct 21 '22 07:10

TanguyAladenise


1 Answers

Thanks to Kay I could be on the right track to achieved this effect.

I am making this answer just to provide more details for those looking for the same thing.

First you need to create a CMMotionManager object.

Then use startDeviceMotionUpdatesToQueue to handle motion events.

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

In processMotion you just need to get the attitude based on the previous one:

// Get attitude difference with previous one
CMAttitude *currentAttitude = self.motionManager.deviceMotion.attitude;
[currentAttitude multiplyByInverseOfAttitude:self.referenceAttitude];

Thanks to this you know the new angle made by the user since the last update. Then where you handle your view you convert the new Euler angle into the amount of pixels you need to move your image. Just be careful Euler angle varies between -180, 180 and are given in rad by Apple. This could be handy:

#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))

So in my case I just calculate the new x offset because I am just moving on the x axis.

Hope this helps.

like image 82
TanguyAladenise Avatar answered Oct 23 '22 01:10

TanguyAladenise