Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Augmented Reality sample code using Gyroscope

Morning,

I have hunted around StackOverFlow for about an hour and found lots of sample code (mainly github) for creating Augmented Reality apps that display where a 2nd location is relative to your current location (e.g. New York).

However, I noticed that none of these are using the Gyroscope functionality provided in the iPhone 4 that gives a far smoother experience to the end users.

Does anyone know if such an example of sample code exists?

Cheers,

Charlie

like image 932
Charlie Seligman Avatar asked Mar 06 '11 10:03

Charlie Seligman


2 Answers

You can definitely use CoreMotion to get data from the gyro. The basic approach would be to get CMAttitude.rotationMatrix and multiply its inverse (transpose) by a reference matrix which you initially set. The Teapot sample project on developer.apple.com shows the basic approach of working with CoreMotion.

For a true augmented reality app you will need to create a model using OpenGL ES. I personally found v1.1 to be more reliable on iOS, after having tried GL ES 2.0. The Teapot sample also uses GLES 1.1.

Using the gyro is much more accurate and "smooth" than using the Magneotmeter for getting the device's rotation around its reference axis. The trick is how to initially calibrate the reference matrix in order to get the true "heading" of the device and to place your GL ES model objects in the correct position around the camera. After you have achieved that you can rotate your model in 3D by multiplying of GL's viewMatrix with the inverse of the CMAttitude.rotationMatrix.

Lastly, if you intend to support iPhone 3Gs as well then don't forget to check gyroAvailable property of CMMotionManager and provide an alternative implementation using the magnetometer.

like image 55
100grams Avatar answered Oct 12 '22 23:10

100grams


You can try using CMMotionManager instance methods

startDeviceMotionUpdatesToQueue:withHandler: or startGyroUpdatesToQueue:withHandler:

 [CMMotionManagerObject startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^ (CMDeviceMotion *devMotion, NSError *error)
         {   
             CMAttitude *currentAttitude = devMotion.attitude;
             xRotation = currentAttitude.roll*180/M_PI;
             yRotation = currentAttitude.pitch*180/M_PI;
             zRotation = currentAttitude.yaw*180/M_PI;
         }];

If you use startGyroUpdatesToQueue:withHandler: you can get the result through the property gyroData

like image 29
visakh7 Avatar answered Oct 12 '22 23:10

visakh7