Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 5-6: Bouncing Ball
int x = 0;
int speed = 1;

void setup() {
  size(200,200);
  smooth();
}

void draw() {
  background(255);

  // Add the current speed to the x location.
  x = x + speed;

  // Remember, || means "or."
  if ((x > width) || (x < 0)) {
    // If the object reaches either edge, multiply speed by -1 to turn it around.
    speed = speed * -1;
  }

  // Display circle at x location
  stroke(0);
  fill(175);
  ellipse(x,100,32,32);
}








  • Bbvjfj

    how to change it to a circle

  • fedhere

    teaching myself processing by reproducing these examples. with a small modification i can deform the ball when it bounces :) but it is done in a nasty nested conditional statement. i am sure there are more elegant ways to do it!

       if (frameCount > xs){ // i need this to let the ball enter the box the first time without being deformed      if (xc>width-xs/2 && xc 0){ // if going toward the edge            ys=ys+1;             xs=xs-1;           }else {  //going away from edge            ys=ys-1;              xs=xs+1;          }       } else if (xc>width-xs/4){ //bounce condition: change sign of speed         speed =-1;      }else if  (xc xs/4){ // if touching the left edge         if (speed < 0){ //going toward the edge            ys=ys+1;              xs=xs-1;          }else {  // moving away from the edge            ys=ys-1;             xs=xs+1;         }        }else if (xc<xs/4){  //bouning condition : change sign of speed           speed = +1;      }
       }
       xc=xc+dx*xs/32; //note i increase position by 1 if away from the edge (constant speed) but i decrease the speed when i touch the edge, increasing the center by progressively small values as the ball shrinks

    stroke(0);
    fill(175);
    ellipse(xc,100,32,32);
    }

  • group1

    LOl

  • group1

    stephan

  • group1

    watching U

  • gmart

    my browsers won’t run the plugin because of an insecure or expired JRE.

  • Nada Feleni

    Mewz found an error on reversing the polarity of a number. at 19% 2143. I think the -5*-1=
    -5 (should be 5). My small contribution to the future editions -ha. I am sure someone pointed this out already tho. Oh well..