ketamine_boy
YaBB Newbies
Offline
Posts: 1
balls colliding problem
Apr 21st , 2008, 9:35pm
hello people, my name is Matias, im new in this list, im also new with processing, ... im need to make a lot spheres that moves randomly in the screen and when they collide (or become very near between each other)they change their color and if they separate they come back to their original color. I was trying to adapt the bouncy bubbles from the processing examples in the web site, but im getting unexpected and wierd behaivors ... do anybody know whats going on? any hint would be very appreciated. here is the code: int numAgentes = 22; Agente[] arreglo =new Agente[numAgentes]; void setup(){ size(500,500); for (int i=0; i<numAgentes; i++){ arreglo[i] = new Agente(random(width), random(height), i, arreglo); } } void draw(){ background(188, 282, 299); for (int i=0; i<numAgentes; i++){ arreglo[i].collide(); arreglo[i].mueve(); arreglo[i].display(); } } class Agente{ float coloro = 0; float x, y; float diameter; float vx = 0; float vy = 0; int id; Agente[] others; Agente(float xin, float yin, int idin, Agente[] oin) { x = xin; y = yin; // diameter = din; id = idin; others = oin; print( id); print(others[1]); } void collide() { for (int i = id + 1; i < numAgentes; i++) { float dx = others[i].x - x; float dy = others[i].y - y; float distance = sqrt(dx*dx + dy*dy); float minDist = 60; if (distance < minDist) { coloro = 122; } else { coloro = 0; } } } void mueve(){ x = x + random(2) - 1; y = y + random(2) - 1; } void display(){ fill(coloro); ellipse(x , y ,20,20); } }