iOS SceneKit欧拉角转四元数

使用欧拉角

    self.shipNode.eulerAngles = SCNVector3Make(pitch, -yaw, -roll);

使用欧拉角会造成万向锁,转成四元数就没有问题了

    float fCosHRoll = cos(-roll * .5f);
    float fSinHRoll = sin(-roll * .5f);
    float fCosHPitch = cos(pitch * .5f);
    float fSinHPitch = sin(pitch * .5f);
    float fCosHYaw = cos(-yaw * .5f);
    float fSinHYaw = sin(-yaw * .5f);
 
    float w = fCosHRoll * fCosHPitch * fCosHYaw + fSinHRoll * fSinHPitch * fSinHYaw;
    float x = fCosHRoll * fSinHPitch * fCosHYaw + fSinHRoll * fCosHPitch * fSinHYaw;
    float y = fCosHRoll * fCosHPitch * fSinHYaw - fSinHRoll * fSinHPitch * fCosHYaw;
    float z = fSinHRoll * fCosHPitch * fCosHYaw - fCosHRoll * fSinHPitch * fSinHYaw;
 
    
    self.shipNode.orientation = SCNVector4Make(x, y, z,w);

你可能感兴趣的:(iOS SceneKit欧拉角转四元数)