Search code examples
javaprocessingparticle-system

Processing: Why is my particle system drawing twice at a different location as it follows the mouse?


I'm new to processing, and I want my particle system to follow my mouse. At the moment, it is trailing behind the mouse (which I suppose is fine), but it is also drawing it twice. Here's what I have so far: I also have a 'star' class that I did not include which draws a simple star that follows the mouse.

ParticleSystem ps;

void setup() {
    ps = new ParticleSystem(new PVector(mouseX,mouseY));
size(1000,1000);
frameRate(30);
noStroke();
}

void draw(){
background(30);

    ps.followMouse();
    ps.addParticle();
    ps.run();
}

class ParticleSystem {

    ArrayList<Particle> particles = new ArrayList<Particle>();
    PVector origin;

    ParticleSystem(PVector position) {
        origin = position.copy();
    }

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

    void run() {
        for (int i = particles.size()-1; i >= 0; i--)
            particles.get(i).run();
    }

    void followMouse() {
        PVector mouse = new PVector(mouseX, mouseY);
        origin = mouse.sub(origin);
    }
}

class Particle {

  PVector pos, velocity = new PVector(random(-1, 1), random(-2, 0)), acceleration = new PVector(0, 0.05);

    Particle(PVector l) {
        pos = l.copy();
    }

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

    void update() {
        velocity.add(acceleration);
        pos.add(velocity);
    }

    void display() {
        noStroke();
        fill(random(255),random(255), random(255));
        ellipse(pos.x,pos.y, 8, 8);
    } 
}

Solution

  • Change followMouse() to this:

    void followMouse() {
        origin = new PVector(mouseX, mouseY);
    }