2011-12-03 12 views
0

私は、拡張現実感アプリケーションのようなものを作っています.iOSデバイスの動きに関係なく、私は、iPhoneのサイドワードを傾けてその番号を壊すことが分かるまで、CMDeviceMotion.attitude.pitchでうまくセットアップしたと思っていました。だから、私はpARk *のコードからいくつかのコードを取りました。そして今、私は垂直アクセスの周りの回転を切り離そうとしています。私が描いているシーンは、ユーザが向いている見出しが気にならず、数字は常に視聴者から一定の距離だけ描かれます。私は、垂直軸の回転成分を計算すると、それを反転して回転行列からそれを掛けることができるので、ユーザが見出しを変えてもOpenGLの図を維持すると思います。CoreMotionの姿勢のrotationMatrixから水平回転を分離して削除します

CMDeviceMotion *d = motionManager.deviceMotion; 
if (d != nil) { 

    CMRotationMatrix r = d.attitude.rotationMatrix; 
    transformFromCMRotationMatrix(cameraTransform, &r); 


    mat4f_t projectionCameraTransform; 
    multiplyMatrixAndMatrix(projectionCameraTransform, projectionTransform, cameraTransform); 

    GLKMatrix4 rotMatrix = GLKMatrix4Make(projectionCameraTransform[0], 
              projectionCameraTransform[1], 
              projectionCameraTransform[2], 
              projectionCameraTransform[3], 
              projectionCameraTransform[4], 
              projectionCameraTransform[5], 
              projectionCameraTransform[6], 
              projectionCameraTransform[7], 
              projectionCameraTransform[8], 
              projectionCameraTransform[9], 
              projectionCameraTransform[10], 
              projectionCameraTransform[11], 
              projectionCameraTransform[12], 
              projectionCameraTransform[13], 
              projectionCameraTransform[14], 
              projectionCameraTransform[15]); 

    } 

は、私は、OpenGLでいつものようにrotMatrixを使用します。

は、ここに私のコードです。

思考、提案?前もって感謝します。

*実空間でいくつかのポイントを設定し、ユーザーの位置とそれらの相対方向を計算し、それらを画面上に描画して、その位置に向かって水平線を浮かべるようにします。

答えて

0

私はこの回答からいくつかのヒントを取り、その溶液を作ってみた:

https://stackoverflow.com/questions/5328848/simulating-an-image-floating-effect-using-coremotion-devicemotion-on-the-iphone/5442962#5442962

if (d != nil) { 

    GLKMatrix4 rotMatrix = GLKMatrix4MakeRotation(0, 0, 1, 0); 
    float pitch = d.attitude.pitch; 

    if (d.gravity.z > 0) 
     pitch = -pitch; 

    rotMatrix = GLKMatrix4Rotate(rotMatrix, pitch, -1, 0, 0); 
    rotMatrix = GLKMatrix4Rotate(rotMatrix, d.attitude.roll, 0, -1, 0); 
    rotMatrix = GLKMatrix4Rotate(rotMatrix, d.attitude.yaw, 0, 0, -1); 
    rotMatrix = GLKMatrix4Multiply(rotMatrix, GLKMatrix4MakeRotation(M_PI/2, 1, 0, 0)); 
} 

しかし、このゴチャゴチャに電話が垂直に近いとき。だから私はまだ見ている。

+0

ロール/ピッチ/ヨーではなく、attitude.rotationMatrixを使用してください。 rotationMatrixでは、「ほぼ垂直」の位置に問題はありません。 –

+1

attitude.rotationMatrixは私の元の質問の3行目です。それを違う方法で使う方法を提案できれば、完全な答えでそれをしてください。私はそれを試してみます。 – Craig

3

デバイスの画面の向きに応じて、Z軸の周りを回転させるだけです。

(gymbalロックの問題を回避し、したがって)オイラーに行くことなく、バック、
GLKMatrix4 deviceMotionAttitudeMatrix; 
if (_cmMotionmanager.deviceMotionActive) { 
    CMDeviceMotion *deviceMotion = _cmMotionmanager.deviceMotion; 

    // Correct for the rotation matrix not including the screen orientation: 
    // TODO: Let the device notify me when the orientation changes instead of querying on each update. 
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 
    float deviceOrientationRadians = 0.0f; 
    if (orientation == UIDeviceOrientationLandscapeLeft) { 
     deviceOrientationRadians = M_PI_2; 
    } 
    if (orientation == UIDeviceOrientationLandscapeRight) { 
     deviceOrientationRadians = -M_PI_2; 
    } 
    if (orientation == UIDeviceOrientationPortraitUpsideDown) { 
     deviceOrientationRadians = M_PI; 
    } 
    GLKMatrix4 baseRotation = GLKMatrix4MakeRotation(deviceOrientationRadians, 0.0f, 0.0f, 1.0f); 

    CMRotationMatrix a = deviceMotion.attitude.rotationMatrix; 
    deviceMotionAttitudeMatrix 
     = GLKMatrix4Make(a.m11, a.m21, a.m31, 0.0f, 
         a.m12, a.m22, a.m32, 0.0f, 
         a.m13, a.m23, a.m33, 0.0f, 
         0.0f, 0.0f, 0.0f, 1.0f); 
    deviceMotionAttitudeMatrix = GLKMatrix4Multiply(baseRotation, deviceMotionAttitudeMatrix); 
} 
else 
{ 
    // Look straight forward (we're probably in the simulator, or a device without a gyro) 
    deviceMotionAttitudeMatrix = GLKMatrix4MakeRotation(-M_PI_2, 1.0f, 0.0f, 0.0f); 
} 
+0

@Craig、この答えを試しましたか?これは、あなたが回転行列を使用せずに手動でしようとしていたものに似ています。私はこれがより良いと思います。 –

2

これはきれいではありませんが、私が必要とする正確に何をしているようだ。ここ態度を使用する方法を明確にするいくつかのコードです。 rotationMatrix

// initial model view matrix 
GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -5.f); 

// convert CMRotationMatrix to GLKMatrix4 
CMRotationMatrix r = motion.attitude.rotationMatrix; 
GLKMatrix4 = GLKMatrix4Make(r.m11, r.m21, r.m31, 0.0f, 
          r.m12, r.m22, r.m32, 0.0f, 
          r.m13, r.m23, r.m33, 0.0f, 
          0.0f, 0.0f, 0.0f, 1.0f); 

// apply motion rotation matrix 
modelViewMatrix = GLKMatrix4Multiply(modelViewMatrix, _motionRotationMatrix); 

// apply matrix to effect 
self.effect.transform.modelviewMatrix = modelViewMatrix; 
関連する問題