2

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);
    } 
}
NellyNova
  • 23
  • 5
  • You have to erase everything you drew before by calling `background(0)` or `background(255)` at the start of your `draw` method. – cegredev Apr 12 '20 at 18:29
  • Is the particle system supposed to "go toward" the mouse pointer or are the particles supposed to "emerge" from the place where the mouse is pointing? – laancelot Apr 12 '20 at 18:45
  • Thank you ! I made the edits to my draw method, but it's still not performing the way that I want. @laancelot The particles are supposed to emerge from the place where the mouse is pointing. – NellyNova Apr 12 '20 at 18:59
  • How *is* it performing? – cegredev Apr 12 '20 at 19:00
  • @Schred Right now, the particles are trailing behind the mouse and also at y=0 it seems. When I move the mouse, the particles appearing near y=0 move in the same way. It just seems duplicated. I'm trying to find where it is displaying twice in my code. – NellyNova Apr 12 '20 at 19:07

1 Answers1

3

Change followMouse() to this:

void followMouse() {
    origin = new PVector(mouseX, mouseY);
}
cegredev
  • 1,485
  • 2
  • 11
  • 25
  • Although this should do the trick, you could get rid of `origin` completely as as far it's been more of a source of confusion than something helpful. – laancelot Apr 12 '20 at 19:14
  • Thank you so much! – NellyNova Apr 12 '20 at 19:14
  • @laancelot How do you imagine that? Change the `addParticle` method so that it always uses the mouse position as the origin? – cegredev Apr 12 '20 at 19:16
  • 1
    Also, @NellyNova, consider posting on the official Processing Forum for help concerning stuff like this. They are much more likely to be able to help you out. https://discourse.processing.org/ – cegredev Apr 12 '20 at 19:18
  • 2
    It depends. If OP has some "plan" with origin, it would have to keep that idea in mind, but if it's just a matter of coordinates I would erase every mention of `origin` and replace it with mouse coordinates where it makes sense, and with `new PVector(mouseX, mouseY)` where a `PVector` is needed instead. – laancelot Apr 12 '20 at 19:19
  • Of course, if OP plans on always using the mouse position, `origin` is unnecessary. – cegredev Apr 12 '20 at 19:22