0

我有一个正在查看球体内部的 OpenGL 世界。相机位于 0,0,0 并且使用 CMMotionManager(accelerometer) 旋转矩阵正确地从 modelViewMatrix 旋转以旋转 modelViewMatrix。

当我尝试使用触摸屏和 CMMotionManager 矩阵旋转世界时,会出现此问题。

我尝试将触摸屏移动值添加到 CMMotionManager 返回的偏航、俯仰、滚动并创建一个新的 modelViewMatrix。我对此很接近,但是当我沿 X(滚动)或 Y(俯仰)触摸旋转 90 度然后将手机从横向旋转到纵向(90 度偏航旋转)时,矩阵无法正确旋转,它开始旋转我认为的 x 和 z 轴。

我认为问题在于我如何从偏航中计算 _rotationX 和 _rotationY。我认为它需要来自当前modelViewMatrix的偏航/俯仰/滚动,我只是无法弄清楚这个公式/如何用当前modelViewMatrix状态计算_rotationX和_rotationY。

更新相机的代码:

- (void)update
{   
    roll =  motionManager.deviceMotion.attitude.roll + _rotationX;
    pitch = motionManager.deviceMotion.attitude.pitch + _rotationY + M_PI;//M_PI is to offset to look at front
    yaw = motionManager.deviceMotion.attitude.yaw; //+ _rotationZ;

    float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
    _projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f),
                                                            -aspect,
                                                            0.2f,   //camera Position
                                                            25.0f);  //view zDistance


    _modelViewMatrix = GLKMatrix4Make(cos(yaw)*cos(pitch), cos(yaw)*sin(pitch)*sin(roll) - sin(yaw)*cos(roll), cos(yaw)*sin(pitch)*cos(roll) + sin(yaw)*sin(roll), 0,
sin(yaw)*cos(pitch), sin(yaw)*sin(pitch)*sin(roll) + cos(yaw)*cos(roll), sin(yaw)*sin(pitch)*cos(roll) - cos(yaw)*sin(roll), 0,
-sin(pitch), cos(pitch)*sin(roll), cos(pitch)*cos(roll), 0,
 0, 0, 0, 1);

    _modelViewProjectionMatrix = GLKMatrix4Multiply(_projectionMatrix, _modelViewMatrix);
}

更新触摸的代码:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    float distX = [touch locationInView:touch.view].x -
                  [touch previousLocationInView:touch.view].x;
    float distY = [touch locationInView:touch.view].y -
                [touch previousLocationInView:touch.view].y;

    distX *= 0.005;
    distY *= 0.005;
    //Update rotationX and rotationY based off the position of the phone's yaw landscape = 0, portrait = 90deg
    _rotationX += ((fabsf(cos(yaw))*distY + fabsf(sin(yaw))*distY)); 
    _rotationY += (-(fabsf(cos(yaw))*distX + fabsf(sin(yaw))*distX));

}

4

0 回答 0