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 goes out of sketch.
Page Index Toggle Pages: 1
Bouncing ball goes out of sketch. (Read 334 times)
Bouncing ball goes out of sketch.
Nov 24th, 2008, 3:51pm
 
I have this bouncing ball sketch, it normally bounces one way to the other at the same speed, but as i like things not so uniform and more random i told it that when it bounces from one side it should bounce a bit faster ( just one pixel faster)

the probelm is there is a point when the ball just goes out of screen , is there a way to keep it bouncing, whitout it going out of scrren ?

It actually does some strange bouncing before going out of screen, do you know why ?


here's the code:

Code:
/*Bouncing ball
*/


//Declare global variables
int xspeed, yspeed;
int xpos,ypos,wdth,ht;

//inicializar sketch

void setup(){
 
 //set sketch window and background color
 
 size(800,300);
 background(#FA005C);
 smooth();
 
 //ball speed
 
 xspeed = 8;
 yspeed = 8;
 
 
 // ball size
 
 
wdth = 10;
ht = 10;

noStroke();

//initial ball placement

xpos = width/2;
ypos = height/2;

frameRate(60);
}

//begin animation draw

void draw (){
 
 //update background

background (#FA005C);
 
 // draw ball
   ellipse ( xpos,ypos,wdth,ht);
   
   //upgrade ball position
   
   xpos+=xspeed+1;
   ypos+=yspeed;
   
   /*detects ball collition with sketch window edges also accounts for thickness of the ball
   */
   
   if ( xpos>width-wdth || xpos<=wdth){
      xspeed*=-1;
      }
        if (ypos>=height-ht || ypos<=ht){
          yspeed*=-1;
          }
           
           //saveFrame("filename-####.png");
          }
Re: Bouncing ball goes out of sketch.
Reply #1 - Nov 24th, 2008, 6:31pm
 
I could see the issue by adding a simple trace: println(xspeed + " " + xpos); just after the xpos test
Trace: Code:

8 796 -- Right bound, go back
-8 5 -- Left bound, go back
8 797 -- Idem
-8 6 -- Idem
8 798 -- Right bound, go back, but cannot go back enough
-8 791
8 800
-8 793
8 802
-8 795
8 804
-8 797
8 806
-8 799
8 808
etc.

The issue is after a while, the ball goes at 798 pixels: on next iteration,
xpos += -8+1 => 791 > width-wdth (790), it is still beyond the limit and you invert again the direction, worsening the difference...
That's because speed is below diameter of ball.
BTW, you should check against the radius, too...

I got correct results by resetting xpos to the bound after inverting the speed.
Code:
   xpos += xspeed;
ypos += yspeed;

if (xpos > width - wdth/2) {
xspeed = -xspeed - 1;
xpos = width - wdth/2;
}

if (xpos <= wdth/2) {
xspeed = -xspeed - 1;
xpos = wdth/2;
}
Re: Bouncing ball goes out of sketch.
Reply #2 - Nov 25th, 2008, 4:59pm
 
try using the constrain method to limit your maximum speed.

constrain(value, min, max)

you should probably constrain the speed to  less than 1/2 of the screen width. if the speed gets over 1/2 of the screen width that is how you get the wierd bouncing.( example: the speed of the ball is 10 pixels over half the screen width. if the ball goes 1 pixel over the right edge it jumps to 9 pixels left of center. the next jump is 20 pixels off the screen so the next iteration is10 pixels from the middle of the screen etc. so you will see the ball just jumping around the middle of the screen for a while. by limiting the max speed this won't happen because you will always need at least 2 jumps on the screen but there are instances where it could cause a problem. if so just lower the max constrained value until you no longer see a problem
Page Index Toggle Pages: 1