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();
}
}
}
