Concept
For this week’s assignment I wanted to create fish swimming towards ripples in the water. The ripple would create attraction for the fish, and the fish would slow down as they reached the ripple. I used force and drag to create these effects.
Code Snippet
if (this.target != null) {
let force = p5.Vector.sub(this.target, this.pos); // Force towards target
let dist = force.mag(); // Distance to target
force.setMag(map(dist, 0, 100, 0, 1)); // Adjust force based on distance
this.applyForce(force);
let drag = this.vel.copy().mult(-1.6).mult(0.1); // Create drag force
this.applyForce(drag); // Apply drag to slow down near target
if (dist < 10) this.target = null; // Clear target if close
} else {
// If no target, allow fish to move randomly
// Add slight randomness to velocity
if (this.vel.mag() < 1) {
this.vel.x += random(-0.1, 0.1);
this.vel.y += random(-0.1, 0.1);
}
this.vel.limit(6); // Limit speed to ensure natural movement
}
this.vel.add(this.acc); // Update velocity
this.pos.add(this.vel); // Update position
this.acc.mult(0); // Reset acceleration
Embedded Code
Reflections
If I had more time, I would have the fish flow more seamlessly. Additionally, I think the drag continues to slow the fish down indefinitely, and I would trouble shoot that. I would also try to gamify the sketch a bit more, but having a start screen and an animation of fish food entering the water.