如何获得任意轴上四元数的符号角

How to get the signed angle of a quaternion on an arbitrary axis

我正在尝试制作一个飞机控制器,我的目标是介于街机和现实之间,所以我希望飞机以与滚转成比例的力转弯。

我没有进行任何调整,我仍在制作整个原型,但我在使用四元数时遇到了获取带符号旋转角度的问题,我查看了 Determining if quarternion rotation is clockwise or counter clockwise 在这里,但是我无法将解决方案推广到旋转可以位于的(几乎)任意平面。

我现在做了什么:

private void FixedUpdate()
    {
        float desiredYaw = _yaw * _rotationSpeed * Time.fixedDeltaTime;
        float desiredPitch = -_pitch * _rotationSpeed * Time.fixedDeltaTime;

        float rotationStepSize = _throttle * Time.fixedDeltaTime;

        Quaternion toRotate = Quaternion.Euler(desiredPitch, 0, desiredYaw);
        Quaternion straighRotation = Quaternion.LookRotation(_transform.forward, Vector3.up );
        
        _rotation = _transform.rotation * toRotate;
        float turningForce = Quaternion.Angle( _rotation, straighRotation );

        _rigidbody.MoveRotation( _rotation );
        _rigidbody.AddTorque( turningForce * _rotationForce * rotationStepSize * Vector3.up );
        _rigidbody.AddRelativeForce( _speed * rotationStepSize * Vector3.forward );
    }

编辑:我意识到我是在使用滚动而不是偏航来计算转动力,这只是错误的措辞,现在已更正。

由于您只需要一个描述飞机右侧向下程度的因子,因此您只需使用飞机右侧的 y 分量即可。无需引入四元数甚至三角函数。评论中的解释:

private void FixedUpdate()
{
    // ...

    // Calculate how downward local right is in range [-1,1]
    // The more downward, the more tilted right the plane is
    //   positive = tilted right
    //   negative = tilted left
    float turnFactor = -_transform.right.y;

    // Could do things to modify turnFactor to affect easing here.
    // For instance, if turning rate should start slower then rapidly increase:
    //   turnFactor = Mathf.Sign(turnFactor) * turnFactor * turnFactor;

    // Use factor and _rotationForce member to calculate torque, apply along 
    //   global up. 
    // We expect to call this every fixed frame so we can just use the default
    //   ForceMode of ForceMode.Force which multiplies fixed delta time inside.
    _rigidbody.AddTorque(_rotationForce * turnFactor * Vector3.up);

    // ...
}