My particles don't interact in the right way in processing, why can this be?

Viewed 41

I Am working on a bigger project, were the perticles need to interact when they get close to eachother. The program is quite simpel. When the balls touch each other, or the centres are closer to each other than their combined radius, they change color. However, the balls are randomly changing color right now and I have no idea why. The objects have an Arraylist with all the objects, so they can interact and their is one big class, which funtions as a particle generator.

Box box;

void setup() {
    size(1000,1000);
    box = new Box(20);
}

void draw() {
    background(255);
    box.run();
}

the object

class Object {
    int on;
    PVector loc = new PVector(500,500);
    boolean detect = false;
    PVector v;
    ArrayList<Object> others = new ArrayList<Object>();

    Object(int nin) {
        this.on = nin;
        this.loc = this.loc.add(PVector.random2D().mult(100));
        this.v = PVector.random2D();
    }

    void move() {
        loc.add(PVector.random2D().limit(5));
    }

    void detect() {
        for(int i = 0; i<others.size(); i++) {

          if(i != on) {
              Object o = others.get(i);
              float distance = sqrt(sq(o.loc.x-this.loc.x)+sq(o.loc.y - this.loc.y));
              if(distance < 100) {
                  detect = true;
              }
              else {
                detect = false;
              }
            }
        }
    }

    void display() {
        if(detect == false) {
            noFill();
        }
        if(detect == true) {
            fill(200);
        }
        stroke(50);
        ellipse(loc.x,loc.y, 50,50);
        fill(0);
        text(on, loc.x, loc.y);
    }
}

the generator

class Box {
    int n;
    ArrayList<Object> objects = new ArrayList<Object>();

    Box(int nin) {
        this.n = nin;
        for(int i = 0; i<n; i++) {
            objects.add(new Object(i));
        }
        for(int i = 0; i< objects.size();i++) {
            Object x =  objects.get(i);
            x.others = this.objects;
        }
    }

    void run() {
        for(Object i : objects) {
            i.move();
            i.detect();
            i.display();
        }
    }
}
1 Answers

The issue is the loop in the method detect int class Object:

for(int i = 0; i<others.size(); i++) {
    if(i != on) {
        Object o = others.get(i);
        float distance = sqrt(sq(o.loc.x-this.loc.x)+sq(o.loc.y - this.loc.y));
        if(distance < 100) {
            detect = true;
        }
        else {
            detect = false;
        }
    }
}

The variable detect is set in every iteration of the loop. Thus the after the loop has terminated, the state of detect depends on the last element in the list others. The result is exactly the same, as you would just evaluate the last element of the list.

Break the loop, if the value of detect becomes true. e.g.:

class Object {
    // [...]

    void detect() {
        detect = false;     
        for(int i = 0; i < others.size() && !detect; i++) {
            if(i != on) {
                Object o = others.get(i);
                float distance = sqrt(sq(o.loc.x-this.loc.x)+sq(o.loc.y - this.loc.y));
                detect = distance < 50;
            }
        }
    }

    // [...]
}

Note, the 3rd and 4th parameter of the ellipse() are the width and the height of the ellipse, thus the the minimum distance is 50, rather than 100.

Related