/* title: bouncing ball description: ball deflects off sketch window edges created: August 9, 2005 by: Ira Greenberg modified: September 6, 2006, ever so slightly, by Fred Green */ // declare global variables int xspeed, yspeed; int xpos, ypos, wdth, ht; float theta; //initialize sketch void setup(){ //set sketch window size and background color size(400, 400); background(0); //ball speed xspeed = 1; yspeed = 2; theta = 0; //ball size wdth = 100; ht = 100; //initial ball placement xpos = width/2; ypos = height/2; //set the animation loop speed frameRate(30); smooth(); } // begin animation loop void draw(){ //update background background(0); //draw ball noFill(); stroke(255, 100); ellipse(xpos, ypos, wdth, ht); /* Comment in for line and rotation:*/ pushMatrix(); translate(xpos, ypos); rotate(theta); line(-wdth/2, 0, wdth/2, 0); theta += 0.02; popMatrix(); /**/ //upgrade position values xpos += xspeed; ypos += yspeed; /*conditionals detects ball collission with sketch window edges also accounts for thickness of ball */ if (xpos>=width-wdth/2 || xpos<=wdth/2){ xspeed *= -1; } if (ypos>=height-ht/2 || ypos<=ht/2){ yspeed *= -1; } }