游戏 AI / 系统课时
用 Flee 让敌人脱离威胁
用 Flee 让敌人脱离威胁
Flee 可以看作 Seek 的反方向版本:单位不是寻找通往目标的方向,而是计算远离威胁的期望速度。真正实用的 Flee 还需要安全距离,否则远处威胁也会让单位持续逃跑,既浪费计算,又容易破坏正常移动行为。
学习目标
- 理解 Flee 与 Seek 在向量计算上的对应关系。
- 使用安全距离限制逃离行为的生效范围。
- 处理速度限制、威胁重合和安全距离边缘抖动等常见问题。
从追向目标改成远离威胁
Seek 的方向通常由 target - position 得到,而 Flee 只需要反过来计算 position - threat。这个向量从威胁位置指向单位当前位置,因此归一化后天然就是逃离方向。
接下来仍然复用同一套 Steering 思路:逃离方向乘以 maxSpeed 得到 desiredVelocity,再减去当前 velocity 得到 steering。因此 Flee 并不是重新设计一套移动系统,而是替换了期望速度的方向来源。

加入安全距离
如果场景中存在很多潜在威胁,所有单位都对无限远目标执行 Flee 会让行为无法收敛。更常见的做法是先计算单位与威胁的距离,只有距离小于 safeDistance 时才产生逃离转向力。
下面的完整 Java 示例包含这一判断,并处理了单位与威胁完全重合的边界情况。重合时无法从零向量得到方向,因此示例使用固定方向作为最小可运行的回退策略;正式游戏可以改成单位最近一次有效移动方向或可复现的随机方向。
import java.util.Locale;
public class FleeDemo {
static final class Vec2 {
final double x;
final double y;
Vec2(double x, double y) {
this.x = x;
this.y = y;
}
Vec2 add(Vec2 other) {
return new Vec2(x + other.x, y + other.y);
}
Vec2 sub(Vec2 other) {
return new Vec2(x - other.x, y - other.y);
}
Vec2 scale(double value) {
return new Vec2(x * value, y * value);
}
double length() {
return Math.sqrt(x * x + y * y);
}
Vec2 normalized() {
double len = length();
return len < 1e-9 ? new Vec2(0, 0) : scale(1.0 / len);
}
Vec2 limit(double maxLength) {
double len = length();
return len > maxLength ? scale(maxLength / len) : this;
}
}
static Vec2 flee(Vec2 position, Vec2 velocity, Vec2 threat,
double safeDistance, double maxSpeed, double maxForce) {
Vec2 away = position.sub(threat);
double distance = away.length();
// 威胁已经在安全范围之外,不再产生逃离转向力。
if (distance >= safeDistance) {
return new Vec2(0, 0);
}
// 完全重合时必须提供一个可用方向,否则归一化结果仍为零。
Vec2 direction = distance < 1e-9 ? new Vec2(1, 0) : away.normalized();
Vec2 desiredVelocity = direction.scale(maxSpeed);
return desiredVelocity.sub(velocity).limit(maxForce);
}
public static void main(String[] args) {
Locale.setDefault(Locale.US);
Vec2 position = new Vec2(3, 4);
Vec2 velocity = new Vec2(0.5, 0.2);
Vec2 threat = new Vec2(0, 0);
double dt = 0.5;
Vec2 steering = flee(position, velocity, threat, 8.0, 5.0, 3.0);
Vec2 nextVelocity = velocity.add(steering.scale(dt)).limit(5.0);
Vec2 nextPosition = position.add(nextVelocity.scale(dt));
Vec2 farThreatSteering = flee(position, velocity, new Vec2(20, 20),
8.0, 5.0, 3.0);
System.out.printf("steering=(%.3f, %.3f)%n", steering.x, steering.y);
System.out.printf("velocity=(%.3f, %.3f)%n", nextVelocity.x, nextVelocity.y);
System.out.printf("position=(%.3f, %.3f)%n", nextPosition.x, nextPosition.y);
System.out.printf("farThreatSteering=(%.3f, %.3f)%n",
farThreatSteering.x, farThreatSteering.y);
}
}预期输出:
steering=(1.649, 2.506)
velocity=(1.324, 1.453)
position=(3.662, 4.727)
farThreatSteering=(0.000, 0.000)第一次计算中,单位距离威胁只有 5,小于 safeDistance=8,因此产生逃离转向力;第二个威胁远在安全范围之外,所以直接返回零向量。
安全距离不是停止移动距离
Flee 返回零转向力,只表示“当前不再因为这个威胁继续加速”,并不代表单位的已有速度会立即归零。如果角色还需要停下来,必须由阻尼、制动行为或其他 Steering Behavior 继续处理当前速度。
这一区别非常重要:Steering Behavior 通常负责计算应该怎样修正运动,而不是直接覆盖角色最终位置。把超出安全距离后的速度强制设为零,会造成明显的突然刹停。
避免安全边界反复开关
只有一个 safeDistance 时,单位可能恰好在边界附近进进出出,使 Flee 在相邻帧之间不断开启和关闭。简单场景可以接受这种行为,但有碰撞、网络抖动或多个威胁时会更明显。
更稳定的办法是使用两个距离:例如距离小于 8 时进入逃离状态,只有距离大于 10 才退出。这个做法形成迟滞区间,可以避免在单个阈值附近频繁切换。
Flee 最适合解决“进入危险范围后迅速拉开距离”的问题。只要保持方向计算、速度修正和状态范围三个职责分离,后续就能自然地把它与 Seek、避障和群体行为组合起来。