Corelocation Heading Base on Back Camera (Augmented Reality)

CoreLocation heading base on back camera (Augmented reality)

After much of research and testing. I ended up using GLKit for the calculation, as it saves me lots of trouble as well. Just leave it here for anyone who happen to get to this question.

First, I started the CMMotionManager device motion updates with CMAttitudeReferenceFrameXTrueNorthZVertical.

self.hasMotion = NO;
CMMotionManager *cmmotionManager = [[CMMotionManager alloc] init];
[cmmotionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXTrueNorthZVertical
toQueue:[[NSOperationQueue alloc] init]
withHandler:^ (CMDeviceMotion *motion, NSError *error) {
self.hasMotion = YES;

}];
self.motionManager = cmmotionManager;

From some codes that I found on the web to draw an openGL world using CoreMotion rotation and mix it with getting a point from screen to 3D world:

float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(45.0f), aspect, 0.1f, 100.0f);

CMRotationMatrix r = self.motionManager.deviceMotion.attitude.rotationMatrix;
GLKMatrix4 camFromIMU = GLKMatrix4Make(r.m11, r.m12, r.m13, 0,
r.m21, r.m22, r.m23, 0,
r.m31, r.m32, r.m33, 0,
0, 0, 0, 1);

GLKMatrix4 viewFromCam = GLKMatrix4Translate(GLKMatrix4Identity, 0, 0, 0);
GLKMatrix4 imuFromModel = GLKMatrix4Identity;
GLKMatrix4 viewModel = GLKMatrix4Multiply(imuFromModel, GLKMatrix4Multiply(camFromIMU, viewFromCam));
bool isInvertible;
GLKMatrix4 modelView = GLKMatrix4Invert(viewModel, &isInvertible);

int viewport[4];
viewport[0] = 0.0f;
viewport[1] = 0.0f;
viewport[2] = self.view.frame.size.width;
viewport[3] = self.view.frame.size.height;

bool success;
//assume center of the view
GLKVector3 vector3 = GLKVector3Make(self.view.frame.size.width/2, self.view.frame.size.height/2, 1.0);
GLKVector3 calculatedPoint = GLKMathUnproject(vector3, modelView, projectionMatrix, viewport, &success);
if(success)
{
//CMAttitudeReferenceFrameXTrueNorthZVertical always point x to true north
//with that, -y become east in 3D world
float angleInRadian = atan2f(-calculatedPoint.y, calculatedPoint.x);
return angleInRadian;
}

How to get phone heading for augmented reality (iOS)

Ok so i solved my problem, I stopped using locationManager, and I did something inspired of CoreLocation heading base on back camera (Augmented reality). It handle tilt and everything i needed for my AR application, but take a bit more of CPU capacities. (Nothing too big!)
So thank you user2629068 !

How to get phone heading for augmented reality?

Both your code and the solution you found are wrong if by "phone facing" you means the opposite direction of the device z-coordinate. For augmented reality, you just call

remapCoordinateSystem(inR, AXIS_X, AXIS_Z, outR);

independent of the device orientation. Then the azimuth in the call to getOrientation() gives the direction of the phone facing with respect to Magnetic North.

These 2 calls amount to projection of the device z-coordinate to the XY plane of the world coordinates and then calculate the direction of the resulting vector.

Augmented Reality Indoors

The CoreMotion framework uses a gyroscope to deliver device motion updates, and gyroscopes drift over time. Additionally, when you tell the framework to use CMAttitudeReferenceFrameXTrueNorthZVertical, you're saying that you want to use the magnetometer to occasionally update the gyroscope's reference frame to keep it synced with a real-world reference frame. The compass on the device can suffer from interference, and is only occasionally used to correct for the drift.

Unfortunately the only thing can do is wait and hope that the framework's internals are magically improved by Apple.

Will it be called augmented reality if I render a reality-based 3D model instead of render a camera view?

In the academic papers applications like this are known as "situated simulations", see the work of Gunnar Liestøl: http://inventioproject.no/sitsim/



Related Topics



Leave a reply



Submit