//// Demo: array of Zoogs. Bumping each other. Zoog[] z= new Zoog[10]; // Make room for (up to) ten Zoogs. int many=10; int debug=2; void setup() { size( 800,600); smooth(); z= new Zoog[10]; populate( ); } void populate() // Populate the array with a random number of (up to ten) Zoogs. { for (int i=0; i0) { text( counter, x+w/4, 12 + y+h/2); // Display hit count, underneath } // (Debugging: display coords.) if (debug>0) { fill(0,0,255); text( x, x+30+w/4, 12 + y+h/2); text( y, x+30+w/4, 24 + y+h/2); } } void move() // Move to new (x,y) { x += dx; // Move one unit. if (x>width-10 || x<10) // Reverse direction, if off sides. { dx = -dx; x += 3*dx; // Bounce (to avoid jiggle at edge). } y += dy; if (y>height-10 || y<10) // Reverse direction, if beyond top or bottom. { dy = -dy; y += 3*dy; // Bounce (to avoid jiggle at edge). } } void collide( Zoog other) // Return true iff collision with other. { if ( hit(other.x, other.y, this.x, this.y) ) { stroke(255,0,0); strokeWeight(2); fill(c); ellipse(x-2*dx, y-2*dy, 30,30); fill(other.c); ellipse(other.x, other.y, 30,30); strokeWeight(1); stroke(0); /* swap( dx, other.dx ); */ float t= dx; dx= other.dx; other.dx= t; swap( dx, other.dx ); x += 10*dx; swap( dy, other.dy ); y += 10*dy; // Also bump both counters. counter++; other.counter++; } } } //// OTHER METHODS (Not part of a class. boolean hit( float x, float y, float xx, float yy) //// Check for hit: return true if (x,y) near (xx,yy) { return abs(x-xx) < 50 && abs(y-yy) <60; } void swap( float a, float b ) //// Exchange two values. { float tmp= a; a= b; b= tmp; }