
在游戏开发中,为ai角色设计漫游行为是常见的需求。然而,简单的随机力生成或角度偏移往往会导致ai角色逐渐偏离其初始生成点,甚至无限远离,这在需要ai在特定区域内活动的场景中是不可接受的。为了解决这一问题,我们需要引入一种“加权”机制,使ai在远离起始点时,其漫游行为能够被引导回原点。
这种方法的核心思想是在AI生成其随机漫游力或漫游角度之后,根据其当前位置与起始点之间的距离和方向,对该力或角度进行修正。如果AI正朝着远离起始点的方向移动,则削弱该力的影响,或调整其方向使其更偏向起始点。
实现原理:
示例代码(概念性):
import com.badlogic.gdx.math.Vector2; // 假设使用LibGDX的Vector2
// 假设SimplexNoise和AngVecTools是已有的工具类
// AngVecTools.angleToVect(angle) 将角度转换为单位向量
// SimplexNoise.noise(x, y) 生成Perlin噪声值
public class WeightedWanderAI {
    private Vector2 spawnPoint; // AI的起始生成点
    private Vector2 currentPosition; // AI当前位置
    private float perlinPos; // Perlin噪声的采样位置,随时间或步进更新
    private float wanderPower = 1.0f; // 漫游力的强度
    private float inc = 0.01f; // Perlin噪声采样步进
    private float startAngle = 0.0f; // 初始角度偏移
    public WeightedWanderAI(Vector2 spawnPoint, Vector2 initialPosition) {
        this.spawnPoint = spawnPoint;
        this.currentPosition = initialPosition;
        this.perlinPos = 0.0f; // 初始化Perlin噪声采样位置
    }
    /**
     * 计算带有回归偏向的漫游力。
     * @return 修正后的漫游力向量。
     */
    public Vector2 calculateWeightedWanderForce() {
        // 1. 生成原始Perlin噪声漫游力
        perlinPos += inc;
        float rawWanderAngle = (float) (SimplexNoise.noise(perlinPos, 100.213) * wanderPower) + startAngle;
        Vector2 rawWanderForce = AngVecTools.angleToVect(rawWanderAngle);
        // 2. 计算与起始点的距离和指向起始点的向量
        Vector2 toSpawnVector = spawnPoint.cpy().sub(currentPosition);
        float distToSpawn = toSpawnVector.len();
        // 3. 定义一个距离阈值和回归强度参数
        float returnRadius = 50.0f; // AI开始有回归倾向的距离
        float maxReturnInfluence = 0.8f; // 最大回归影响,例如,将回归力占总力的比例
        // 4. 根据距离计算回归权重
        float returnWeight = 0.0f;
        if (distToSpawn > returnRadius) {
            // 距离越远,回归权重越高,线性增长
            // 假设在 returnRadius * 2 处达到最大权重1
            returnWeight = Math.min(1.0f, (distToSpawn - returnRadius) / returnRadius);
        }
        // 5. 应用回归偏向:引入一个指向起始点的额外力
        Vector2 normalizedToSpawn = toSpawnVector.nor(); // 指向起始点的单位向量
        // 回归力强度与漫游力强度、回归权重和最大回归影响相关
        Vector2 returnForce = normalizedToSpawn.scl(returnWeight * wanderPower * maxReturnInfluence);
        // 6. 结合原始漫游力与回归力
        // 可以直接相加,或者根据距离动态调整原始漫游力的贡献
        Vector2 finalWanderForce = rawWanderForce.add(returnForce);
        return finalWanderForce;
    }
    // 假设AI的更新方法会调用calculateWeightedWanderForce
    public void update(float deltaTime) {
        Vector2 force = calculateWeightedWanderForce();
        // 应用力到AI的物理系统,更新currentPosition
        // 例如:currentPosition.add(force.scl(deltaTime));
    }
}注意事项:
这种方法需要仔细调整权重因子和距离阈值,以避免AI行为过于生硬或抖动。如果参数设置不当,AI可能会在到达回归点附近时突然转向,导致不自然的往复运动。
这种方法不直接修改漫游力,而是为AI设定一个动态的“目标漫游点”。这个目标漫游点本身会围绕AI的起始生成点进行Perlin噪声式的随机移动。AI的实际行为是持续地朝着这个目标漫游点移动。这样,AI便会自然地在起始点周围的一个限定区域内活动,而不会无限远离。
实现原理:
示例代码:
import com.badlogic.gdx.math.Vector2; // 假设使用LibGDX的Vector2
// 假设SimplexNoise是已有的工具类
// SimplexNoise.noise(x, y) 生成Perlin噪声值
public class TargetBasedWanderAI {
    private Vector2 spawnPoint; // AI的起始生成点
    private Vector2 currentPosition; // AI当前位置
    private Vector2 targetWanderPosition; // 动态目标漫游点
    private float wanderMaxDistance = 100.0f; // 目标点相对于spawnPoint的最大偏移距离
    private float movementStrength = 0.5f; // AI移动到目标点的速度/力量
    // Perlin噪声的种子,用于确保不同AI的漫游模式不同
    private float noiseSeedX;
    private float noiseSeedY;
    public TargetBasedWanderAI(Vector2 spawnPoint, Vector2 initialPosition) {
        this.spawnPoint = spawnPoint;
        this.currentPosition = initialPosition;
        this.targetWanderPosition = spawnPoint.cpy(); // 初始时目标点在spawnPoint
        // 为Perlin噪声生成独特的种子,例如基于spawnPoint的坐标
        this.noiseSeedX = spawnPoint.x * 0.1f + 123.45f;
        this.noiseSeedY = spawnPoint.y * 0.1f + 678.90f;
    }
    /**
     * 每帧或每隔一段时间更新目标漫游点。
     */
    public void updateTargetWanderPoint() {
        // 使用时间作为Perlin噪声的一个维度,确保连续变化
        // System.currentTimeMillis() 提供一个随时间变化的数值
        float timeComponent = (System.currentTimeMillis() % 1以上就是AI漫游行为优化:实现加权回归起始点机制的详细内容,更多请关注php中文网其它相关文章!
 
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
 
                 
                                
                                 收藏
收藏
                                                                             
                                
                                 收藏
收藏
                                                                             
                                
                                 收藏
收藏
                                                                            Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号