Update on Asteroid & Boids
Previous version
- move asteroid with mouse
- let boids have an “idle” and “seek” state based on a certain boundary
- map every parameter into sliders
Asteroid_v2

- switched from moving by arrow keys to seeking mouse position
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);
}

- switched logic to arrive instead of seek
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);
}

- copied the heading logic of boid.js in order to make the asteroid face mouse all the time
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();
}