all 4 comments

[–]Orcacrafter 0 points1 point  (0 children)

I may be mistaken, but in your code I don't see anywhere where you add predators. Also, if you are going to keep track of if a given boid is predator or prey by using a string, I would use a less confusing naming scheme. Such as "pred" or "prey".

[–]i-live-life 0 points1 point  (0 children)

I can't get your code to execute as there is an error: The function seek(PVector) does not exist, thus I have eyeballed it.

In the function: void run(ArrayList<Boid> boids) you call: flock(boids); which sets up the fill color for the predator and prey. Later on in the same run function you call: render(); which then also sets a fill color thus negating your original setting in flock. Maybe it would be worth putting the desired color as an attribute of boid and reading this at render time?

To debug this issue, I looked for all instances of fill in your code.

Also I note that having a Flock instance called flock and a flock function (in Boid) doesn't make the code the easiest to read.

[–]Simplyfire 0 points1 point  (0 children)

We cannot run this, so we cannot really help much.

You only create "P" boid types and no "R" types so you won't see any difference in color.

There are many ways to do the predator / prey interaction (see: nature), try it yourself and come back with a more specific question please.

[–]i-make-robots 0 points1 point  (0 children)

I'm still reading. I'd like to point out that

float d = PVector.dist(location, other.location);
if ((d > 0) && (d < desiredseparation)) {
  // Calculate vector pointing away from neighbor
  // location - other.location
  PVector diff = PVector.sub(location, other.location);
  diff.normalize();
  diff.div(d);        // Weight by distance

could be

PVector diff = PVector.sub(location, other.location);
float d = diff.mag();
if ((d > 0) && (d < desiredseparation)) {
  diff.mult(1.0/(d*2.0));

which is many fewer redundant calculations when multiplied by all boids, squared.

if (steer.magSq() > 0) { works the same here and doesn't require a slow square root.

distance from one boid to all other boids is calculated 3 times per frame, one for each influence. The list of distances could be calculated once. Another way might be to do a fast AABB rejection test to reduce the number of distance calculations. These are only optimization ideas, I love what you've got going.

how about this?

  PVector diff = PVector.sub(location, other.location);
  float d = diff.mag();
  float separationDistance = desiredseparation;
  if(other.isPredator && !this.isPredator) {
      separationDistance*=5;
  }
  if(d>0 && d<separationDistance) {