天天看點

Understanding Steering Behaviors:Flee and Arrival(行為控制-逃離和抵達)原文連結:http://gamedevelopment.tutsplus.com/tutorials/understanding-steering-behaviors-flee-and-arrival--gamedev-1303跑開增加逃離力抵達減速結論

原文連結:http://gamedevelopment.tutsplus.com/tutorials/understanding-steering-behaviors-flee-and-arrival--gamedev-1303

跑開

在前面介紹的尋找行為中,我們介紹了一種基于兩個向量力來将人物推向目标的行為方式:預期速度和控制力

desired_velocity = normalize(target - position) * max_velocity ;

steering = desired_velocity - velocity ;

在上面說的desired_velocity是從人物直接指向目标的。我們隻要簡單的将目标點減去人物的點就能夠得到這個速度。

而在本節中,同樣的,我們也需要使用這樣的兩個力,但是,我們卻是使用這兩個力來避免物體移動到目标那裡去。

我們現在計算新的desired_velocity,這個速度想在用人物的位置減去目标的位置,這樣得到的desired_velocity方向就和上面介紹的行為方式的方向是相反的

我們同樣的來用代碼來闡釋這個概念:

desired_velocity = normalize(position - target) * max_velocity

steering = desired_velocity - velocity

上面計算出來的desired_velocity表示了最容易逃離的路線。而産生的steering控制力将會使的物體丢棄原有的移動路徑,而迫使它向desired_velocity靠近。

我們可以将逃離行為和尋找行為聯系在一起:

flee_desired_velocity = -seek_desired_velocity

換句話說,一個向量是另外一個向量的反向向量。

增加逃離力

當我們計算出來了逃離的力之後,我們就必須将這個力添加到人物的速度向量上面去。由于這個力是将物體推向目标相反的方向上去,是以,每一幀物體都會向逃離目标的方向去移動,自然而然就産生了一條逃離路徑了。

如何增加逃離力,和前面介紹的seek行為是一樣的累積方法。

上面的算法是一直的逃離,我們可以添加一個熟悉來控制,如果距離目标有一段距離之後就不再進行逃離。

抵達

尋找行為使的物體向一個指定的目标移動。當到達這個目标之後,物體會在目标附近來回不斷的亂動,也就是說會穿過目标地點。而抵達行為就是控制人物不要穿過這個目标點,在當人物慢慢的接近目标點的時候,速度會慢慢的下降,直到最後在目的地點停止下來。

這個行為由兩個部分控制。當人物遠離目标地點的時候,就像原來的seek行為一樣。

當人物距離目标地點很近的時候,在減速區域的時候,我們就慢慢的讓人物減速下來:

Understanding Steering Behaviors:Flee and Arrival(行為控制-逃離和抵達)原文連結:http://gamedevelopment.tutsplus.com/tutorials/understanding-steering-behaviors-flee-and-arrival--gamedev-1303跑開增加逃離力抵達減速結論

當人物進入這個圓圈的時候,它就會慢慢的減速下來。

減速

當人物進入了減速區域的時候,它的速度就會線性的慢慢的減下來。這樣的效果可以通過簡單的增加一個新的控制力到人物的速度向量上來就可以做到。當這個方法最終增加的速度變成0的時候,那麼就表示将沒有任何的速度添加到這個人物的位置上:

<span style="font-family:Microsoft YaHei;">// If (velocity + steering) equals zero, then there is no movement
velocity = truncate(velocity + steering, max_speed)
position = position + velocity
 
function truncate(vector:Vector3D, max:Number) :void {
    var i :Number;
    i = max / vector.length;
    i = i < 1.0 ? i : 1.0;
    vector.scaleBy(i);
}</span>
           

為了保證人物會慢慢的減速下來,我們不能立即的将人物的速度變成0.我們可以通過人物現在距離目标的地點和減速區域的半徑來判斷應該對人物現有的速度做多少的變化,進而能夠很平滑的實作減速效果:

<span style="font-family:Microsoft YaHei;">// Calculate the desired velocity
desired_velocity = position - target
distance = length(desired_velocity)
 
// Check the distance to detect whether the character
// is inside the slowing area
if (distance < slowingRadius) {
    // Inside the slowing area
    desired_velocity = normalize(desired_velocity) * max_velocity * (distance / slowingRadius)
} else {
    // Outside the slowing area.
    desired_velocity = normalize(desired_velocity) * max_velocity
}
 
// Set the steering based on this
steering = desired_velocity - velocity</span>
           

如果人物距離目标地點的距離遠遠的大于slowRadious,那麼就意味着這個人物距離目标地點非常的遠,是以,我們隻要簡單的使用尋找行為來尋找目标點即可,并且保持速度不變。

如果他們之間的距離比slowRadious要小的時候,這就意味着,人物抵達了減速區域,是以現在我們需要将它進行減速。

distanc/slowRadious 的值在減速區域中将會從0到1不等。這樣的線性變化就會使得人物的移動速度慢慢的呈線性減速下來。

Understanding Steering Behaviors:Flee and Arrival(行為控制-逃離和抵達)原文連結:http://gamedevelopment.tutsplus.com/tutorials/understanding-steering-behaviors-flee-and-arrival--gamedev-1303跑開增加逃離力抵達減速結論

正如前面介紹的那樣,這個移動方式将會像下面這樣進行表示:

<span style="font-family:Microsoft YaHei;">steering = desired_velocity - velocity
velocity = truncate (velocity + steering , max_speed)
position = position + velocity</span>
           

如果預期速度最後降低到0的時候,那麼控制力的大小就會變成-velocity。是以,把這個行為力加到速度上的時候,就會導緻人物最終移動的速度變成了0,停止下來了。

結論

逃離行為使的人物遠離目标地點,而抵達行為使得人物在抵達地點時停止下來。這樣行為都是可以在一定程度上進行組合進而做成類似于跟随,逃離這樣的行為。

繼續閱讀