gpt4 book ai didi

ios - 从 GLKQuaternion 中提取的意外滚动和俯仰

转载 作者:行者123 更新时间:2023-11-29 11:51:47 25 4
gpt4 key购买 nike

我使用 CMAttitude 中的四元数来旋转 SceneKit 相机。我还需要使用从四元数中提取的 Y 旋转角度。我预计它会滚动,但提取后 Y 旋转角度对应于具有 -90:90 范围的间距。如何将此范围转换为 0:180 或 0:360?

- (SCNQuaternion)SCNQuaternionFromCMQuaternion:(CMQuaternion)q {

GLKQuaternion Q = GLKQuaternionMake(q.x, q.y, q.z, q.w);
GLKQuaternion xRotQ = GLKQuaternionMakeWithAngleAndAxis(-M_PI_2, 1, 0, 0);
Q = GLKQuaternionMultiply(xRotQ, Q);

double roll = atan2(2.0 * (Q.y * Q.z - Q.w * Q.x), 1.0 - 2.0 * (Q.x * Q.x + Q.y * Q.y)); // 0:180 but around X

double pitch = RADIANS_TO_DEGREES(asin(-2.0f * (Q.x * Q.z + Q.w * Q.y))); // 0:90 around Y

NSLog(@"%f", pitch);
// ...

CMQuaternion rq = {.x = Q.x, .y = Q.y, .z = Q.z, .w = Q.w};

return SCNVector4Make(rq.x, rq.y, rq.z, rq.w);
}

最佳答案

我是这样找到的:

- (SCNQuaternion)SCNQuaternionFromCMQuaternion:(CMQuaternion)q {

GLKQuaternion Q = GLKQuaternionMake(q.x, q.y, q.z, q.w);
GLKQuaternion xRotQ = GLKQuaternionMakeWithAngleAndAxis(-M_PI_2, 1, 0, 0);
Q = GLKQuaternionMultiply(xRotQ, Q);

double gx = 2.0 * (Q.y * Q.w - Q.x * Q.z);
//double gy = 2.0 * (Q.x * Q.y + Q.z * Q.w);
double gz = Q.x * Q.x - Q.y * Q.y - Q.z * Q.z + Q.w * Q.w;

double pitch = RADIANS_TO_DEGREES(-asin( -2.0 * (Q.y * Q.w - Q.x * Q.z)));

if (gx >= 0 && gz < 0)
pitch = 180 - pitch;
else if (gx < 0 && gz < 0)
pitch = 180 - pitch;
else if (gx < 0 && gz >= 0)
pitch = 360 + pitch;

NSLog(@"%f", pitch); // now it has 0-360 range

CMQuaternion rq = {.x = Q.x, .y = Q.y, .z = Q.z, .w = Q.w};

return SCNVector4Make(rq.x, rq.y, rq.z, rq.w);
}

关于ios - 从 GLKQuaternion 中提取的意外滚动和俯仰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40793949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com