Hello. I’m working on a project using Peter Lager’s AI for 2D games library, and tried to make a program to see if I understood how the library is used, since I am new to the concept. The program would make a world with obstacles with a wandering entity in it which would aimlessly wander around the screen, avoiding the obstacles. That worked.
Then I wanted to add a second entity, which would pursue the first one(Which would hide from it). All seemed well until I ran the new program and got a blank gray screen, which immediately crashed. I looked at the program but the only hint I could find was the error NullPointerException on lines 97 & 99. Can anybody explain this?
Here’s the code;
import game2dai.*;
import game2dai.entities.*;
import game2dai.entityshapes.*;
import game2dai.entityshapes.ps.*;
import game2dai.fsm.*;
import game2dai.graph.*;
import game2dai.maths.*;
import game2dai.steering.*;
import game2dai.utils.*;
int[] obs = new int[] { //obstacles
100, 100, 36,
200, 200, 32,
270, 70, 16,
380, 180, 37,
510, 110, 27,
520, 210, 23,
400, 80, 10,
90, 240, 15,
20, 50, 20
};
World world;
Domain wd;
Vehicle tank; //hiding tank
Vehicle tank2; //pursuing tank
BitmapPic view; //hiding tank
BitmapPic view2; //pursuing tank
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[i], obs[i+1]), // position
obs[i+2] // collision radius
);
obstacle.renderer(obView);
world.add(obstacle);
}
// Create the entity that's going to hide
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
);
PersonPic view = new PersonPic(this); //hiding entity icon
view.showHints(Hints.HINT_HEADING | Hints.HINT_VELOCITY | Hints.HINT_OBS_AVOID | Hints.HINT_WANDER);
tank.renderer(view);
tank.AP().obstacleAvoidOn().wanderOn().hideOn();
tank.AP().wanderFactors(90, 20, 20);
tank.AP().obstacleAvoidDetectBoxLength(35);
tank.worldDomain(wd, SBF.REBOUND);
world.add(tank);
//Create the entity that's going to pursue the other one
tank2 = new Vehicle(
new Vector2D(100,40), // 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
);
ArrowPic view2 = new ArrowPic(this); //pursuing entity icon
view2.showHints(Hints.HINT_HEADING | Hints.HINT_VELOCITY | Hints.HINT_OBS_AVOID | Hints.HINT_WANDER | Hints.HINT_VIEW);
tank2.renderer(view);
tank2.AP().obstacleAvoidOn().wanderOn().pursuitOn(tank);
tank2.AP().wanderFactors(90, 20, 20);
tank2.AP().obstacleAvoidDetectBoxLength(35);
tank2.worldDomain(wd, SBF.REBOUND);
world.add(tank2);
}
public void draw() {
double elapsedTime = sw.getElapsedTime();
// Animate the tank image
float speed = (float) tank.speed();
float maxSpeed = (float) tank.maxSpeed();
if (speed > 1) {
float newInterval = map(speed, 0, maxSpeed, 0.6f, 0.04f);
view.setAnimation(newInterval, 1);
}
else {
view.pauseAnimation(); //line 97
}
world.update(elapsedTime); //line 99
background(220, 255, 220);
world.draw(elapsedTime);
}