Concept:
The core idea of this assignment is to create a realistic Fly Spray simulation where:
- Flies are attracted to food when it appears on the canvas.
- Flies flee from a spray can when it gets too close.
- The spray can follows the mouse cursor and sprays a burst when flies get too close.
To make this happen, I used steering behaviors, a concept popularized by Craig Reynolds, to model realistic movement. Steering behaviors are all about adjusting an object’s acceleration to achieve goals like fleeing, seeking, and arriving.
Embedded Sketch:
The code for this fly spray simulation focuses on three main parts: flies, food, and the fly spray. Each of these parts has its own behaviors and interactions, using a coding technique called steering behaviors.
The flies move randomly but change direction based on what’s nearby. If food appears within a certain range, the flies move toward it using an attraction behavior. However, if the fly spray gets too close, the flies switch to a fleeing behavior, quickly steering away to escape. This creates a realistic effect of flies avoiding the spray while seeking food.
The food appears randomly on the canvas for a limited time, attracting nearby flies. It disappears after a while and then reappears at a new spot. This makes the flies constantly adjust their paths, adding excitement to the simulation.
The fly spray follows the mouse using a smooth arrive behavior and sprays when flies are within a certain range. The spray creates a visual burst when close to flies, simulating a spraying action. The code uses a loop to constantly check distances between the flies, food, and spray, adjusting their movements to create a lively and interactive simulation.
Code Snippet:
Here’s the part of the code where I handled the spray effect when flies come near the fly spray:
// Spray effect when close to a fly
sprayEffect(flyPos) {
let distance = dist(this.pos.x, this.pos.y, flyPos.x, flyPos.y);
if (distance < sprayRadius) {
push();
translate(this.pos.x, this.pos.y);
rotate(this.vel.heading());
noStroke();
fill('rgba(0, 0, 0, 0.5)'); // Black spray effect with transparency
triangle(10, -60, 60, -100, 60, -20); // Bigger spray burst
pop();
}
}
I’m particularly proud of this snippet because it brings the fly spray to life! The spray burst animates when flies come too close, making the simulation feel more realistic and interactive. It’s a simple but satisfying visual element that adds a lot of character to the spray can.
Challenges Faced:
Like any coding project, this one came with its fair share of challenges:
- Ensuring Consistent Spray Behavior: Initially, the spray effect wasn’t triggering consistently, and it sometimes missed flies that were within range. I had to fine-tune the logic and adjust the proximity checks to make sure the spray worked every time.
- Balancing Attraction and Repulsion: Making the flies switch seamlessly between attraction (to food) and repulsion (from the spray) required careful control of their acceleration and speed. It was tricky to get the right balance so that the flies didn’t act erratically.
- Managing Timing: The food appears randomly and disappears after a set duration, which added an extra layer of complexity. I used a timer to handle the food’s lifecycle, but it took some tweaking to get it to behave consistently.
Future Improvements:
There’s always room for improvement! Here are some ideas I’d love to implement in the future:
- More Realistic Fly Movements: Right now, the flies have basic behaviors. Adding more randomness and making them swarm together around food could add realism.
- Better Spray Animation: The spray burst is a simple triangle, but I’d love to add particles or make it look more like a mist.
- Multiple Food Sources: Currently, only one food source appears at a time. Adding multiple food sources that attract flies in different directions would make the simulation more dynamic.
- Scoring System: Adding a scoring system could make the simulation more game-like, where users earn points for getting close to the flies.