This program creates in a ArrayList and places cells with a given lifetime. Each cell has a random acceleration and random location where appears. It was really easy to do this code given that in the challenge before I created a game of life program where this code was based on. In the game of life ecosystem challenge there was no repulsion between the cells but that code was from exercises before. There was no difficulty doing this one.
I think that if you see my challenge ecosystem code it does exactly this but with one circle bigger than others not between each other.
//Kai Kawasaki Ueda A01336424
//#IBCS3 "Chapter3 E_10"
//Profesor: José Manuel Vega Cebrián
ParticleSystem ps;
void setup() {
size(700,700);
ps = new ParticleSystem(new PVector(width/2,50));
}
void draw() {
background(224);
ps.addParticle(random(width),random(height));
ps.update();
ps.intersection();
ps.display();
}
//Kai Kawasaki Ueda A01336424
//#IBCS3 "Chapter2 E_10"
//Profesor: José Manuel Vega Cebrián
class Particle {
PVector loca,velo,acce;
float lifespan,r=6;
Particle(float x, float y) {
acce = new PVector();
velo = PVector.random2D();
loca = new PVector(x, y);
lifespan = 255.0;
}
void run() {
update();
display();
}
void intersects(ArrayList<Particle> particles) {
for (Particle other : particles) {
if (other != this) {
PVector dir = PVector.sub(loca, other.loca);
if (dir.mag() < r*2) {
dir.setMag(0.5);
applyForce(dir);
}
}
}
}
void applyForce(PVector f) {
acce.add(f);
}
// Method to update location
void update() {
velo.add(acce);
loca.add(velo);
acce.mult(0);
lifespan -= 2.0;
checkEdges();
}
// Method to display
void display() {
stroke(0, lifespan);
strokeWeight(2);
fill(127, lifespan);
ellipse(loca.x, loca.y, r*2, r*2);
}
// Is the particle still useful?
boolean isDead() {
if (lifespan < 0.0) {
return true;
}
else {
return false;
}
}
if (loca.x > width) {
loca.x = width;
velo.x *= -1;
}
else if (loca.x < 0) {
loca.x = 0;
velo.x *= -1;
}
if (loca.y > height) {
loca.y = height;
velo.y *= -1;
}
else if (loca.y < 0) {
loca.y = 0;
velo.y *= -1;
}
Code class ParticleSystem:
//Kai Kawasaki Ueda A01336424
//#IBCS3 "Chapter2 E_10"
//Profesor: José Manuel Vega Cebrián
class ParticleSystem {
ArrayList<Particle> particles;
ParticleSystem(PVector location) {
particles = new ArrayList<Particle>();
}
void addParticle(float x, float y) {
particles.add(new Particle(x, y));
}
void display() {
for (Particle p : particles) {
p.display();
}
}
void applyForce(PVector f) {
for (Particle p : particles) {
p.applyForce(f);
}
}
void intersection() {
for (Particle p : particles) {
p.intersects(particles);
}
}
void update() {
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.update();
if (p.isDead()) {
particles.remove(i);
}
}
}
}