2D逆运动学角度约束

时间:2016-08-05 18:08:55

标签: javascript algorithm math inverse-kinematics

我正在编写一个带有反向运动学的2D游戏。我正在使用这个简单的算法:

let angleToParent = angleBetweenPoints(parent, child)
child.x = parent.x + distance*Math.cos(angleToParent)
child.y = parent.y + distance*Math.sin(angleToParent)
child.rotation = angleToParent

如何为孩子实施角度约束?

当父级旋转180度时,此代码工作错误:

let implementBound = (lowerBound, upperBound, input) => { 
    return Math.min(Math.max(lowerBound, input), upperBound)
}

let constraint = (Math.PI/180)*15 
let left = parent.rotation - constraint 
let right = parent.rotation + constraint 
angleToParent = implementBound(left, right, angleToParent)

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

if (input < lowerBound || input > upperBound)
{
    //check which limit is closer
    while (Math.abs(upperBound - input ) > Math.PI)
        input += 2 * Math.PI * (input < upperBound ? 1.0f : -1.0f);
    double distance_to_upper = Math.abs(upperBound - input);

    while (Math.abs(lowerBound - input) > Math.PI)
        input += 2 * Math.PI * (input < lowerBound ? 1.0f : -1.0f);
    double distance_to_lower = Math.abs(lowerBound - input);

    if (distance_to_lower < distance_to_upper)
        return lowerBound;
    else
        return upperBound;
}
else
    return input;