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..

  • Harshit

    despite having the video file in my sketch’s data folder, I am unable to play it. please tell me where I am going wrong. here is the code:

    import processing.video.*;

    Movie move;

    void setup()

    {

    move = new Movie(this, “kasoor.mp4″);

    move.play();

    }

    void draw()

    {

    image(move, 0, 0);

    }

    void movieEvent(Movie m)

    {

    m.read();

    }

  • Theresakiwi

    i tried to make the ball bounce back earlier, so when the edge of the ball hits the edge of the canvas. So i added the radius to the 0 and subtracted the radius of the width.
    Problem now: when played, the ball stays in the left corner and doesn’t move at all. Why is that?

    int x = 0;
    int speed = 1;

    void setup() {

    size(200,200);
    smooth();
    }

    void draw() {

    background(255);
    x = x + speed;

    if ((x > width – 16) || (x < 0 + 16)) {
    speed = speed * -1;
    }

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

  • Hossam Elbanna

    could someone telling me why the shaps not moving in this code.I want to make them bouncing from side to side.

    // width and height are used by Processing, you should use different variable names

    // Your code actually works with these commented out, see setup()

    //int width = 640, height = 680;

    color colour;

    Rotater[] rotaters;

    int width = 640, height = 480;

    float radius =100.0f;

    PVector[] optionOrigin;

    PVector[] optionSpeed;

    color [] optionColour;

    int optionSelected;

    void mainMenu() {

    //process each menu option

    for (int i=0; i< optionOrigin.length; i++) {

    //calculate next possition

    optionOrigin[i].x += optionSpeed[i].x;

    optionOrigin[i].y += optionSpeed[i].y;

    // collision with side

    if (optionOrigin[i].x – radius = width)

    optionSpeed[i].x *= -1;

    if (optionOrigin[i].y – radius = height)

    optionSpeed[i].y *= -1;

    }

    }

    void setup() {

    // This is where width and height are defined by Processing

    size(width, height);

    frameRate(20);

    noStroke();

    optionSelected = -1;

    optionOrigin = new PVector[3];

    for (int i = 0;i < optionOrigin.length; i++) {

    optionOrigin[i] = new PVector (random(radius,640 – radius),random(radius,480 – radius));

    }

    optionSpeed = new PVector[3];

    for (int i=0;i<optionOrigin.length; i++) {

    optionSpeed[i] = new PVector (random(10),random(10));

    rectMode(CENTER);

    stroke(0);

    }

    rotaters = new Rotater[20];

    for (int i = 0; i < rotaters.length; i++ ) {

    rotaters[i] = new Rotater(random(width), random(height), random(-0.1, 0.1), random(48));

    }

    }

    void draw() {

    background(255);

    for (int i = 0; i < rotaters.length; i++) {

    rotaters[i].spin();

    rotaters[i].display();

    }

    }

    class Rotater {

    float x, y; // x,y location

    float theta; // angle of rotation

    float speed; // speed of rotation

    float w; // size of rectangle

    Rotater(float tempX, float tempY, float tempSpeed, float tempW) {

    x = tempX;

    y = tempY;

    theta = 0;

    speed = tempSpeed;

    w = tempW;

    }

    void spin() {

    theta += speed;

    }

    void display() {

    // All of your rect() have rectMode(CENTER)

    // Instead of calling this every time you draw a rect() do it once in setup()

    //rectMode(CENTER);

    // All of your rect() have stroke(0)

    // Moved to setup()

    //stroke(0);

    fill(0, 500);

    pushMatrix();

    translate(x, y);

    rotate(theta);

    rect(0, 0, w, w);

    popMatrix();

    }

    }