Update on Asteroid & Boids

Previous version

Asteroid_v2

Screen Recording 2025-03-13 at 8.56.30 AM.gif

    seek(target) {
      let desired = p5.Vector.sub(target, this.position);
      let distance = desired.mag();
      let speed = map(distance, 0, width, this.topspeed, 0); // Adjust speed based on distance
      desired.setMag(speed);
      let steer = p5.Vector.sub(desired, this.velocity);
      steer.limit(this.maxforce);
      this.applyForce(steer);
    }

Screen Recording 2025-03-13 at 8.58.48 AM.gif

    arrive(target) {
      let desired = p5.Vector.sub(target, this.position);
      let d = desired.mag();
      if (d < 100) {
        let m = map(d, 0, 100, 0, this.topspeed);
        desired.setMag(m);
      } else {
        desired.setMag(this.topspeed);
      }
      let steer = p5.Vector.sub(desired, this.velocity);
      steer.limit(this.maxforce);
      this.applyForce(steer);
    }

Screen Recording 2025-03-13 at 10.19.40 AM.gif

show() {
      // Draw a triangle rotated in the direction of velocity
      let angle = this.velocity.heading();
      fill(255);
      noStroke();
      push();
      translate(this.position.x, this.position.y);
      rotate(angle + PI / 2);
      beginShape();
      vertex(0, -this.r * 2);
      vertex(this.r, this.r * 1.5);
      vertex(0, this.r);
      vertex(-this.r, this.r * 1.5);
      endShape(CLOSE);
      pop();
    }