Drawing array of curves from PVector

you call

ps.addParticle();

every draw

void draw() {
  background(0);
  for (ParticleSystem ps : systems) {
    ps.run();
    ps.addParticle();
  }
  if (systems.isEmpty()) {
    fill(255, 255, 0);
  }
}

but that function just adds another particle at the origin of that particle system

  void addParticle() {
    Particle p;
    // Add either a Particle or CrazyParticle to the system
    p = new Particle(origin);

    particles.add(p);
  }

so basically you end up drawing multiple curves from origin. remove that and you will have a single curve i believe.

edit: i think this is what you are aiming for yeh?

ArrayList<Particle> particles;

void setup() {
  size(640, 480);
  noFill();
  strokeWeight(2);
  particles = new ArrayList<Particle>();
}

void draw() {
  background(255);
  for(int i = particles.size() - 1; i >= 0; i--) {
    Particle p = particles.get(i);
    p.update();
    //if(p.pos.x < -100 || p.pos.x > width+100 || p.pos.y < -100 || p.pos.y > height+100)
      //particles.remove(i);
    p.present();
  }
}

void mousePressed() {
  Particle p = new Particle(mouseX, mouseY);
  p.addForce(random(-1, 1), random(-1, 1));
  particles.add(p);
}

class Particle {
  PVector pos;
  PVector vel;
  PVector acc;
  ArrayList<PVector> history;
  int maxHistory;
  float maxWander;
  float maxSpeed;
  
  Particle(float x, float y) {
      this.pos = new PVector(x, y);
      this.vel = new PVector();
      this.acc = new PVector();
      this.history = new ArrayList<PVector>();
      this.history.add(this.pos.copy());
      this.maxHistory = 50;
      this.maxWander = 45 * (PI / 180);
      this.maxSpeed = 2;
  }
  
  void addForce(float fx, float fy) {
    this.acc.x += fx;
    this.acc.y += fy;
  }
  
  void update() {
    //add some wander to the particle to make it more interesting
    float heading = this.vel.heading();
    heading += random(-this.maxWander, this.maxWander);
    this.addForce(cos(heading), sin(heading));
    
    this.vel.add(this.acc);
    this.vel.limit(this.maxSpeed);
    this.acc.mult(0);
    this.pos.add(this.vel);
    
    float dist = PVector.dist(this.pos, this.history.get(this.history.size() - 1));
    if(dist > 5) {
      this.history.add(this.pos.copy());
      if(this.history.size() > this.maxHistory) {
        this.history.remove(0);
      }
    }
  }
  
  void present() {    
    float k = (float)this.history.size();
    for(int i = 1; i < this.history.size(); i++) {
      stroke(0, (i / k) * 255);
      PVector p1 = this.history.get(i - 1);
      PVector p2 = this.history.get(i);
      line(p1.x, p1.y, p2.x, p2.y);
    }     
  }
}

it’s just you are repurposing code that has a lot of superfluous stuff for your goal making the situation a bit confusing

1 Like