Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F87027264
ParticleSystem.pde
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, Oct 10, 02:21
Size
2 KB
Mime Type
text/x-c++
Expires
Sat, Oct 12, 02:21 (2 d)
Engine
blob
Format
Raw Data
Handle
21523031
Attached To
rBAFOURPROJECT InfoVisuGit
ParticleSystem.pde
View Options
// 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));
score += 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);
gameSurface.rotateX(PI);
gameSurface.scale(120);
gameSurface.shape(robot,0,0);
gameSurface.popMatrix();
}
}
}
Event Timeline
Log In to Comment