We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Bouncing Ball Difficulties
Page Index Toggle Pages: 1
Bouncing Ball Difficulties (Read 966 times)
Bouncing Ball Difficulties
Feb 9th, 2010, 3:04pm
 
Hi,

I'm having some difficulties with a slight variation of the Processing Example 5-6 (Bouncing Ball). I can get the ball to bounce up and down, and I can make it bounce at a 45° angle. I am trying to get the ball to bounce at an angle other than that which it hits the wall. For example, if the ball were to fall straight down, I'd like it to bounce back at a 45° angle, and continue to do so along the other walls.
I'm new to Processing, and know the code roughly through Chapter 5 of Learning Processing.

Here's the code so far, with my comments as well:

//Set up necessary variables.
int x=10; //x-coordinate of ellipse moving around sketch edge.
int y=10; //y-coordinate of ellipse moving around sketch edge.
int cx=100; //x-coordinate of bouncing ball
int cy=10; //y-coordinate of bouncing ball
int r=20; //radius of bouncing ball
int ballspeed=1; //bouncing ball's speed
int dirx=1; //bouncing ball's direction on the x-axis
int diry=1; //bouncing ball's direction on the y-axis

//Set up variable for speed of ellipse moving around sketch edge.
int speed=1;

//Allows ellipse moving around sketch edge to change direction depending on position.
int state=0;

//Set up size of and smooth out sketch.
void setup(){
 size(200,200);
 smooth();
}

//Set background to white to hide previous draw commands.
void draw(){
 background(200);

 noStroke();
 //Fill bouncing ball as white.
 fill(255);
 //Change ellipse mode.
 ellipseMode(CENTER);
 //Draw bouncing ball with variables.
 ellipse(cx,cy,r,r);
 
 //Set cy variable to move across the sketch
 cy=cy+ballspeed;
 
 //Set condition to bounce ball once it reaches sketch edge.
 if((cy>height-40)||(cy<0)){
   ballspeed=ballspeed*-1;
 }
 //Set cx variable to bouncing ball move across the sketch.
 cx=cx+ballspeed;
 
 //Set condition to bounce ball once it reaches sketch edge.
 if((cx>width-20)||(cx<0)){
   ballspeed=ballspeed*-1;
 }
 //Fill ellipse to move around sketch edge with black.
 fill(0);
 //Draw ellipse to move around sketch edge, using variables.
 ellipse(x,y,20,20);
 //Change rectangle mode, stroke joining mode, and stroke weight.
 rectMode(CENTER);
 strokeJoin(ROUND);
 strokeWeight(5);
 stroke(0);
 //Change fill color.
 fill(100);
 //Draw rectangle.
 rect(150,200,100,50);
 //Set condition for ellipse to move right and stay inside sketch.
 //Change state variable by 1.
 if(state==0){
   x=x+speed;
   if(x>width-10){
     x=width-10;
     state=1;
   }
 }
 //Set condition for ellipse to move down and stay inside sketch.
 //Make allowances for rectangle shape.
 //Change state variable by 1.
 else if(state==1){
   y=y+speed;
   if(y>height-35){
     y=height-35;
     state=2;
   }
 }
 //Set condition for ellipse to move left and stay inside sketch.
 //Make allowances for rectangle shape.
 //Change state variable by 1.
 else if(state==2){
   x=x-speed;
   if(x<90){
     x=width-110;
     state=3;
   }
 }
 //Set condition for ellipse to move down and stay inside sketch.
 //Make allowances for rectangle shape.
 //Change state variable by 1.
 else if(state==3){
   y=y+speed;
   if(y<190){
     y=190;
     state=4;
   }
 }//Set condition for ellipse to move left and stay inside sketch.
 //Change state variable by 1.
 else if(state==4){
   x=x-speed;
   if(x<10){
     x=10;
     state=5;
   }
 }//Set condition for ellipse to move up and stay inside sketch.
 //Change state variable to zero to repeat movement.
 else if(state==5){
   y=y-speed;
   if(y<10){
     y=10;
     state=0;
   }
 }
}

The bouncing ball is the only major difficulty in the sketch, but if there're any glaring mistakes in the rest of the sketch, feel free to point it out.
Thanks for the help!
Re: Bouncing Ball Difficulties
Reply #1 - Feb 9th, 2010, 4:24pm
 
Nevermind, I fixed the problem. Here's the (decently) working code for anyone interested:

//Set up necessary variables.
int x=10; //x-coordinate of ellipse moving around sketch edge.
int y=10; //y-coordinate of ellipse moving around sketch edge.
int cx=100; //x-coordinate of bouncing ball
int cy=10; //y-coordinate of bouncing ball
int r=20; //radius of bouncing ball
int vx=2; //bouncing ball's direction on the x-axis
int vy=3; //bouncing ball's direction on the y-axis

//Set up variable for speed of ellipse moving around sketch edge.
int speed=1;

//Allows ellipse moving around sketch edge to change direction depending on position.
int state=0;

//Set up size of and smooth out sketch.
void setup(){
 size(200,200);
 smooth();
}

//Set background to white to hide previous draw commands.
void draw(){
 background(200);

 noStroke();
 //Fill bouncing ball as white.
 fill(255);
 //Change ellipse mode.
 ellipseMode(CENTER);
 //Draw bouncing ball with variables.
 ellipse(cx,cy,r,r);
 
 //Set cy variable to move across the sketch
 cy=cy+vy;
 
 //Set condition to bounce ball once it reaches sketch edge.
 if((cy>height-40)||(cy<0)){
   vy=vy*-1;
 }
 //Set cx variable to bouncing ball move across the sketch.
 cx=cx+vx;
 
 //Set condition to bounce ball once it reaches sketch edge.
 if((cx>width-10)||(cx<0)){
   vx=vx*-1;
 }
 
 //Fill ellipse to move around sketch edge with black.
 fill(0);
 //Draw ellipse to move around sketch edge, using variables.
 ellipse(x,y,20,20);
 //Change rectangle mode, stroke joining mode, and stroke weight.
 rectMode(CENTER);
 strokeJoin(ROUND);
 strokeWeight(5);
 stroke(0);
 //Change fill color.
 fill(100);
 //Draw rectangle.
 rect(150,200,100,50);
 //Set condition for ellipse to move right and stay inside sketch.
 //Change state variable by 1.
 if(state==0){
   x=x+speed;
   if(x>width-10){
     x=width-10;
     state=1;
   }
 }
 //Set condition for ellipse to move down and stay inside sketch.
 //Make allowances for rectangle shape.
 //Change state variable by 1.
 else if(state==1){
   y=y+speed;
   if(y>height-35){
     y=height-35;
     state=2;
   }
 }
 //Set condition for ellipse to move left and stay inside sketch.
 //Make allowances for rectangle shape.
 //Change state variable by 1.
 else if(state==2){
   x=x-speed;
   if(x<90){
     x=width-110;
     state=3;
   }
 }
 //Set condition for ellipse to move down and stay inside sketch.
 //Make allowances for rectangle shape.
 //Change state variable by 1.
 else if(state==3){
   y=y+speed;
   if(y<190){
     y=190;
     state=4;
   }
 }//Set condition for ellipse to move left and stay inside sketch.
 //Change state variable by 1.
 else if(state==4){
   x=x-speed;
   if(x<10){
     x=10;
     state=5;
   }
 }//Set condition for ellipse to move up and stay inside sketch.
 //Change state variable to zero to repeat movement.
 else if(state==5){
   y=y-speed;
   if(y<10){
     y=10;
     state=0;
   }
 }
}
Page Index Toggle Pages: 1