Coding Assignment #9 – Nematodes

Concept & Inspiration:

For this week’s assignment, I tried to recreate a microscopic view of nematodes (parasitic species that feed on plants and are found in soil environments) using flocking behaviors (alignment, cohesion, separation). Nematodes have smooth unsegmented bodies similar to worms, as shown in the following picture:

View post on imgur.com

In the sketch, there is a circle in the middle representing the view from the microscope oculars (eyepieces), and starts with a smooth fade in animation as if the light of the microscope was turned on.

View post on imgur.com

Sketch:

Code Walkthrough:

https://editor.p5js.org/bdr/sketches/n0c4DAM2S

Nematode (vehicle) Class:

Flocking – Separation: to cause nematodes to steer away from all the others.

separate() {
    let desiredSeparation = 25;
    let steer = createVector(0, 0);
    let count = 0;

    for (let i = 0; i < Nematodes.length; i++) {
      let d = p5.Vector.dist(this.position, Nematodes[i].position);
      if (d > 0 && d < desiredSeparation) {
        let diff = p5.Vector.sub(this.position, Nematodes[i].position);
        diff.normalize();
        diff.div(d);
        steer.add(diff);
        count++;
      }
    }

    if (count > 0) {
      steer.div(count);
    }

    if (steer.mag() > 0) {
      steer.normalize();
      steer.mult(this.maxspeed);
      steer.sub(this.velocity);
      steer.limit(this.maxforce);
    }
    return steer;
  }

Flocking – Cohesion: to cause nematodes to steer towards the center of mass – the average position of the nematodes within the circle.

// Flocking - Cohesion
  cohere() {
    let neighborDistance = 50;
    let sum = createVector(0, 0);
    let count = 0;

    for (let i = 0; i < Nematodes.length; i++) {
      let d = p5.Vector.dist(this.position, Nematodes[i].position);
      if (d > 0 && d < neighborDistance) {
        sum.add(Nematodes[i].position);
        count++;
      }
    }

    if (count > 0) {
      sum.div(count);
      return this.seek(sum);
    } else {
      return createVector(0, 0);
    }
  }

Flocking – Alignment: to cause the nematodes to line up with others close by.

align() {
    let neighborDistance = 40;
    let sum = createVector(0, 0);
    let count = 0;

    for (let i = 0; i < Nematodes.length; i++) {
      let d = p5.Vector.dist(this.position, Nematodes[i].position);
      if (d > 0 && d < neighborDistance) {
        sum.add(Nematodes[i].velocity);
        count++;
      }
    }

    if (count > 0) {
      sum.div(count);
      sum.normalize();
      sum.mult(this.maxspeed);
      let steer = p5.Vector.sub(sum, this.velocity);
      steer.limit(this.maxforce);
      return steer;
    } else {
      return createVector(0, 0);
    }
  }

Trail: FrameRate animation: To get a smooth animation, I used frame rates.

frameRate(30)
// Add a trail
if (frameCount % 20 == 0) {
  fill(160, 160, 45, 15);
}
ellipse(width/2, height/2, 440, 440);
Improvements:

One of the next steps would be to add user interactions using the mouse, i.e. creating an obstacle when the user clicks within the bounds of the circle, add food to the nematodes to consume that would help increase their speed/behavior…

Leave a Reply

Your email address will not be published. Required fields are marked *