Adding aging to boids simulation

58 Views Asked by At

I am working on expanding this sketch: http://www.openprocessing.org/sketch/11045

Trying to add aging to boids agents using frameCount. I initialise ArrayList with age inbuilt:

boids = new ArrayList();
for (int i = 0; i < boidNum; i++) {
  Agent boid = new Agent(random(width), random(height), 1, round(frameCount + random(300, 400)));
  boids.add(boid);
}

Then retrieve it :

Agent(float posX, float posY, int t, int a) {
mass = 5.0;
location = new PVector(posX, posY);
vel = new PVector(random(-5,5), random(-5, 5));
acc = new PVector();
type = t;
wdelta = 0.0;
action = 0;
age = a;
}

I want to use something like this for the living cycle :

if (frameCount != age) {
  age = age - 1;
}
if (frameCount == age) {
  boids.remove(this);
}

But I'm not sure where in the code I should put it. Also is this the best way to do it, or am I overcomplicating things?

Update: I wrote a new method:

void boid(ArrayList boids) {
  for (int i = 0; i < boids.size(); i++) {
    if (frameCount >= age) {
      boids.remove(this);
    }
  }
}

which is being called from:

void steer(ArrayList boids, ArrayList predators, ArrayList landscape) {
  if (type == 1) boid(boids); ...
1

There are 1 best solutions below

1
On

It sounds like you would want to put that code in the Agent class, after you do the updating and drawing of the Agent- taking a quick look at the code, that's probably the run() function in the Agent class.

But I'm not totally sure why you're comparing each Agent's age with the frameCount. The frameCount variable just tells you how long the sketch has been running. You if statement kills any birds that have the same age as the sketch, which doesn't make any sense.

Instead, you need to have two variables in your Agent class: the age variable that starts at 0 and increments by one each frame, and a maxAge variable that stores the age at which the Agent should be removed.

If you want some friendly advice though, I'd really recommend starting over from scratch with your own code instead of trying to modify an existing one, especially if you aren't really sure how the code works yet. It might seem like you're saving time by using existing code, but if you don't really know how code works yet, you'll definitely save yourself a bunch of headaches by writing it yourself. Up to you though.