yangfanconan 发表于 2013-10-14 14:40:55

Processing之旅-----【好玩的库之AI_for_2D_Games5】

下面说一下,避障的功能,其实避障的功能首先一个条件是什么?有障碍。
import game2dai.entities.*;
import game2dai.entityshapes.ps.*;
import game2dai.maths.*;
import game2dai.*;
import game2dai.entityshapes.*;
import game2dai.fsm.*;
import game2dai.steering.*;
import game2dai.utils.*;
import game2dai.graph.*;

// ObstacleAvoid_01.pde
int[] obs = new int[] {
100, 100, 36,
200, 200, 32,
270, 70, 16,
380, 180, 37,
510, 110, 27,
520, 210, 23,
400, 80, 10,
90, 240, 6
};
World world;
Domain wd;
Vehicle tank;
BitmapPic view;

StopWatch sw = new StopWatch();

public void setup() {
size(600, 300);
world = new World(width, height);
wd = new Domain(0, 0, width, height);

ObstaclePic obView = new ObstaclePic(this, color(0, 96, 0), color(0), 3);
for (int i = 0; i < obs.length; i += 3) {
    Obstacle obstacle = new Obstacle(
    new Vector2D(obs, obs), // position
    obs // collision radius
    );
    obstacle.renderer(obView);
    world.add(obstacle);
}

// Create the tank
tank = new Vehicle(
new Vector2D(width/2, height/2), // position
16, // collision radius
new Vector2D(0, 0), // velocity
60, // maximum speed
new Vector2D(1, 1), // heading
7, // mass
1.5f, // turning rate
1000 // max force
);
view = new BitmapPic(this, "tanks32_4.png", 8, 1, 0);
view.showHints(Hints.HINT_HEADING | Hints.HINT_VELOCITY | Hints.HINT_OBS_AVOID);
tank.renderer(view);
tank.AP().obstacleAvoidOn().wanderOn();
tank.AP().wanderFactors(60, 40, 10);
tank.AP().obstacleAvoidDetectBoxLength(45);
tank.worldDomain(wd, SBF.WRAP);
world.add(tank);
}

public void draw() {
double elapsedTime = sw.getElapsedTime();
// Animate the tank image
float speed = (float) tank.speed();
float maxSpeed = (float) tank.maxSpeed();
if (speed > 1) {
    int newInterval = (int) map(speed, 0, maxSpeed, 600, 40);
    view.interval(newInterval);
}
else {
    view.interval(0);
}
world.update(elapsedTime);
background(220, 255, 220);
world.draw();
}

弘毅 发表于 2013-10-14 21:06:43

这个。。这个。。。都开始避障了。。。厉害

bacon6581 发表于 2013-10-15 09:30:07

niubility!
页: [1]
查看完整版本: Processing之旅-----【好玩的库之AI_for_2D_Games5】