Programme works until there are 2 or more particles

So i tried to make the gravitation simulation in processing and it worked really well unti i tried to add more than 1 particle. here is my code:

PVector attract;
int count = 0;
particle par[] = new particle[100];
void setup(){
  size(600,600);
  background(0);
  attract = new PVector();
  noStroke();
  for (int i = 0; i < 20; i++){ // change this 20 to 1 to see intended behavior
    par[i] = new particle(0,-200,1);
    count++;
  }
  noStroke();
}

void draw(){
  background(0);
  translate(width/2, height/2);
  fill(0,255,0);
  ellipse(attract.x,attract.y,20,20);
  fill(255);
for (int i = 0; i < count; i++){
    par[i].update();
    par[i].show();
    par[i].attraction(attract, 1);
  }
}

void mousePressed(){
  par[count] = new particle(0,-200,1);
  count++;
}

And there is my particle class:

class particle {
  float x, y;
  float mass;
  PVector pos;
  PVector vel;
  PVector acc;
  particle(float x, float y, float mass) {
    this.pos = new PVector(x, y);
    this.mass = mass;
    this.vel = PVector.random2D();
    this.acc = new PVector();
  }
  void update() {
    this.vel.add(this.acc);
    this.pos.add(this.vel);
  }
  void show() {
    fill(255);
    noStroke();
    ellipse(this.pos.x, this.pos.y, this.mass*5, this.mass*5);
  }

  void attraction(PVector target, float targetMass) {
    PVector dir = target.sub(this.pos);
    float disSq = dir.magSq();
    disSq = con(disSq, 25, 500);
    float G = 6.67408;
    float magnitude = G*((this.mass*targetMass)/disSq);
    dir.setMag(magnitude);
    this.acc=dir;
  }
  PVector conVec(PVector val, float min, float max) {
    if (val.x>max) val.x = max;
    if (val.x<min) val.x = min;
    if (val.y>max) val.y = max;
    if (val.y<min) val.y = min;
    return val;
  }
  float con(float val, float min, float max) {
    if (val>max) val = max;
    if (val<min) val = min;
    return val;
  }
}

I really don’t know what I’m doing wrong please someone help me.`

1 Like

Hello and welcome to the forum @Xetrocs!

Please note, unformatted code is hard to read!

Increase your chances for a faster response to your question. Please format your code. To learn how: https://discourse.processing.org/faq#format-your-code

Thank you!
:slightly_smiling_face:

2 Likes

Hello, except your typo here where you have added 2 “5”:

ellipse(this.pos.x, this.pos.y, this.mass5, this.mass5);

Everything seems to work quite well.

Your program will crash after a 100 mouse clicks though because of the size of your array particle.

1 Like

those 2 “5” are there to make the dots bigger depending on their mass (i was missing multiplication symbol because I didn’t format my code well) and the mousePressed part is there only for testing. The problem is when there is only oe particle it works just as intended but when i click to add another it starts going the wrong way and as i add more particles it just worse and worse.

I solved the problem. Turns out all that was wrong was that i need to use par[i].attraction(attract, 1); BEFORE I do par[i].update();
A really dumb mistake :smiley:

1 Like