# 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:** 7
**Page:** 4

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [November 28, 2020, 4:41pm UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/61 "2020-11-28T16:41:31Z")

</div>

> [@Waka](#):
>
> Can you help me fix them.

Interesting question because as a _ **very** _ experienced programmer I can see many problems / issues with your code (apart from the collision detection) which occur frequently with novice programmers. So I would not be interested at hacking your code just to get it to work. It would be like trying to weld a patch over a hole in a rusty bucket - cover one hole only to make another hole elsewhere.

If you are interested I can show you how to create a simple sketch that does what you are looking for using good programming practice. It would involve looking more closely at structured programming and algorithm design especially the collision detection algorithm to produce more generic code.

By the time we were finished you will have learnt a lot about structured programming, algorithm design and have a good working program.

The downside is that it would take several days to do this in sufficient detail, so its up to you to decide what you want to get out of doing 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 29, 2020, 7:40am UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/62 "2020-11-29T07:40:57Z")

</div>

Hi @quark, Sorry for the Late Reply. I think hacking my code just to get it to work is better. But could you also please tell me what other problems you have identified in this.

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [November 29, 2020, 11:21am UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/63 "2020-11-29T11:21:45Z")

</div>

> [@Waka](#):
>
> I think hacking my code just to get it to work is better

Good luck with that.

> [@Waka](#):
>
> what other problems you have identified in this.

I will identify one - [magic numbers](https://en.wikipedia.org/wiki/Magic_number_(programming)) - in this case you are using numbers directly to represent the box position and size for

1. displaying the rectangle
2. collision detection

It means that if you want to change the position or size of the rectangle then you have _remember_ to modify the collision detection. Not only that look at this code …

```auto
  if (ballx <= mouseX+300 && bally > 880 && bally < 955) {
    xspeed = xspeed*-1;
  }

```

what is the _meaning_ of `300`, `880` and `955`, anyone reading this code would have no idea. It is better to use named variables or named constants.

Other issues are related to program structure so it is not worth my time discussing them if all you want to do is hack the code you have.

Final words of advice - hacking code to get something to work does not beat having a good program design/structure and a clear understanding of the algorithms used.

Bonne chance 😃

---

<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 29, 2020, 11:35am UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/64 "2020-11-29T11:35:07Z")

</div>

Thank You for identifying all of these problems but can you actually help me hacking my codes.  
Thank You

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [November 29, 2020, 2:05pm UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/65 "2020-11-29T14:05:03Z")

</div>

Just to remind you of what I said in a previous post

> [@quark](#):
>
> So I would not be interested at hacking your code just to get it to work. It would be like trying to weld a patch over a hole in a rusty bucket - cover one hole only to make another hole elsewhere.

---

<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 29, 2020, 2:15pm UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/66 "2020-11-29T14:15:46Z")

</div>

So am I allowed to open a new tread for solving this problem

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [November 29, 2020, 5:05pm UTC](https://discourse.processing.org/t/this-ball-goes-through-the-rectangle-rather-than-hitting-it-or-bouncing-it/25783/67 "2020-11-29T17:05:50Z")

</div>

That is a good idea as this one is very long so might put people off reading it but _ **do not delete anything in this discussion** _ 😃

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