ArrayList particles = new ArrayList();
for (int i = 0; i < 25; i++) {
particles.add(new Particle(new PVector(random(width), height/2), PVector.random2D(), 25));
for (int i = 0; i < particles.size(); i++) {
Particle p = (Particle) particles.get(i);
for (int i = 0; i < particles.size(); i++) {
Particle p = (Particle) particles.get(i);
Particle(PVector location_, PVector velocity_, float radius_) {
void boundaryConstraints() {
if (location.x < radius) {
if (location.x > width-radius) {
location.x = width-radius;
if (location.y < radius) {
if (location.y > height-radius) {
location.y = height-radius;
for (int j = 0; j < particles.size(); j++) {
Particle p = (Particle) particles.get(j);
if (!this.equals(p) && location.dist(p.location) < (radius+p.radius)) {
fill(125 + sin(radians(frameCount))*125, 125 + cos(radians(frameCount))*125, 125 + sin(radians(-frameCount))*125);
ellipse(location.x, location.y, radius*2, radius*2);
void bounce(Particle p, Particle p2) {
PVector direction = PVector.sub(p.location, p2.location);
while (p.location.dist(p2.location) < (p.radius+p2.radius)) {
p.location.add(direction);
PVector u = PVector.sub(p.velocity, p2.velocity);
PVector un = componentVector(u, direction);
p.velocity = PVector.add(u, p2.velocity);
p2.velocity = PVector.add(un, p2.velocity);
PVector componentVector (PVector vector, PVector directionVector) {
directionVector.normalize();
directionVector.mult(vector.dot(directionVector));