约束音高,偏航和&滚

时间:2015-09-27 22:47:09

标签: vector 3d constraints quaternions

我表示为四元数的旋转,我试图限制音高,偏航和&滚动轴。我试着这样做了:

public struct Orientation
{
    public Vector3 up, forward;

    public Orientation(Vector3 up, Vector3 forward)
    {
        this.up = up;
        this.forward = forward;
    }
}

public static Orientation[] orientations = new Orientation[3]
{
    new Orientation(Vector3.right, Vector3.up),
    new Orientation(Vector3.up, Vector3.forward),
    new Orientation(Vector3.forward, Vector3.right)
};

public enum Axis
{
    Pitch,
    Yaw,
    Roll
};

private Vector3 ConstrainAxis(Vector3 vector, Axis axis, float from, float to)
{
    Orientation orientation = orientations[(int)axis];

    float theta = (to - from) * 0.5F;

    Vector3 cons = Quaternion.AngleAxis(from + theta, orientation.up) * orientation.forward;
    Vector3 proj = Vector3.ProjectOnPlane(vector, orientation.up);

    return ConstrainVector(cons.normalized, proj.normalized, theta);
}

private Vector3 ConstrainVector(Vector3 from, Vector3 to, float angle)
{
    float theta = Mathf.Abs(angle / Vector3.Angle(from, to));

    if(theta < 1.0F)
    {
        return Vector3.Slerp(from, to, theta);
    }

    return to;
}

事实证明,这只是一种过于复杂的约束欧拉角表示的各个组成部分的方式,其中两个都受到奇怪的抖动问题(万向节锁定相关?)。

约束这些轴的最佳方法是什么?

2 个答案:

答案 0 :(得分:2)

对于关节约束,通常的做法是使用“摆动扭曲”参数化。 为了将当前旋转表示为四元数的“摆动扭曲”,这是良好的分解 http://www.alinenormoyle.com/weblog/?p=726

“摆动”和“扭曲”的约束可以用四元数来完成。

如果我们想将摆动约束到+ -30度,则伪代码看起来像

if($(this).nextAll("div").children('form').length>0) {
        isFormHeading = true;

答案 1 :(得分:0)

添加到minorlogic的答案:保存targetQuat的W分量的符号很重要。这是twist约束的Three.js实现。似乎还有一些我没有检查过的奇点: http://www.allenchou.net/2018/05/game-math-swing-twist-interpolation-sterp/

const HEAD_YAW_MAX = 40
const MAX_MAGNITUDE = Math.sin(0.5 * THREE.Math.degToRad(HEAD_YAW_MAX));
const MAX_MAGNITUDE_W = Math.sqrt(1.0 - MAX_MAGNITUDE * MAX_MAGNITUDE);
const MAX_MAG_POW_2 = MAX_MAGNITUDE * MAX_MAGNITUDE;

in update function

const qT = this.headBone.quaternion;
v1.set(qT.x, qT.y, qT.z); //todo check singularity: rotation by 180
v1.projectOnVector(this.headBone.up); //up is direction around twist
// v1.set(0, qT.y, 0); //project on y axis
q1.set(v1.x, v1.y, v1.z, qT.w); //twist
q1.normalize();
q3.copy(q1).conjugate();
q2.multiplyQuaternions(qT, q3); //swing
q2.normalize();
v1.set(q1.x, q1.y, q1.z);
if (v1.lengthSq() > MAX_MAG_POW_2) {
   v1.setLength(MAX_MAGNITUDE);
   const sign = qT.w < 0 ? -1 : 1;
   q1.set(v1.x, v1.y, v1.z, sign * MAX_MAGNITUDE_W);
   this.headBone.quaternion.multiplyQuaternions(q2, q1); //swing * twist
}

摆动扭曲参数化算法的来源: Component of a quaternion rotation around an axis