Coremotionの態度の回転マトリックスから水平方向の回転を分離して除去します
-
27-10-2019 - |
質問
私は拡張現実アプリのようなものを作っています。そこには、iOSデバイスがどのように動いても重力と整合したいOpenGLシーンがあります。 iPhoneがその数を覆い隠すことを発見するまで、cmdevicemotion.attitude.pitchで正常にセットアップされたと思いました。そこで、私は公園*の例からいくつかのコードを取りましたが、今では垂直アクセスの周りの回転を分離しようとしています。私が描いているシーンは、ユーザーがどちらに向いているかを気にしません。数字は常に視聴者から設定された距離に描かれます。垂直軸回転コンポーネントを把握したとき、それを逆転させて回転マトリックスから掛けることができるので、ユーザーが見出しを変更したときにオープングリューの数値をまだ保ちます。
これが私のコードです:
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を使用します。
考え、提案?前もって感謝します。
*Park Sample Codeは、実際のスペースにいくつかのポイントを設定し、ユーザーの位置とそれらのポイントの相対的な方向を実行し、画面上に描画するため、その位置に向かって地平線に浮かぶように見えます。
解決 3
私はこの答えからいくつかのヒントを取り、解決策を思いつきました:
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));
}
ただし、電話が垂直に近い場合は、この干し草があります。だから私はまだ探しています。
他のヒント
デバイス画面の向きに応じて、Z軸の周りに向けを回転させるだけです。これは最もきれいではありませんが、オイラーに行ったり背を向けたりせずに必要なことを正確に行うようです(したがって、ジンバルロックの問題を避けます)
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);
}
Attuster.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;