This ball goes through the rectangle rather than hitting it or bouncing it

Hi, Can you please help to make the ball hit the rectangle rather than go through the rectangle

float ballx,bally,xspeed=5,yspeed=8;

void setup() {
size(displayWidth,displayHeight);
stroke(255);
strokeWeight(100);
}

void draw() {
background(0);
point(ballx,bally); //draw the ball
if(sideCollision(ballx)) {
xspeed = xspeed * -1; //reverse horizontal speed
}
if(topOrBottomCollision(bally)) {
yspeed = yspeed*-1; //reverse vertical speed
}
ballx = ballx + xspeed;
bally = bally + yspeed;
rect(810,100,300,75);
rect(mouseX,880,300,75);
}

boolean sideCollision(float x) {
if(x<0 || x>width) {
return true;
} else {
return false;
}
}

boolean topOrBottomCollision(float y) {
if(y<0 || y>height) {
return true;
} else {
return false;
}
}

Hi, I used to struggle with collision too but it is not that hard if you make a lot of them.

I don’t know how your program looks or what it is supposed to do and I dont see any other values for the balls appart from ballx and bally. Usually when I make collisions I write an If statement:

if( ballx > 810 || balx < 1110){
xspeed = xspeed*-1
}

I usually try to think about the square it has to hit. The left side value is 810, the right side value is 810+300=1110. The ball has to change direction if it comes between those two values so change direction if: the right side of the ball (ballx) is bigger than the left side value of the box (810) or the other way around, if the left side of the ball (ballx) is smaller than the right side value of the box (1110)

thank you, but can you write the whole thing in codes, so I can understand better. As it gives me a error that ballx cannot be resolved to a variable.

That is the code for the X-side of the box repeat this proces for the Y-side of the box and it should work

but it isnt working, and where exactly should i put it

Put this in the void draw() , it will only work when the ball hits the square.

I aready put it in void draw but it did not work

It says ballx cannot be resolved to a variable

I found the problem,

if the ball has to hit this rectangle you have to put the mouseX variable in the if statement:
if( ballx > mouseX || balx < mouseX+300){
xspeed = xspeed*-1
}

can you do it yourself on your proccessing app and then paste in the whole thing

Please, because it is confusing for me

Becuase on my codes it still says ballx cannot be resolved to a variable

are you there?? Please Help Me

put the code inside the void draw() you made the variable ballx

I know look this is what I did, and the reason why I was getting that error was because you wrote the wrong spelling for ball you wrote balx instead of ballx but also after doing that it is the same as before:

float ballx,bally,xspeed=5,yspeed=8;

void setup() {
size(displayWidth,displayHeight);
stroke(255);
strokeWeight(100);
}

void draw() {
background(0);
point(ballx,bally); //draw the ball
if(sideCollision(ballx)) {
xspeed = xspeed * -1; //reverse horizontal speed
}
if(topOrBottomCollision(bally)) {
yspeed = yspeed*-1; //reverse vertical speed
}
ballx = ballx + xspeed;
bally = bally + yspeed;
rect(810,100,300,75);
rect(mouseX,880,300,75);
if( ballx > 810 || ballx < 1110){
xspeed = xspeed*-1;
}
}

boolean sideCollision(float x) {
if(x<0 || x>width) {
return true;
} else {
return false;
}
}

boolean topOrBottomCollision(float y) {
if(y<0 || y>height) {
return true;
} else {
return false;
}
}

The ball will now bounce between the X sides you have to make the if statement for the Y side as well

You started this conversation in another thread (your first post) and members of the forum responded to you. Since then you have deleted the discussion title and all your comments making the whole conversation useless to anyone wanting to read it.
This is extremely disrespectful to the members who responded and goes against forum etiquette. Please do not do that again here

@yanze03, I also did the other side you ballx and bally but it is still not bouncing on it Here are my codes:

float ballx,bally,xspeed=5,yspeed=8;

void setup() {
size(displayWidth,displayHeight);
stroke(255);
strokeWeight(100);
}

void draw() {
background(0);
point(ballx,bally); //draw the ball
if(sideCollision(ballx)) {
xspeed = xspeed * -1; //reverse horizontal speed
}
if(topOrBottomCollision(bally)) {
yspeed = yspeed*-1; //reverse vertical speed
}
ballx = ballx + xspeed;
bally = bally + yspeed;
rect(810,100,300,75);
rect(mouseX,880,300,75);
if( ballx > 810 || ballx < 1110){
xspeed = xspeed*-1;
}
if( bally > 810 || bally < 1110){
xspeed = xspeed*-1;
}
}

boolean sideCollision(float x) {
if(x<0 || x>width) {
return true;
} else {
return false;
}
}

boolean topOrBottomCollision(float y) {
if(y<0 || y>height) {
return true;
} else {
return false;
}
}

Please Help as Soon as Possible

Continuing the discussion from TfGuy44 (Please Help) - This ball goes through the rectangle rather than hitting it or bouncing it:

@yanze03, I also did the other side you ballx and bally but it is still not bouncing on it Here are my codes:

float ballx,bally,xspeed=5,yspeed=8;

void setup() {
size(displayWidth,displayHeight);
stroke(255);
strokeWeight(100);
}

void draw() {
background(0);
point(ballx,bally); //draw the ball
if(sideCollision(ballx)) {
xspeed = xspeed * -1; //reverse horizontal speed
}
if(topOrBottomCollision(bally)) {
yspeed = yspeed*-1; //reverse vertical speed
}
ballx = ballx + xspeed;
bally = bally + yspeed;
rect(810,100,300,75);
rect(mouseX,880,300,75);
if( ballx > 810 || ballx < 1110){
xspeed = xspeed*-1;
}
if( bally > 810 || bally < 1110){
xspeed = xspeed*-1;
}
}

boolean sideCollision(float x) {
if(x<0 || x>width) {
return true;
} else {
return false;
}
}

boolean topOrBottomCollision(float y) {
if(y<0 || y>height) {
return true;
} else {
return false;
}
}

Please Help as Soon as Possible

you do realise that you just coppied the code for the X-sides and pasted them for the Y-sides?

I suggest you check out: