我正在努力在我的一个 OpenGL Android 项目中实现转向行为,但我在理解为什么我的 Arrive 行为代码的行为不正常时遇到了一些麻烦。
在更新循环期间,我的程序计算一个转向力来更新移动代理的位置。目前,它的行为与“Seek”行为相同,因为它会移动到 Targetpos 中定义的位置,但它不会在接近应有的点时减速和停止,而是继续向前移动并一遍又一遍地越过目标再次。
对此的任何帮助将不胜感激,下面是应该为代理返回正确转向力的代码。
减速只是一个 1-3 的枚举,编码代理应该减速的不同速率。
private Vector2 Arrive(Vector2 Targetpos, Deceleration deceleration) {
Vector2 ToTarget = sub(Targetpos, agentPos);
//calculate the distance to the target
float dist = ToTarget.len();
if (dist > 0.2f) {
final float DecelerationTweaker = 0.3f;
//calculate the speed required to reach the target given the desired
//deceleration
float speed = dist / ((float) deceleration.value() * DecelerationTweaker);
//make sure the velocity does not exceed the max
speed = Math.min(speed, agentMaxSpeed);
Vector2 DesiredVelocity = mul(ToTarget, speed / dist);
return sub(DesiredVelocity, agentVelocity);
}
return new Vector2(0, 0);
}