Keeping a ball within bounds

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++;
}

}

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

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

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:

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

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.

1 Like



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;
}