Auto-pilot control
In the Getting Started guide we created an autonomous agent using the MovingEnity class. This type of agent does not use any form of AI to control it's movement, once it has been given a velocity it will move without further intervention from the user.
There is another type of autonomous agent that uses AI to control its movement through the domain. This type of agent is created from the Vehicle class, it can still do everything the MovingEntity agent does, but it also has an AutoPilot object to provide the AI control.
The auto-pilot can choose from 16 different steering behaviours working either individually or in combination to create the wanted motion.
There are many instances where we might want an agent to simply wander across the domain. In this sketch the auto-pilot implements the wander behaviour.
// AutoPilot_01.pde
World world;
StopWatch sw;
Vehicle mover0;
public void setup() {
size(480, 320);
world = new World(width, height);
sw = new StopWatch();
// Create the mover
mover0 = new Vehicle(
new Vector2D(width/2, height/2), // position
15, // collision radius
new Vector2D(15, 15), // velocity
40, // maximum speed
new Vector2D(1, 1), // heading
1, // mass
0.5, // turning rate
200 // max force
);
// Start wandering
mover0.AP().wanderOn().wanderFactors(60, 30, 20);
// What does this mover look like
ArrowPic view = new ArrowPic(this);
// Show wander behaviour hints
view.showHints(Hints.HINT_WANDER);
mover0.renderer(view);
// Constrain movement
Domain d = new Domain(0, 0, width, height);
mover0.worldDomain(d, SBF.WRAP);
// Finally we want to add this to our game domain
world.add(mover0);
sw.reset();
}
public void draw() {
double elapsedTime = sw.getElapsedTime();
world.update(elapsedTime);
background(200, 255, 200);
world.draw(elapsedTime);
}
The agent mover0 is now created from the Vehicle class - lines 4 and 11. Notice that the expected parameters (lines 12-19) are the same as those expected by the MovingEntity agent.
In line 26 we have asked to see the hints for the wander behaviour.
The key is line 22 where we switch on the wander behaviour and set the factors used to calculate the wander steering force.
mover0.AP().wanderOn().wanderFactors(60, 30, 20);
NOTE: All the steering behaviour AI is in the library class AutoPilot, you should study the reference to find out all the available methods. All Vehicles have an attribute of type AutoPilot.
The first part of line 22 mover0.AP()
returns the
auto-pilot object in control of this vehicle. This is the only way
we can access the agent's AutoPilot object since this is needed to
change the steering behaviours.
The next part, .wanderOn()
not only switches on the
wander behaviour but returns the SB attribute.
Most methods in the SB class also return the SB attribute so we can chain method calls.
The final part, .wanderFactors(60, 30, 20)
sets the
tweak factors for this behaviour. See the library guide for wander
for more details.
When we want the agent to stop wandering then we can do that with
mover0.AP().wanderOff();
Auto-pilot with wander behaviour switched on