For this week’s assignment, the first thing I thought of when I saw “attractors,” I thought of moths and other insects flying around a light. Right outside my home’s front door is a light that always has bugs flying around it. Whenever I leave or return home, I see them while unlocking my front door. So, I decided I would try to emulate this with a program. It can be seen here:
Code on GitHub here.
The image in the background is the lamp in front of my house. Since I can’t easily get home from Abu Dhabi, I asked my brother to take a picture of it at night, while the light was on. The code is simple, it has a point that is an attractor and then a Moth class that will move towards and away from the light, simulating insects hovering around a light. I also added some random positioning when calculating the movement of the moths, to emulate the erratic way insects sometimes fly. The colors of the moths get lighter or darker depending on the distance from the light, to emulate actual light. I also tried to add wings to the moth that would constantly move. It’s not perfect, but it is at least somewhat animated. The moths also rotate themselves so they are always facing the light. This all can be seen here:
display(light) { noStroke(); let distance = Math.abs(Math.sqrt(Math.pow(this.pos.x-light.pos.x, 2)+Math.pow(this.pos.y-light.pos.y, 2))) let theta = this.vel.heading() + PI / 2; map(distance, 0, height, 50, 200) fill(Math.abs(255-distance)); push() translate(this.pos.x, this.pos.y) rotate(theta); ellipse(0, 0, this.size*0.6, this.size); if (this.counter%2 == 0) { rotate(290) } else { rotate(310) } fill(Math.abs(200-distance)); ellipse(4, -2, this.size*0.8, this.size*0.5); ellipse(-4, -2, this.size*0.8, this.size*0.5); pop() this.counter++ }
If I were to improve this, I think I would try to make the wings more accurate for the moths. Also, I would want to try making the movement better, perhaps adding variable speeds for the insects or allowing them to stop and land on the light, before taking off into flight again.