# Programme works until there are 2 or more particles

**URL:** <https://discourse.processing.org/t/programme-works-until-there-are-2-or-more-particles/18541>\
**Category:** Coding Questions\
**Created:** [March 9, 2020, 8:02pm UTC](https://discourse.processing.org/t/programme-works-until-there-are-2-or-more-particles/18541 "2020-03-09T20:02:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xetrocs](https://avatars.discourse-cdn.com/v4/letter/x/cc9497/32.png) [@Xetrocs](https://discourse.processing.org/u/Xetrocs)\
**Post date:** [March 9, 2020, 8:02pm UTC](https://discourse.processing.org/t/programme-works-until-there-are-2-or-more-particles/18541/1 "2020-03-09T20:02:02Z")

</div>

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:

```auto
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:

```auto
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.`

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [March 9, 2020, 8:04pm UTC](https://discourse.processing.org/t/programme-works-until-there-are-2-or-more-particles/18541/2 "2020-03-09T20:04:45Z")

</div>

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](https://discourse.processing.org/faq#format-your-code)

Thank you!  
🙂

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [March 9, 2020, 8:31pm UTC](https://discourse.processing.org/t/programme-works-until-there-are-2-or-more-particles/18541/3 "2020-03-09T20:31:56Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![Xetrocs](https://avatars.discourse-cdn.com/v4/letter/x/cc9497/32.png) [@Xetrocs](https://discourse.processing.org/u/Xetrocs)\
**Post date:** [March 9, 2020, 9:15pm UTC](https://discourse.processing.org/t/programme-works-until-there-are-2-or-more-particles/18541/4 "2020-03-09T21:15:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Xetrocs](https://avatars.discourse-cdn.com/v4/letter/x/cc9497/32.png) [@Xetrocs](https://discourse.processing.org/u/Xetrocs)\
**Post date:** [March 22, 2020, 10:51am UTC](https://discourse.processing.org/t/programme-works-until-there-are-2-or-more-particles/18541/5 "2020-03-22T10:51:20Z")

</div>

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 😃
