here's the entire configuration:
StopWatchTimer sw; // a timer-clock implementation for time-triggering events
Work wanderer; // a simple wondering agent
Agent01 capsule; // male, worker agent
void setup() {
size(300,300);
wanderer = new Work(random(width),random(height));
capsule = new Agent01(random(width),random(height));
smooth();
println (millis());
sw = new StopWatchTimer();
sw.start();
}
void draw() {
background(255);
time();
wanderer.wander();
wanderer.run();
capsule.wander();
capsule.run();
while (sw.second()>=30 && sw.second()<=60) {
PVector mouse = new PVector(mouseX, mouseY);
capsule.seek(mouse);
}
}
void time() {
background(#FFFFFF);
fill(#000000);
textAlign(CENTER);
text(nf(sw.hour(), 2)+":"+nf(sw.minute(), 2)+":"+nf(sw.second(), 2)+":"+nf(sw.hundrensec(), 2), 260, 290);
}
======================================================================================
class Agent01 {
PVector location;
PVector velocity;
PVector acceleration;
float r;
float wandertheta;
float maxforce; // Maximum steering force
float maxspeed; // Maximum speed
Agent01(float x, float y) {
acceleration = new PVector(0,0);
velocity = new PVector(0,0);
location = new PVector(x,y);
r = 6;
wandertheta = 0;
maxspeed = 2;
maxforce = 0.05;
}
void run() {
update();
borders();
display();
}
// Method to update location
void update() {
// Update velocity
velocity.add(acceleration);
// Limit speed
velocity.limit(maxspeed);
location.add(velocity);
// Reset accelertion to 0 each cycle
acceleration.mult(0);
}
void wander() {
float wanderR = 25; // Radius for our "wander circle"
float wanderD = 80; // Distance for our "wander circle"
float change = 0.3;
wandertheta += random(-change,change); // Randomly change wander theta
// Now we have to calculate the new location to steer towards on the wander circle
PVector circleloc = velocity.get(); // Start with velocity
circleloc.normalize(); // Normalize to get heading
circleloc.mult(wanderD); // Multiply by distance
circleloc.add(location); // Make it relative to boid's location
float h = velocity.heading2D(); // We need to know the heading to offset wandertheta
PVector circleOffSet = new PVector(wanderR*cos(wandertheta+h),wanderR*sin(wandertheta+h));
PVector target = PVector.add(circleloc,circleOffSet);
seek(target);
}
void applyForce(PVector force) { // Newton's second law; We could add mass here if we want A = F / M
acceleration.add(force);
}
// A method that calculates and applies a steering force towards a target
// STEER = DESIRED MINUS VELOCITY
void seek(PVector target) {
PVector desired = PVector.sub(target,location); // A vector pointing from the location to the target
desired.normalize(); // Normalize desired and scale to maximum speed
desired.mult(maxspeed);
PVector steer = PVector.sub(desired,velocity); // Steering = Desired minus Velocity
steer.limit(maxforce); // Limit to maximum steering force
applyForce(steer);
}
void display() {
// Draw an ellipse that represents an agent-capsule
fill(175);
stroke(0);
pushMatrix();
translate(location.x, location.y);
ellipse(0, 0, r, r);
popMatrix();
}
// Wraparound
void borders() {
if (location.x < -r) location.x = width+r;
if (location.y < -r) location.y = height+r;
if (location.x > width+r) location.x = -r;
if (location.y > height+r) location.y = -r;
}
}
======================================================================================
// class timer
class StopWatchTimer {
int startTime = 0, stopTime = 0;
boolean running = false;
void start() {
startTime = millis();
running = true;
}
void stop() {
stopTime = millis();
running = false;
}
int getElapsedTime() {
int elapsed;
if (running) {
elapsed = (millis() - startTime);
}
else {
elapsed = (stopTime - startTime);
}
return elapsed;
}
// And add in the class before int second() this :
int hundrensec()
{
return (getElapsedTime() / 10) % 100;
}
int second() {
return (getElapsedTime() / 1000) % 60;
}
int minute() {
return (getElapsedTime() / (1000*60)) % 60;
}
int hour() {
return (getElapsedTime() / (1000*60*60)) % 24;
}
}
======================================================================================
class Work {
PVector location;
PVector velocity;
PVector acceleration;
float r;
float wandertheta;
float maxforce; // Maximum steering force
float maxspeed; // Maximum speed
Work(float x, float y) {
acceleration = new PVector(0,0);
velocity = new PVector(0,0);
location = new PVector(x,y);
r = 6;
wandertheta = 0;
maxspeed = 2;
maxforce = 0.05;
}
void run() {
update();
borders();
display();
}
// Method to update location
void update() {
// Update velocity
velocity.add(acceleration);
// Limit speed
velocity.limit(maxspeed);
location.add(velocity);
// Reset accelertion to 0 each cycle
acceleration.mult(0);
}
void wander() {
float wanderR = 25; // Radius for our "wander circle"
float wanderD = 80; // Distance for our "wander circle"
float change = 0.3;
wandertheta += random(-change,change); // Randomly change wander theta
// Now we have to calculate the new location to steer towards on the wander circle
PVector circleloc = velocity.get(); // Start with velocity
circleloc.normalize(); // Normalize to get heading
circleloc.mult(wanderD); // Multiply by distance
circleloc.add(location); // Make it relative to boid's location
float h = velocity.heading2D(); // We need to know the heading to offset wandertheta
PVector circleOffSet = new PVector(wanderR*cos(wandertheta+h),wanderR*sin(wandertheta+h));
PVector target = PVector.add(circleloc,circleOffSet);
seek(target);
}
void applyForce(PVector force) {
// We could add mass here if we want A = F / M
acceleration.add(force);
}
// A method that calculates and applies a steering force towards a target
// STEER = DESIRED MINUS VELOCITY
void seek(PVector target) {
PVector desired = PVector.sub(target,location); // A vector pointing from the location to the target
// Normalize desired and scale to maximum speed
desired.normalize();
desired.mult(maxspeed);
// Steering = Desired minus Velocity
PVector steer = PVector.sub(desired,velocity);
steer.limit(maxforce); // Limit to maximum steering force
applyForce(steer);
}
void display() {
// Draw an ellipse that represents an agent-capsule
fill(255,0,0);
stroke(0);
pushMatrix();
translate(location.x, location.y);
ellipse(0, 0, r, r);
popMatrix();
}
// Wraparound
void borders() {
if (location.x < -r) location.x = width+r;
if (location.y < -r) location.y = height+r;
if (location.x > width+r) location.x = -r;
if (location.y > height+r) location.y = -r;
}
}
=====================================================================================
hope it gets clearer now :)