路径跟随,就是按照指定的路径行驶
1目的
1目的
如题,机车按照指定的路径行驶
靠近路径的wayPoint,Seek下一个wayPoint
路径跟随的基本思路就是假设在路径上有n个wayPoint,
这样机车的路径跟随就可以化解为seek一个一个的wayPoint,
这样就不用路径中那些弧形弯而犯愁了.好了,看看程序怎么实现吧.
首先我们要向一个wayPoint进行行驶,如前面讲的使用seek方法,当机车与wayPoint的距离dist小于特定的pathThreshold时,
seek下一个wayPoint
,直接看代码吧,更加明了:
public function followPath(path:Array, loop:Boolean = true):void {
//当前的wayPoint
var wayPoint:Vector2D = path[_pathIndex];
if (wayPoint == null) return;
//是否到达当前wayPoint
if (<span style="color:#ff0000;">_position.dist(wayPoint) < _pathThreshold</span>) {
if (_pathIndex >= path.length - 1) {
//如果是最后一个wayPoint,如果循环,返回到第一个wayPoint
if (loop) {
_pathIndex = 0;
}else {
<span style="color:#ff0000;">arrive(wayPoint);</span>
}
}else {
//如果已经到达,<span style="color:#cc0000;">seek下一个wayPoint</span>
_pathIndex++;
}
}else {
<span style="color:#ff0000;"> seek(wayPoint);</span>
}
}
------------------------------------------
c++
它适用于大多数需要智能体访问一系列检查点的情况
如果是封闭的路径,应该回到第一个路点重新开始
如果是开放的路径,交通工具减速(arrive)在最后的路点上。
//------------------------------- FollowPath -----------------------------
//
// Given a series of Vector2Ds, this method produces a force that will
// move the agent along the waypoints in order. The agent uses the
// 'Seek' behavior to move to the next waypoint - unless it is the last
// waypoint, in which case it 'Arrives'
//------------------------------------------------------------------------
Vector2D SteeringBehavior::FollowPath()
{
//如果离当前目标足够近,移到下一个目标(用距离平方计算)
if(Vec2DDistanceSq(m_pPath->CurrentWaypoint(), m_pVehicle->Pos()) <
m_dWaypointSeekDistSq)
{
m_pPath->SetNextWaypoint();
}
if (!m_pPath->Finished())
{
return Seek(m_pPath->CurrentWaypoint());
}
else
{
return Arrive(m_pPath->CurrentWaypoint(), normal);
}
}