追逐 Pursuit
在我们追到目标之前,目标有多长的时间可以逃跑?就是我们追他的这段时间嘛lookAheadTime,这个时间就是我们与目标之间的距离除以我们的宿舍啊!转换到机车来想就是:
lookAheadTime = pursuer.position.dist(vech.position)/maxSpeed;
pursuer.position是目标的位置
dist 计算出 智能体 距离 目标的距离 ,再把计算出的距离 除以 maxSpeed ,就是追他的时间
public function pursue(target:Vehicle):void {
var lookAheadTime:Number = target.position.dist(_position) / _maxSpeed;
//计算此时追上目标的预计时间,即lookAheadTime.
var predictedTarget:Vector2D = target.position.add(target.velocity.multiply(lookAheadTime));
//在这段时间内,目标以现在的速度,预计会到达的位置,即"未来目标"
seek(predictedTarget);
//寻找这个"未来目标"
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//------------------------------ Pursuit ---------------------------------
//
// this behavior creates a force that steers the agent towards the
// evader
// 参数是逃跑者
//------------------------------------------------------------------------
Vector2D SteeringBehavior::Pursuit(const Vehicle* evader)
{
//如果逃避者在前面,而且面对着智能体,那么我们可以正好靠近逃避者的当前位置
//这个向量是指向淘宝者的,因为是逃跑者减去智能体
Vector2D ToEvader = evader->Pos() - m_pVehicle->Pos();
//智能体方向 和 逃跑者 的点乘
double RelativeHeading = m_pVehicle->Heading().Dot(evader->Heading());
if ( (ToEvader.Dot(m_pVehicle->Heading()) > 0) &&
(RelativeHeading < -0.95))
//acos(0.95)=18 degs(度)
{
return Seek(evader->Pos());
}
//预测逃避者的位置
//预测的时间正比于逃避者和追逐着的距离,反比于智能体的速度和
double LookAheadTime = ToEvader.Length() /
(m_pVehicle->MaxSpeed() + evader->Speed());
//现在靠近逃避者的被预测的位置
return Seek(evader->Pos() + evader->Velocity() * LookAheadTime);
}
Vector2D ToEvader = evader->Pos() - m_pVehicle->Pos();
//智能体方向 和 逃跑者 的点乘
double RelativeHeading = m_pVehicle->Heading().Dot(evader->Heading());
if ( (ToEvader.Dot(m_pVehicle->Heading()) > 0) &&
(RelativeHeading < -0.95))
//acos(0.95)=18 degs(度)
{
return Seek(evader->Pos());
}
如果RelativeHeading 小于0 ,表示点击为负,在m_pVehicle->Heading 和 evader->Heading 的方向相对的情况下会有,比如向量(2,3) 和 (-4,-2)
ToEvader 表示 朝向 逃避者的方向,和智能体的方向是一样的,所以点乘之后 大于 0
以上的判断是看智能体是不是几乎面对逃避者,如果是,那么智能体直接向逃避者的 当前位置 移动。
否则就要靠近逃避者的被预测的位置。
-------------------------------------------------------------