Augmented Reality sample code using Gyroscope

Charlie Seligman picture Charlie Seligman · Mar 6, 2011 · Viewed 8.4k times · Source

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

Answer

100grams picture 100grams · Mar 14, 2011

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.