After many research and testing. I ended up using GLKit for calculation, as it saves me a lot of problems. Just leave it here for anyone who comes up with this question.
First, I started updating the movements of the CMMotionManager device 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 Internet to draw an openGL world using CoreMotion rotation and mix it to get a point from the screen to the 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;
source share