2014-10-06 76 views
0

我想用平底锅旋转3d view中的对象。有2个旋转轴:对象Y轴和全局固定X轴。在y轴周围,我简单地使用方法CATransform3DRotate生成的旋转矩阵旋转,并围绕固定的x轴使用四元数。 在这里,我的一些代码至极演示行为:用四元数旋转对象

UITouch* touch = [touches anyObject]; 
CGPoint location = [touch locationInView: viewController.view]; 
CGPoint lastLoc = [touch previousLocationInView: viewController.view]; 
CGPoint diff  = CGPointMake(lastLoc.x - location.x, lastLoc.y - location.y); 

float rotX = 1 * DEGREES_TO_RADIANS(diff.y/4.0); 
float rotY = -1 * DEGREES_TO_RADIANS(diff.x/4.0); 

CATransform3D m = glView.modelTransform; 
if(fabs(diff.y) > fabs(diff.x) * 5.0) { //up-down 
    Vector3D xAxis = {1.0, 0.0, 0.0}; 
    Vector4D quaternion = [self makeQuaternionFromAxis: xAxis andAngle:rotX]; 
    Vector4D currentQuaterion = [self makeQuaternionFromRotationMatrix: m]; 
    quaternion = [self multiplyQuaternion:currentQuaterion buAnother:quaternion]; 
     m = [self makeRo 
    tationMatrixFromQuaternion:quaternion]; 
     m = CATransform3DConcat(m, origin); 
    } 
else {         //right-left 
    Vector3D yAxis = {0.0, 1.0, 0.0}; 
    m = CATransform3DRotate(m, rotY, yAxis.x, yAxis.y,yAxis.z); 
} 
glView.modelTransform = m; 

我单独的水平和垂直移动。原始变量是3D视图中对象的原始位置。问题是有时对象会获得偏航角度旋转,并且旋转会变得混乱,有时候对象本身也会变得混乱!当我混乱地在屏幕上制作平底锅时会出现这种情况。我试图规范运动,但这没有帮助我。有人能告诉我我做错了什么吗?

+0

已解决此问题。 – Demonoid67 2014-10-06 10:50:43

回答

0

固定码是这样的:

Vector4D quaternion; 
if(fabs(diff.y) > fabs(diff.x)) { //up-down 
    Vector3D xAxis = {1.0, 0.0, 0.0}; 
    quaternion = [self makeQuaternionFromAxis: xAxis andAngle:rotX]; 
    quaternion = [self multiplyQuaternion:quaternion buAnother:currentQuaterion]; 
    } 
else {         //right-left 
    Vector3D yAxis = {0.0, 1.0, 0.0}; 
    quaternion = [self makeQuaternionFromRotationMatrix:CATransform3DMakeRotation(rotY, yAxis.x, yAxis.y,yAxis.z); 
    quaternion = [self multiplyQuaternion:quaternion buAnother:currentQuaterion] 
} 
currentQuaterion = quaternion; 
glView.modelTransform = CATransform3DConcat([self makeRotationMatrixFromQuaternion:quaternion],perspective); 

currentQuaternion保持当前对象的旋转。

我的概要:乘以'旋转'总是使用四元数,而不是将tham转换为旋转矩阵。