I want to create a “pulse” effect after objects collide with each other. The intended effect in this example is that when the particles collide (come within a certain distance from each other) an ellipse appears, grows to a specified radius, then disappears. The ellipse x, y coordinates will be the same as the particle’s (ellipse moves with the particle). And this animation should occur as a trigger - once triggered, the distance between particles should no longer matter.
In the code below the effect is not what I’m intending. Instead of the particle continuing it’s trajectory at the same velocity with an ellipse growing from the center, the particle slows down, an ellipse appears for the time that the two particles are near each other, then disappears.
Here’s a sample:
ArrayList<Particle> particles;
void setup() {
size(400, 400);
particles = new ArrayList<Particle>();
int numparticles = 2;
for (int i=0; i < numparticles; i++) {
float initX = random(0, width);
float initY = random(0, height);
Particle particle = new Particle(initX, initY);
particles.add(particle);
}
}
void draw() {
background(0);
// There's a much cleaner way to do this, but showing this
// way to illustrate the example.
Particle particle1 = particles.get(0);
Particle particle2 = particles.get(1);
// Make the particles seek each other
particle1.seek(particle2.pos);
particle2.seek(particle1.pos);
// Update position and display
particle1.update();
particle1.display();
particle2.update();
particle2.display();
// NEED HELP: Attempt at interaction here.
for (Particle particle : particles) {
particle.interact(particles,25);
}
}
class Particle {
PVector pos;
PVector velocity;
PVector acceleration;
float maxforce;
float maxspeed;
float r;
boolean interacting;
Particle(float x, float y) {
acceleration = new PVector(0, 0);
velocity = new PVector(0, 0);
pos = new PVector(x, y);
maxspeed = 4.0;
maxforce = 0.03;
r = 15;
interacting = false;
}
void applyForce(PVector force) {
acceleration.add(force);
}
void seek(PVector target) {
PVector desired = PVector.sub(target, pos);
desired.normalize();
desired.mult(maxspeed);
PVector steer = PVector.sub(desired, velocity);
steer.limit(maxforce);
applyForce(steer);
}
// NEED HELP HERE:
// if the particles come within a certain distance
// then interact by pulsing a circle.
// The pulse effect should be a circle that grows until
// it reaches a set radius, then disappears.
// The pulsing circle will be centered at the particle's
// x,y coordinates at each step.
void interact(ArrayList<Particle> particles, float radius) {
for (Particle other : particles) {
float iradius = 0.0;
float d = PVector.dist(pos, other.pos);
// trigger the interaction.
if ((d > 0) && (d <= radius)) {
interacting = true;
}
// NEED HELP HERE.
// what to do while interacting.
while (interacting) {
if (iradius < radius) {
pushMatrix();
noFill();
stroke(255, 255, 0);
ellipse(pos.x, pos.y, iradius, iradius);
popMatrix();
iradius += 0.01;
} else {
interacting = false;
}
}
}
} // end interact function.
void update() {
velocity.add(acceleration);
velocity.limit(maxspeed);
pos.add(velocity);
acceleration.mult(0);
}
void display() {
fill(255);
ellipse(pos.x, pos.y, r, r);
}
} // end Particle```