Concept
For this project, I wanted to simulate how moths fly around a porch light. I noticed that real bugs are chaotic. They don’t fly in straight lines, and they are all different sizes. I created a system where every moth tries to fly toward the mouse (the light), but they are constantly pushed around by a “jitter” force that makes them flutter. I also gave each moth a specific weight where big moths are heavy and slow to turn, while small moths are hyperactive and fast.
Code Highlight
I chose this function because it acts as the “brain” of the moth. It uses a physics formula to calculate exactly how to steer toward the light. When the user clicks, the code multiplies the allowed acceleration by 5, instantly transforming the moth from a lazy flyer into a chaotic one.
seek(target) {
// 1. Find the direction pointing to the light
let desired = p5.Vector.sub(target, this.pos);
desired.normalize();
// 2. Set the speed: If clicking, double the top speed!
let speedLimit = mouseIsPressed ? this.maxSpeed * 2 : this.maxSpeed;
desired.mult(speedLimit);
// 3. Calculate steering (Desired velocity minus Current velocity)
let steer = p5.Vector.sub(desired, this.vel);
// 4. Limit the turning power: If clicking, allow 5x more force
let currentForceLimit = mouseIsPressed ? this.maxForce * 5 : this.maxForce;
steer.limit(currentForceLimit);
// 5. Apply this steering force
this.applyForce(steer);
}
Reflection and Future Work
I was surprised by how realistic the movement felt, especially when the moths reach the light source and begin orbiting around it. It created a very organic, feeling without complex animation code. For future improvements, I would like somehow make them bump into each other so they don’t overlap.
