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
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.
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
I don't know of any code sample, unfortunately.
A problem common to all AR apps is that you need to find out the orientation of your device. You can do that with atan2 and the accelerometer, but it has an unholy amount of noise (as seen in Apple's AccelerometerGraph sample project). If you try to fix it with an adaptive low pass filter you reduce the noise but you also make it less responsive.
The gyro doesn't have noise but error accumulates fast enough that you have to constantly reset the position using the accelerometer. It seems good to rotate an object, but not to replace the compass.
精彩评论