Programming for Interactive Digital Arts
CS 2, Fall 2023

SA 8. Due: Wed, April 20

To get acquainted with objects, modify the BouncingBall class to produce a sketch in which the ball grows and shrinks while varying in color from mostly red (when large) to mostly blue (when small). Exact sizes and colors are up to you. Note: the embedded sketch shows one BouncingBall instance and the attached code creates two BouncingBall instances. You may decide how many instances to use in your submission.

BouncingBall b1, b2;

void setup(){
  size(600,600);
  fill(255);
  b1 = new BouncingBall();
  b2 = new BouncingBall();  
}

void draw(){
  background(0);
  b1.update();    
  b2.update();    
}

class BouncingBall{
  float x,y,dx,dy,D;
  
  BouncingBall(){
    x = random(width);
    y = random(height);
    dx = random(-10,10);
    dy = random(-10,10);
    D = random(10,50); 
  }

  void update(){
    x += dx; // update x and y positions according to velocity in x and y
    y += dy;  
    pushMatrix(); // preserve current grid state
    translate(x,y); // transform
    ellipse(0,0,D,D); // draw the circle
    popMatrix(); // restore previous grid state
    bounce(); // check the position of the ball and bounce if necessary
  }

  void bounce(){
    if( x != constrain(x, D/2, width-D/2) ){
      x = constrain(x, D/2, width-D/2);
      dx = -dx;
    }
    if( y != constrain(y, D/2, height-D/2) ){
      y = constrain(y, D/2, height-D/2);
      dy = -dy;
    }
  }

}

Applet

As always, turn in the zipfile on Canvas, as SA 8 in the Assignments section. In the field where you can type in some information, please tell me roughly how long this assignment took, and if there were any particular difficulties.