//Matt Testa// //X5 Collisions// ///Globals// String title= "Collisions"; String news= "Use 'q' key to reset"; String author= "Matt Testa"; float top, bottom, left, right, center, middle; float cueX, cueY, cueDX, cueDY; float redX, redY, redDX, redDY; float greenX, greenY, greenDX, greenDY; float yellowX, yellowY, yellowDX, yellowDY; //setup/// void setup() { size (800,600); left= 80; right= width-50; top= 120; bottom= height-80; middle= width/2; reset (); } void reset() { cueX= left + ( right-left )/6; cueY= top + ( bottom-top )/4; //Random// redX= random( middle,right ); redY= random( top, bottom ); yellowX= random( middle,right ); yellowY= random( top, bottom ); greenX= random( middle,right ); greenY= random( top, bottom ); // Random speeds redDX= random( 1,1 ); redDY= random( 1,1 ); yellowDX= random( 1,1 ); yellowDY= random( 1,1 ); greenDX= random( 2,2 ); greenDY= random( 2,2 ); } //// NEXT FRAME: table, bounce off walls, collisions, show all void draw() { background( 250,250,200 ); rectMode( CORNERS ); table( left, right, top, bottom ); bounce(); collisions(); show(); messages(); } //// SCENE: draw the table with walls void table( float left, float right, float top, float bottom ) { fill( 200, 80, 120 ); //pool table strokeWeight(20); stroke( 127, 0, 0 ); // Brown walls rect( left, top, right, bottom ); stroke(0); strokeWeight(1); } //ACTION// void bounce() { redX += redDX; if (redXright) redDX *= -1.5; redY += redDY; if (redYbottom) redDY *= -1.5; yellowX += yellowDX; if (yellowXright) yellowDX *= -1.5; yellowY += yellowDY; if (yellowYbottom) yellowDY *= -1.5; greenX += greenDX; if (greenXright) greenDX *= -1.5; greenY += greenDY; if (greenYbottom) greenDY *= -1.5; cueX += cueDX; if (cueXright) cueDX *= -1.5; cueY += cueDY; if (cueYbottom) cueDY *= -1.5; } void collisions() { float tmp; //elastic impact// if ( dist (redX, redY, yellowX, yellowY ) < 50 ) { tmp=redDX; redDX=yellowDX; yellowDX=tmp; tmp=redDY; redDY=yellowDY; yellowDY=tmp; } if ( dist (redX, redY, greenX, greenY ) < 50 ) { tmp=redDX; redDX=greenDX; greenDX=tmp; tmp=redDY; redDY=greenDY; greenDY=tmp; } if ( dist (yellowX, yellowY, greenX, greenY ) < 50 ) { tmp=yellowDX; yellowDX=greenDX; greenDX=tmp; tmp=yellowDY; yellowDY=greenDY; greenDY=tmp; } } void show() { fill( 255,255,255); one( cueX, cueY); fill( 255,0,0); one (redX, redY); fill(0,255,0); one (greenX, greenY); fill(230,240,120); one (yellowX, yellowY); } void one( float x, float y ) { ellipse(x,y, 40,40); } void messages() { fill(0); text( title, width/2, 20); text( news, width/2, 600); text(author, 40, 40); } void keyPressed() { if (key == 'q') { reset(); } if (key == '+') { cueDX *=3; cueDY *=3; } if (key == '-') { cueDX *=3; cueDY *=3; } } void mousePressed() { float force= dist( mouseX,mouseY, cueX, cueY ) / 15; strokeWeight( force ); }