Assignment Week #9 – Flocking Simulation with Obstacle Avoidance

Concept:

In this project, I aimed to create an immersive simulation that showcases the collective behavior of a group of boids, as they navigate through an environment that can be filled with obstacles. The focus was to mimic the natural flocking behavior observed in birds or fish, incorporating obstacle avoidance to enhance the realism of their movement.

Implementation:

  • Boid Behavior: Each boid follows three primary rules – alignment, cohesion, and separation – to simulate natural flocking behavior.
  • Dynamic Interaction: Boids react to user input, moving towards the mouse pointer when a specific key is pressed. Pressing ‘a’ makes the boids get attracted toward the cursor. In addition, when clicking on two points on the canvas, it spawns a line that acts as an obstacle for the boids.
  • Obstacle Avoidance: Boids dynamically detect and steer clear of line obstacles in their environment, enhancing the realism of their movement patterns.
  • Visual Aesthetics: The boids change colors dynamically, creating a visually captivating display that reflects their speed and movement.

Sketch:

 

Code:

display() {
  // Calculate the size based on velocity
  let speed = this.velocity.mag();
  let size = map(speed, 0, this.maxSpeed, 2, 10); // Map speed to a reasonable size range

  // Calculate hue for color
  let time = millis() / 1000;
  let timeFactor = (sin(time + this.color) + 1) / 2;
  let velFactor = speed / this.maxSpeed;
  let hue = (this.color + 360 * timeFactor + 180 * velFactor) % 360;

  // Set color and size
  colorMode(HSB, 360, 100, 100);
  stroke(hue, 80, 90);
  strokeWeight(size);

  // Draw the boid
  push();
  translate(this.position.x, this.position.y);
  point(0, 0);
  pop();

  colorMode(RGB, 255);
}
  • The steering forces that direct the boids’ movement towards alignment, cohesion, and separation as well as additional forces for obstacle avoidance and attraction towards the mouse pointer.
avoidObstacles(obstacles) {
  let steer = createVector(0, 0);
  let count = 0;
  let safeDistance = 30; // Increase safe distance for early avoidance

  obstacles.forEach(obstacle => {
    let closestPoint = this.closestPointOnLine(this.position, obstacle.start, obstacle.end);
    let d = p5.Vector.dist(this.position, closestPoint);

    if (d < safeDistance) {
      let diff = p5.Vector.sub(this.position, closestPoint);
      diff.normalize();
      diff.div(d); // Stronger weighting by distance
      steer.add(diff);
      count++;
    }
  });

  if (count > 0) {
    steer.div(count);
    steer.setMag(this.maxSpeed * 1.5); // Increase the magnitude of the steering force
    steer.sub(this.velocity);
    steer.limit(this.maxForce * 1.5); // Increase the maximum force limit
  }

  return steer;
}
  • Obstacles are represented as lines, and a specialized algorithm calculates the closest point on these lines to each boid, triggering avoidance maneuvers.

Challenges:

  • Obstacle Avoidance Algorithm: Crafting an effective method for boids to avoid line-shaped obstacles requires careful consideration of the forces required to allow the boids to avoid obstacles properly.

Future Improvements:

  • Enhanced User Interaction: Exploring more complex user interactions, such as allowing users to create and modify obstacles in real time. making the obstacles possibly move or experience forces.
  • Environmental Complexity: Adding more environmental elements like varying wind currents or areas of attraction and repulsion could create more dynamic scenarios for the boids.

Leave a Reply

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