Page MenuHomec4science

ParticleSystem.pde
No OneTemporary

File Metadata

Created
Sun, May 5, 04:00

ParticleSystem.pde

// A class to describe a group of Particles
class ParticleSystem {
ArrayList<PVector> cylinders;
Cylinder cyl = new Cylinder();
float cylinderRadius = cyl.cylinderBaseSize;
float radius;
PVector mainCyl;
PVector origin;
boolean shutdown=false;
Mover m;
ParticleSystem(PVector origin,Mover m,float radius) {
this.m=m;
this.radius=radius;
this.origin = origin.copy();
cylinders = new ArrayList<PVector>();
mainCyl=origin;
cylinders.add(mainCyl);
}
void addParticle() {
PVector center;
if(!cylinders.contains(mainCyl)){
shutdown=true;
}
int numAttempts = shutdown?0:100;
for (int i=0; i<numAttempts; i++) {
// Pick a cylinder and its center.
int index = int(random(cylinders.size()));
center = cylinders.get(index).copy();
// Try to add an adjacent cylinder.
float angle = random(TWO_PI);
center.x += sin(angle) * 2*cylinderRadius;
center.y += cos(angle) * 2*cylinderRadius;
if (checkPosition(center)) {
cylinders.add(new PVector(center.x, center.y));
updateScore(scoreMalus);
break;
}
}
}
boolean checkPosition(PVector center) {
if(!(-plate_w/2<center.x && center.x<plate_w/2 && -plate_w/2<center.y && center.y<plate_w/2)){
return false;
}
if((m.getX()-center.x)*(m.getX()-center.x)+(m.getY()-center.y)*(m.getY()-center.y)<=(radius+cylinderRadius)*(radius+cylinderRadius)){
return false;
}
for (PVector pv : cylinders) {
if (checkOverlap(pv, center)) {
return false;
}
}
return true;
}
boolean checkOverlap(PVector c1, PVector c2) {
return (c1.x-c2.x)*(c1.x-c2.x)+(c1.y-c2.y)*(c1.y-c2.y)<=4*cylinderRadius*cylinderRadius;
}
// Iteratively update and display every particle,
// and remove them from the list if their lifetime is over.
void run(float z, float r) {
for (int i=cylinders.size()-1; i>=0; --i) {
PVector p=cylinders.get(i);
cyl.draw(p.x, p.y, z, r);
}
if(cylinders.contains(mainCyl)){
gameSurface.pushMatrix();
gameSurface.translate(mainCyl.x,z - cyl.cylinderHeight, mainCyl.y);
float angle = atan2(mainCyl.y-m.location.y, mainCyl.x-m.location.y);
gameSurface.rotateY(-angle+PI/2);
gameSurface.rotateX(PI);
gameSurface.scale(120);
gameSurface.shape(robot,0,0);
gameSurface.popMatrix();
}
}
}

Event Timeline