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

no i changed it to bally

can you please help me rather than sending links, because i understand better thatway

yes… Bbut you didn’t change the coordinates. you are using the X-axis coordinates for the Y-axis, this will not work

i understand what you are saying, but even the x axis is not working

as in even when it touches the x axis it still goes through.

If you get all the positions right and do it for all sides it should work

if( bally > mouseX || bally < 880){
xspeed = xspeed*-1;
}
how about this

Please reply, I changed the coordinates but it is still not working

Here is the full code, I have 2 rectangles:

rect(810,100,300,75);
rect(mouseX,880,300,75);
if( ballx > 810 || ballx < 1110){
xspeed = xspeed*-1;
}
if( bally > mouseX || bally < 880){
xspeed = xspeed*-1;
}

Get a piece of paper, make a drawing of your game, set your coordinates and variables on the drawing and it will make sence

The rectangle with the mouse will always go wrong in this case, MouseX is a variable this means it will constantly update and change, don’t use 880 but rather: mouseX+300

ok thank you
please stay onlin for 5 mins more

If i do Mouse X +300 than it kinds of works but it only works with the topleft corner and that also 300 far than the rectangle

Here are my whole codes:


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 > mouseX+300 || bally < 880){
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;
}
}

You should also include the code for the Y-axis it will work better. you only want the ball to bounce when it hits your rectangle.

I have written the y axis code

what exactly do you mean can you write it out in codes

and by the way thank you for helping me out, and sorry for troubling you so much

OMG Ijust saw something really stupid,
In my firstreply Imade a mistake:

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

|| means or, it should be && and.

the code should be

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

sorry for this.

and if it only has to bounce on the rectangle and you add the Y-axis:
if( ballx > 810 && balx < 1110 && bally <100){
xspeed = xspeed*-1
}

sorry, this is confusing can you write it a bit more neatly