gpt4 book ai didi

javascript - 二维逆运动学 Angular 约束

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:59:27 24 4
gpt4 key购买 nike

我正在编写一个具有反向运动学的 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

如何实现childs的 Angular 约束?

当父级旋转 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)

最佳答案

您可以执行以下操作:

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;

关于javascript - 二维逆运动学 Angular 约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38795450/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com