C# 通过另一个四元数的旋转来旋转一个四元数

C# 通过另一个四元数的旋转来旋转一个四元数,c#,unity3d,rotation,quaternions,C#,Unity3d,Rotation,Quaternions,上下文 我正在使用混合现实和统一,并试图用手控制游戏对象的3d旋转 以下是我目前掌握的情况: Vector3 _lefthand = new Vector3(Left.x, Left.y, Left.z); Vector3 _righthand = new Vector3(Right.x, Right.y, Right.z); _myObject.transform.rotation = Quaternion.FromToRotation(Vector3.right, _

上下文

我正在使用混合现实和统一,并试图用手控制游戏对象的3d旋转

以下是我目前掌握的情况:

    Vector3 _lefthand = new Vector3(Left.x, Left.y, Left.z);
    Vector3 _righthand = new Vector3(Right.x, Right.y, Right.z);
    _myObject.transform.rotation = Quaternion.FromToRotation(Vector3.right, _righthand - _lefthand));
该块包含在
IEnumerator
协同程序中,当满足
Update
中的
if
语句时,该协同程序开始。
if
检查特定的手部姿势,因此当手部姿势处于活动状态时旋转“开始”,而当它们不处于活动状态时旋转“停止”

对于一个小的旋转,我所做的很好,但是如果我想旋转对象几次,就会失败,因为
\u myObject
的旋转正好匹配我手的当前
矢量3
位置创建的当前四元数

问题


如何根据手的起始位置和结束位置之间的差值,将myObject从当前旋转旋转到新的旋转?

在每帧开始处,缓存旋转和左手->右手方向,然后使用
Quat B*Quat a
在世界空间中按B旋转a,或者使用
transform.Rotate
Space.World

IEnumerator DoRotate()
{        
    Vector3 previousLeftHand = new Vector3(Left.x, Left.y, Left.z);
    Vector3 previousRightHand = new Vector3(Right.x, Right.y, Right.z);  

    Vector3 previousFromLeftToRight = previousRightHand - previousLeftHand;

    while (!doneFlag)
    {
        yield return null;

        Vector3 newLeftHand = new Vector3(Left.x, Left.y, Left.z);
        Vector3 newRightHand = new Vector3(Right.x, Right.y, Right.z);

        Vector3 newFromLeftToRight = newRightHand - newLeftHand;

        Quaternion deltaRotationWorld = Quaternion.FromToRotation(
                previousFromLeftToRight, newFromLeftToRight);

        _myObject.transform.rotation = deltaRotationWorld * _myObject.transform.rotation;
        // or _myObject.transform.Rotate(deltaRotationWorld, Space.World);

        previousFromLeftToRight = newFromLeftToRight;

    }
}

除了缓存开始旋转和开始手的位置外,您可以使用此技术,但它通常会导致沿着手之间的局部轴进行意外旋转。使用每帧增量可以最小化这一点。

@derHugo我认为这更像是一个XY问题-最简单的解决方案是从旋转过程的一开始就应用每帧增量,而不是增量。此外,从文档中也不能立即看出,在世界参照系中通过
lhs
旋转
rhs
也是
lhs*rhs
——文档中只明确提到在
lhs
的本地参照系中通过
rhs
旋转
lhs
。感谢@Ruzihm,这非常有效这很容易理解。