# Keeping a ball within bounds

**URL:** <https://discourse.processing.org/t/keeping-a-ball-within-bounds/23700>\
**Category:** Coding Questions\
**Created:** [September 5, 2020, 6:59pm UTC](https://discourse.processing.org/t/keeping-a-ball-within-bounds/23700 "2020-09-05T18:59:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![FloorBean](https://avatars.discourse-cdn.com/v4/letter/f/c2a13f/32.png) [@FloorBean](https://discourse.processing.org/u/FloorBean)\
**Post date:** [September 5, 2020, 6:59pm UTC](https://discourse.processing.org/t/keeping-a-ball-within-bounds/23700/1 "2020-09-05T18:59:28Z")

</div>

I am trying to keep the ball in the display window and it only is sometimes working. I am not really sure where to go from here. (Ignore the paddle for now).  
float gap; //Assign variables  
float ballX = random(300);  
float ballY = random(100);  
float dx = 3;  
float neg\_dx = -2;  
float dy = 3;  
float neg\_dy = -3;  
float rcolor = random(255);  
float gcolor = random(255);  
float bcolor = random(255);  
float disp = 0;  
float number = random(3);  
boolean a = number\>1;

void setup(){  
background(50);  
size(640,360);  
gap = height-30;

}

void draw(){

rcolor = random(255);  
gcolor = random(255);  
bcolor = random(255);

rectMode(CENTER);  
background(50);  
fill(255);  
rect(mouseX,gap,100,20);

fill(rcolor,gcolor,bcolor);  
rcolor+=1;  
rcolor= constrain(rcolor,bcolor,gcolor); //Makes the coloring random

if(ballX \>width) { //Statements that keep the ball within the screen.  
dx = -dx;  
neg\_dx = dx;  
}  
if (ballX \<0){  
dx =-dx;  
neg\_dx = dx;  
}  
if(ballY \>height-40){  
dy = -dy;  
neg\_dy = dy;

}  
if(ballY \<0){  
dy = -dy;  
neg\_dy =dy;  
}

if(a){  
circle(ballX,ballY,20); //Position of the ball  
ballX= ballX+dx; //Displacement  
ballX++;  
ballY=ballY+dy;  
ballY++;  
}  
else {  
circle(ballX,ballY,20); //Position of the ball  
ballX= ballX-dx;  
ballX++;  
ballY=ballY+dy;  
ballY++;  
}

}

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 5, 2020, 7:45pm UTC](https://discourse.processing.org/t/keeping-a-ball-within-bounds/23700/2 "2020-09-05T19:45:11Z")

</div>

ballX= ballX-dx;  
ballX++;  
ballY=ballY+dy;  
ballY++;

first, get rid of ballX++; and ballY++;

dx and dy should be enough

**Remark**

ballX= ballX-dx;  
ballY=ballY+dy;

use only + here.

**Remark**

You check all 4 boundaries separately.

I like this.

But you can make the sign then the way you want to have it, using abs() function (same amount, but **always** positive).

So instead of

```auto
if(ballX >width) { //Statements that keep the ball within the screen.
dx = -dx;
neg_dx = dx;
}
if (ballX <0){
dx =-dx;
neg_dx = dx;
}

```

say

```auto
if(ballX >width) { //Statements that keep the ball within the screen.
dx = - abs ( dx );// always negative 
neg_dx = dx;
}
if (ballX <0){
dx = abs (dx); // always positive 
neg_dx = dx;
}

```

Same for y…

**Remark**

These lines:

```auto
float number = random(3);
boolean a = number>1;

```

Not sure what you plan with `a`, but must make the random and the `a = number>1;` inside draw() to apply.  
The way it is now, it is run only once.

Chrisir

---

<div class="post-metadata">

**Author:** ![FloorBean](https://avatars.discourse-cdn.com/v4/letter/f/c2a13f/32.png) [@FloorBean](https://discourse.processing.org/u/FloorBean)\
**Post date:** [September 5, 2020, 8:54pm UTC](https://discourse.processing.org/t/keeping-a-ball-within-bounds/23700/3 "2020-09-05T20:54:53Z")

</div>

Thank you. I had the  
float number = random(3);  
boolean a = number\>1;

in place because I wanted to randomize whether the ball would begin going left or right. I made a boolean statement to pick 1 which would make it go right. If not then it would go left.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 6, 2020, 7:35am UTC](https://discourse.processing.org/t/keeping-a-ball-within-bounds/23700/4 "2020-09-06T07:35:58Z")

</div>

```auto

float ballX = random(300);
float ballY = random(100);

float dx = random(.993, 3); 
float dy = random(.993, 3);

float rcolor = random(255);//Makes the coloring random
float gcolor = random(255);
float bcolor = random(255);

void setup() {
  size(640, 360);
  background(50);
}

void draw() {

  background(50);

  rcolor+=1;
  rcolor= constrain(rcolor, bcolor, gcolor); 
  fill(rcolor, gcolor, bcolor);

  if (ballX > width) { //Statements that keep the ball within the screen.
    dx = - abs(dx);
  }
  if (ballX < 0) {
    dx = abs(dx);
  }
  if (ballY > height) {
    dy = - abs(dy);
  }
  if (ballY < 0) {
    dy = abs(dy);
  }

  ellipse(ballX, ballY, 20, 20); //Position of the ball

  ballX = ballX+dx; //Displacement
  ballY = ballY+dy;
}

```
