javaprocessing

My "final PVector" variable is changing every frame, why?


Every Frame, my particle system position changes, without me ever assigning to it.

Like so:

[50.821663, 49.430023]
[51.553301, 50.059512]
[52.480217, 47.301073]
[53.643673, 48.139871]
[55.829941, 50.354572]
[58.192818, 51.504097]
[57.826793, 52.627758]
[54.456882, 52.148365]

Anyway here's my (simplified) Particle class:

class Particle {
  PVector pos = new PVector();
  void Display() {
    pos.add(PVector.random2D().limit(7));
  }
  Particle(PVector pos) {
    this.pos = pos;
  }
}

And the ParticleSystem:

class PartSys {
  final PVector Pos = new PVector(50,50);
  ArrayList<Particle> Particles = new ArrayList<Particle>();
  void run() {
    circle(Pos.x,Pos.y,100);
    for(Particle part : Particles){
      part.Display();
    }
  }
}

ParticleSys.run(); is ran every frame.


Solution

  • My trouble came from this line: this.pos = pos; When I set the position in the constructor function, it set this.pos as a reference to our ParticleSystem position.

    You see, on this line pos.add(PVector.random2D().limit(7)); (Which seemed like a perfectly reasonable thing to do)

    I was indirectly (through this.pos) adding to the original ParticleSystem position.

    MASSIVE thanks to @xerx593