Coding Assignment #7 – Week 9

Concepts:

For this week’s assignment, I tried to implement a cloud of particles, each inspired by Craig Reynolds’s autonomous agents, that seek invisible and randomly generated vertices. I wanted to see how these agents work together, figure out the best path, and adapt to the changing environment. In terms of technicalities, I’m using a seek function and acceleration to drift toward the vertices one by one, and the HSB color mode for aesthetics.

Sketch:

Process:

View post on imgur.com

View post on imgur.com

View post on imgur.com

Code Walkthrough:

Vehicle Class:

The vehicle class represents the autonomous agents (cloud of particles), and has one movement function alongside the constructor. It uses position, velocity, acceleration, direction, and color properties.

Move():

The move() function combines the seeking force and other helper functions (finding the next vertex index, checking if the particle reaches the vertex, changing direction)

move() {
  // Go to the next vertex
  let target = this.vertices[this.idx];
  let distance = dist(this.position.x, this.position.y, target.x, target.y);
        
  if (distance < 45) {
    if ((this.direction==1 && this.idx==this.vertices.length-1) || (this.direction==-1 && this.idx==0)) {
    this.direction*= -1;
    }
    this.idx+=this.direction;
  }

  // Seek the next vertex
  if (distance >1) {
    let steer = new p5.Vector(target.x, target.y);
    steer.sub(this.position);
    steer.normalize();
    steer.mult(0.045);
    this.acceleration.add(steer);
  }
        
  // Add movement
  this.velocity.mult(0.99);
  this.velocity.add(this.acceleration);
  this.velocity.limit(6);
  this.position.add(this.velocity);
  this.acceleration.mult(0);
}

Upon every mouseclick, 1000 new particles are generated and the vertices change as well.

Vertices:

To generate the vertices, I’m using a random function:

vertices=[];
for (let i = 0; i < 6; i++) {
      let x = map(i, 0, 6, width/4, width - width/4);
      let y = random(120, height - 120);
      vertices.push(new p5.Vector(x, y))
}

HSB Color Mode:

I’m using the Hue specifically and brightness to add an aesthetic look to the particles.

Next Steps:

Future steps would be to add other forces i.e. avoiding obstacles.

Leave a Reply

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