When objects interact, we can trigger an event or effect to show that an interaction has occurred. We'll use Processing to show how to implement object interactions in a simulation environment.
For this tutorial, we'll create Particle
objects (displayed as ellipses) that move around the screen. When two particles come within a certain distance of each other, an ellipse will pulse from the center of each particle. The ellipse will grow to a specified size, then disappear.
First let's create the base Particle
class with some physics to move around.
class Particle {
PVector pos;
PVector velocity;
PVector acceleration;
float maxforce;
float maxspeed;
float r = 15;
int id;
Particle(int x, int y, int _id) {
accleration = new PVector(0, 0);
velocity = new PVector(0, 0);
pos = new PVector(x, y);
maxspeed = 4.0;
maxforce = 0.03;
id = _id;
} // end Particle constructor
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);
}
void update() {
velocity.add(acceleration);
velocity.limit(maxspeed);
pos.add(velocity);
acceleration.mult(0);
}
void display() {
fill(255);
noStroke();
ellipse(pos.x, pos.y, r, r);
}
} // end Particle class
The main program will look like this:
void setup() {
}
void draw() {
}