I want to add 3D objects from cocos3d as camera overlay for augmented reality in my iPhone app. I've got CC3Scene with some model. I've create motionManager capturing to get information about device camera's direction, and then in -(void)update:(ccTime)dt I'm trying to update cocos3d in-scene camera's position (up direction of the scene is the Z axis):
-(void) update:(ccTime)dt
{
CMDeviceMotion *currentDeviceMotion = motionManager.deviceMotion;
CMAttitude *currentAttitude = currentDeviceMotion.attitude;
CC3Camera *cam = (CC3Camera*)[cc3Scene getNodeNamed:@"Camera"];
static double lastY = 0;
static double lastP = 0;
static double lastR = 0;
double yaw = CC_RADIANS_TO_DEGREES(currentAttitude.yaw-lastY);
double pitch = CC_RADIANS_TO_DEGREES(currentAttitude.pitch-lastP);
double roll = CC_RADIANS_TO_DEGREES(currentAttitude.roll-lastR);
CC3Vector fd = cc3v(0,1,0);
[cam rotateByAngle:roll aroundAxis:fd];
CC3Vector rp = cc3v(1,0,0);
[cam rotateByAngle:pitch aroundAxis:rp];
CC3Vector up = cc3v(0,0,1);
[cam rotateByAngle:yaw aroundAxis:up];
lastY = currentAttitude.yaw;
lastP = currentAttitude.pitch;
lastR = currentAttitude.roll;
[super update:dt];
}
I could not directly use cam.rotation = cc3v(pitch, roll, yaw) since the order of rotation in cocos (with my current axes) does not match the one in the device. But my code is not working correct neither. There is a error while rotating camera manually, and I dont know how should I rotate it to make it correct (i've tried almost all combinations of order and axes).
So the question is how can I match cocos's camera rotation and device's one? Am I missing something? Or could you provide code or tutorial projects with such subjects? I understand that is it rather simple question to solve since there is only trigonometry, but I've blown my mind trying to do it.
Thanks!