# Ball to bounce of an object

**URL:** <https://forum.kirupa.com/t/ball-to-bounce-of-an-object/456227>\
**Category:** flash\
**Created:** [January 2, 2015, 10:32pm UTC](https://forum.kirupa.com/t/ball-to-bounce-of-an-object/456227 "2015-01-02T22:32:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnien2](https://avatars.discourse-cdn.com/v4/letter/j/a8b319/32.png) [@johnnien2](https://forum.kirupa.com/u/johnnien2)\
**Post date:** [January 2, 2015, 10:32pm UTC](https://forum.kirupa.com/t/ball-to-bounce-of-an-object/456227/1 "2015-01-02T22:32:54Z")

</div>

Hello!

I need help with a ball colliding correctly with a square on stage. This is the file: [https://app.box.com/s/ui4q15juyp9iqszmm95g](https://app.box.com/s/ui4q15juyp9iqszmm95g)  
It’s basically a ball bouncing of four walls and hitting 2 object incorrectly. Because there is a quite a bit of code relevant to the ball hitting the walls I am only posting the code for the object on the main timeline.

> var ballXSpeed:Number = 8;//X Speed of the Ball  
> var ballYSpeed:Number = 8;//Y Speed of the Ball  
> mcBall.addEventListener(Event.ENTER\_FRAME, moveBall);  
> function moveBall(event:Event):void {  
> mcBall.x += ballXSpeed;//Move the ball horizontally  
> mcBall.y += ballYSpeed;//Move the ball vertically

and inside the object _(brick .as)_

> if (this.hitTestObject(\_main.mcBall))  
> {  
> \_main.ballYSpeed \*= -1;

What code do I need to change inside _brick .as_ to fix this?  
Thanks!

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [January 5, 2015, 6:25pm UTC](https://forum.kirupa.com/t/ball-to-bounce-of-an-object/456227/2 "2015-01-05T18:25:26Z")

</div>

If you’re struggling with writing your own collision physics, it might be a good idea to look into a library or framework that has that stuff written for you already, like [Box2D](http://www.box2dflash.org/). I usually find it difficult to wade into someone else’s hand-rolled physics code, because there are a lot of simplifying assumptions that people make, which usually aren’t expressed well in one-time-use code.

Edit: For example, your expression `_main.ballYSpeed *= -1` is, I think, a simplification of a vector cross product, since you’re kind of special-casing the physics of the situation. But that makes it harder to read through the logic of your code, since it’s depending on contextual details like the static position and orientation of the bricks.

---

<div class="post-metadata">

**Author:** ![johnnien2](https://avatars.discourse-cdn.com/v4/letter/j/a8b319/32.png) [@johnnien2](https://forum.kirupa.com/u/johnnien2)\
**Post date:** [January 6, 2015, 8:43pm UTC](https://forum.kirupa.com/t/ball-to-bounce-of-an-object/456227/3 "2015-01-06T20:43:36Z")

</div>

I understand… Thank you for answering me!
