particle program not working properly

edited February 2014 in Questions about Code

this is a copy of the particle program in the tutorial. the ellipse particle was changed for a star, but besides trembling it does not move. please help

//particles_exmpl2 Simple Particle System by Daniel Shiffman. 
//Particles are generated each cycle through draw(), fall with gravity and fade out over time A ParticleSystem object manages a variable size (ArrayList) list of particles.
PShape star; 
ParticleSystem ps;

void setup() {
  size(640,360, P2D);
  star = createShape();
  star.beginShape();
  // You can set fill and stroke
  star.fill(102);
  star.stroke(255);
  star.strokeWeight(2);
  // Here, we are hardcoding a series of vertices
  star.vertex(0, -50);  star.vertex(14, -20);
  star.vertex(47, -15); star.vertex(23, 7);
  star.vertex(29, 40);  star.vertex(0, 25);
  star.vertex(-29, 40); star.vertex(-23, 7);
  star.vertex(-47, -15);star.vertex(-14, -20);
  star.endShape(CLOSE);
  ps = new ParticleSystem(new PVector(width/8,50));
}

void draw() {
  background(0);
  ps.addParticle();
  ps.run();
}
// A simple Particle class

class Particle {
  PVector location;
  PVector velocity;
  PVector acceleration;
  float lifespan;

  Particle(PVector l) {
    acceleration = new PVector(0,0.05);
    velocity = new PVector(random(-1,1),random(-2,0));
    location = l.get();
    lifespan = 255.0;
  }

  void run() {
    update();
    display();
  }

  // Method to update location
  void update() {
    velocity.add(acceleration);
    location.add(velocity);
    lifespan -= 1.0;
  }

  // Method to display
  void display() {
    stroke(255,lifespan);
    fill(255,lifespan);
    ellipse(location.x,location.y,8,8);
//    translate(location.x,location.y);
//    shape(star);
  }

  // Is the particle still useful?
  boolean isDead() {
    if (lifespan < 0.0) {
      return true;
    } else {
      return false;
    }
  }
}

// A class to describe a group of Particles
// An ArrayList is used to manage the list of Particles 

class ParticleSystem {
  ArrayList<Particle> particles;
  PVector origin;

  ParticleSystem(PVector location) {
    origin = location.get();
    particles = new ArrayList<Particle>();
  }

  void addParticle() {
    particles.add(new Particle(origin));
  }

  void run() {
    for (int i = particles.size()-1; i >= 0; i--) {
      Particle p = particles.get(i);
      p.run();
      if (p.isDead()) {
        particles.remove(i);
      }
    }
  }
}
Tagged:

Answers

  • this is a copy of the particle program in the tutorial. the ellipse particle was changed for a star, but besides trembling it does not move. please help

  • You shouldn't just copy-paste code and hope for the best. You have to understand exactly what the code is doing. Step through this by hand or by using print statements. Where does the program's execution differ from your expectations?

  • well, thanks for your attention anyway. as said, the ellipse particle (in the original code) was changed for a star. that of course requires a pshape, which is written. yet it doesnot behave as a particle at all (rather a star trembling in the cold). hope for your advice.

  • only now i notice the pgrm is added in this discussion. lines 61 and 62 (remarked) should be instead of line 60

Sign In or Register to comment.