Performance Enhancment of Particle Systems Help
in
Programming Questions
•
2 years ago
I am interested to find out more about how others improve performance when it comes to building Particle Systems. Mainly, when I work with alot of particles I see a degrade in the frame rate and overall speed of the sketch. Can anyone shed any light on this for me?
Here is my sketch below. It is based on the particle systems examples from Daniel Shiffman's upcoming Nature of Code book. Any help would be greatly appreciated. Thanks!
MultipleFireParticleSystem.pde
Image
Here is my sketch below. It is based on the particle systems examples from Daniel Shiffman's upcoming Nature of Code book. Any help would be greatly appreciated. Thanks!
MultipleFireParticleSystem.pde
- ArrayList<ParticleSystem> psArray;
Random generator;
void setup() {
size(400, 400);
background(255);
smooth();
generator = new Random();
psArray = new ArrayList<ParticleSystem>();
for (int i = 0; i< 10; i++) {
psArray.add(new ParticleSystem(0, new PVector(random(width), random(height))));
}
}
void draw() {
background(0);
Iterator it = psArray.iterator();
while (it.hasNext()) {
ParticleSystem ps = (ParticleSystem)it.next();
float dx = map(mouseX, 0,width,-0.1,0.1);
PVector wind = new PVector(dx,0);
ps.applyForce(wind);
ps.addParticle();
ps.run();
}
}
- class Particle {
PVector location;
PVector velocity;
PVector acceleration;
float lifespan; // keep track of how long the particle is alive
float mass = 1;
PImage img;
Particle (PVector l, PImage img_) {
location = l.get(); // make copy of PVector
acceleration = new PVector(0,0); //init to 0,0
// create initial velocities
float vx = (float) generator.nextGaussian() * 0.3;
float vy = (float) generator.nextGaussian() * 0.3-1.0;
velocity = new PVector(vx, vy);
//velocity = new PVector(random(-2, 2), random(-3, 0));
lifespan = 255;
img = img_;
}
void run() {
update();
display();
}
void applyForce(PVector force) {
PVector f = force.get();
//f.div(mass); //ignore mass
acceleration.add(f);
}
void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
lifespan -= 2.0;
}
void display() {
imageMode(CENTER);
tint(255, lifespan);
image(img, location.x, location.y);
}
boolean isDead() {
if (lifespan < 0.0) {
return true;
}
else {
return false;
}
}
}
- class ParticleSystem {
ArrayList<Particle> particles; // use one list to include all subclasses of Particle - polymorphism awesomeness!
PVector origin;
PImage pImg;
ParticleSystem (int num, PVector location) {
origin = location.get();
particles = new ArrayList<Particle>();
//img = img_;
for(int i=0; i < num; i++) {
addParticle();
}
}
void addParticle() {
pImg = loadImage("textureRed.png");
particles.add(new Particle(origin, pImg));
}
// Recieve a force as a pVector and apply that force to all the Particles in the Particle System
void applyForce(PVector f) {
for(Particle p: particles) {
p.applyForce(f);
}
}
void run() {
Iterator it = particles.iterator();
while (it.hasNext()) {
Particle p = (Particle)it.next();
p.run();
if (p.isDead()) {
it.remove();
}
}
}
}
Image
1