不容易啊,搞了好几天,几乎重看了一遍,3d数学基础,写的不对的一定得指出来哦, 原贴地址:
可以旋转,但角度(轴)还不对(x轴除外)
-(void)update:(ccTime)time { CCCamera *cam = [self camera]; if (nil == cam) { NSLog(@"Call Camera faild!"); return; } Document* doc = [Document sharedDocument]; kmVec3 center = {0, 0, -1}; kmVec3 eye = {0, 0, 0}; kmVec3 up = {0, 1, 0}; //kmVec3Subtract(¢er, &eye, ¢er); // 得到陀螺仪数据 float yaw = (float)(CC_RADIANS_TO_DEGREES(doc.motionManager.deviceMotion.attitude.yaw)); float pitch = (float)(CC_RADIANS_TO_DEGREES(doc.motionManager.deviceMotion.attitude.pitch)); float roll = (float)(CC_RADIANS_TO_DEGREES(doc.motionManager.deviceMotion.attitude.roll)); // 构建摄相机矩阵 kmMat4 camMat; kmMat4Identity(&camMat); // 构建旋转矩阵 kmQuaternion vec4; kmMat4 rotaMat; kmQuaternionRotationYawPitchRoll(&vec4, yaw, pitch, roll); kmMat4RotationQuaternion(&rotaMat, &vec4); // 构建平移矩阵 kmMat4 transMat; kmMat4Translation(&transMat, 0, 0, 100); // 矩阵变换 kmMat4Multiply(&camMat, &camMat, &rotaMat); kmMat4Multiply(&camMat, &camMat, &transMat); // 用最终的矩阵变换相应的点 kmVec3Transform(¢er, ¢er, &camMat); kmVec3Transform(&up, &up, &camMat); kmVec3Transform(&eye, &eye, &camMat); [cam setEyeX:eye.x eyeY:eye.y eyeZ:eye.z]; [cam setCenterX:center.x centerY:center.y centerZ:center.z]; [cam setUpX:up.x upY:up.y upZ:up.z];}