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

**URL:** <https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783>\
**Category:** Coding Questions\
**Created:** [November 28, 2020, 9:08am UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783 "2020-11-28T09:08:45Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Waka](https://avatars.discourse-cdn.com/v4/letter/w/f14d63/32.png) [@Waka](https://discourse.processing.org/u/Waka)\
**Post date:** [November 28, 2020, 12:44pm UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/21 "2020-11-28T12:44:05Z")

</div>

no i changed it to bally

---

<div class="post-metadata">

**Author:** ![Waka](https://avatars.discourse-cdn.com/v4/letter/w/f14d63/32.png) [@Waka](https://discourse.processing.org/u/Waka)\
**Post date:** [November 28, 2020, 12:44pm UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/22 "2020-11-28T12:44:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yanze03](https://avatars.discourse-cdn.com/v4/letter/y/e9a140/32.png) [@yanze03](https://discourse.processing.org/u/yanze03)\
**Post date:** [November 28, 2020, 12:45pm UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/23 "2020-11-28T12:45:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Waka](https://avatars.discourse-cdn.com/v4/letter/w/f14d63/32.png) [@Waka](https://discourse.processing.org/u/Waka)\
**Post date:** [November 28, 2020, 12:46pm UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/24 "2020-11-28T12:46:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Waka](https://avatars.discourse-cdn.com/v4/letter/w/f14d63/32.png) [@Waka](https://discourse.processing.org/u/Waka)\
**Post date:** [November 28, 2020, 12:46pm UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/25 "2020-11-28T12:46:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yanze03](https://avatars.discourse-cdn.com/v4/letter/y/e9a140/32.png) [@yanze03](https://discourse.processing.org/u/yanze03)\
**Post date:** [November 28, 2020, 12:47pm UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/26 "2020-11-28T12:47:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Waka](https://avatars.discourse-cdn.com/v4/letter/w/f14d63/32.png) [@Waka](https://discourse.processing.org/u/Waka)\
**Post date:** [November 28, 2020, 12:47pm UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/27 "2020-11-28T12:47:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Waka](https://avatars.discourse-cdn.com/v4/letter/w/f14d63/32.png) [@Waka](https://discourse.processing.org/u/Waka)\
**Post date:** [November 28, 2020, 12:48pm UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/28 "2020-11-28T12:48:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Waka](https://avatars.discourse-cdn.com/v4/letter/w/f14d63/32.png) [@Waka](https://discourse.processing.org/u/Waka)\
**Post date:** [November 28, 2020, 12:49pm UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/29 "2020-11-28T12:49:43Z")

</div>

Here is the full code, I have 2 rectangles:

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

```

---

<div class="post-metadata">

**Author:** ![yanze03](https://avatars.discourse-cdn.com/v4/letter/y/e9a140/32.png) [@yanze03](https://discourse.processing.org/u/yanze03)\
**Post date:** [November 28, 2020, 12:50pm UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/30 "2020-11-28T12:50:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yanze03](https://avatars.discourse-cdn.com/v4/letter/y/e9a140/32.png) [@yanze03](https://discourse.processing.org/u/yanze03)\
**Post date:** [November 28, 2020, 12:50pm UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/31 "2020-11-28T12:50:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Waka](https://avatars.discourse-cdn.com/v4/letter/w/f14d63/32.png) [@Waka](https://discourse.processing.org/u/Waka)\
**Post date:** [November 28, 2020, 12:51pm UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/32 "2020-11-28T12:51:37Z")

</div>

ok thank you  
please stay onlin for 5 mins more

---

<div class="post-metadata">

**Author:** ![Waka](https://avatars.discourse-cdn.com/v4/letter/w/f14d63/32.png) [@Waka](https://discourse.processing.org/u/Waka)\
**Post date:** [November 28, 2020, 12:53pm UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/33 "2020-11-28T12:53:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Waka](https://avatars.discourse-cdn.com/v4/letter/w/f14d63/32.png) [@Waka](https://discourse.processing.org/u/Waka)\
**Post date:** [November 28, 2020, 12:57pm UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/34 "2020-11-28T12:57:48Z")

</div>

Here are my whole codes:

```auto

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

```

---

<div class="post-metadata">

**Author:** ![yanze03](https://avatars.discourse-cdn.com/v4/letter/y/e9a140/32.png) [@yanze03](https://discourse.processing.org/u/yanze03)\
**Post date:** [November 28, 2020, 12:58pm UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/35 "2020-11-28T12:58:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Waka](https://avatars.discourse-cdn.com/v4/letter/w/f14d63/32.png) [@Waka](https://discourse.processing.org/u/Waka)\
**Post date:** [November 28, 2020, 12:59pm UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/36 "2020-11-28T12:59:22Z")

</div>

I have written the y axis code

---

<div class="post-metadata">

**Author:** ![Waka](https://avatars.discourse-cdn.com/v4/letter/w/f14d63/32.png) [@Waka](https://discourse.processing.org/u/Waka)\
**Post date:** [November 28, 2020, 12:59pm UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/37 "2020-11-28T12:59:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Waka](https://avatars.discourse-cdn.com/v4/letter/w/f14d63/32.png) [@Waka](https://discourse.processing.org/u/Waka)\
**Post date:** [November 28, 2020, 1:00pm UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/38 "2020-11-28T13:00:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yanze03](https://avatars.discourse-cdn.com/v4/letter/y/e9a140/32.png) [@yanze03](https://discourse.processing.org/u/yanze03)\
**Post date:** [November 28, 2020, 1:02pm UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/39 "2020-11-28T13:02:09Z")

</div>

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  
}

---

<div class="post-metadata">

**Author:** ![Waka](https://avatars.discourse-cdn.com/v4/letter/w/f14d63/32.png) [@Waka](https://discourse.processing.org/u/Waka)\
**Post date:** [November 28, 2020, 1:03pm UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/40 "2020-11-28T13:03:05Z")

</div>

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

[Previous page](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783.md?page=1)

[Next page](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783.md?page=3)
