图示:
实现:
public class InterceptSteering: Steering
{
public float safeDistance=15;
public override Vector3 ComputerFinalForce()
{
float angle = Vector3.Angle((target.transform.position - this.transform.position).normalized, this.transform.forward.normalized);
//if(Mathf.Acos(angle))
//目标距离
float distance = Vector3.Distance(target.transform.position, this.transform.position);
//目标速度
float targetSpeed = target.GetComponent<Vehicle>().currentForce.magnitude;
//时间
var time = distance / (speed + targetSpeed);
//目标在推断时间内行走的距离=速度*时间
var moveDistance = targetSpeed * time;
//拦截点位置=目标自身位置+目标在推断时间内行走的距离
Vector3 interceptPoint = target.transform.position + (target.transform.position).normalized * moveDistance;
if (distance > safeDistance) return Vector3.zero;
//期望=拦截点位置-自身位置
exceptForce = (interceptPoint-transform.position ).normalized * speed;
//实际=期望-当前
return (exceptForce - vehicle.currentForce) * weight;
}
}
本文介绍了一种名为InterceptSteering的算法实现,该算法用于计算一个对象如何调整其方向以最有效的方式拦截另一个移动目标。文章详细展示了算法的数学原理及Unity3D中的具体实现代码。
1166

被折叠的 条评论
为什么被折叠?



