Bounce off on Contact

How do I make and object bounce in the oposite direction when it hits X object? For instance I have a ball in the middle of the field, and when it hits any one of the four sides it stops on that side. I want it to bounce off without gravity pulling it down to the bottom, and bounce off in the oposite direction with equal force and not loose momentum?

Is this in the manual? I couldn't find it. I've also spent hours on Google for collision detection and can't find it ha ha.

Thanks.
Set the restitution property on your body. A restitution of 1 should mean it bounces off with the same energy it hits.
Sweet thanks. Should of found it 😳

Quick other question, right now since there is zero gravity on my object, I have to start it moving, I want it to move at all times and I would like a small amount of speed. Where is this setting at?
Once again found it! 🙂 ha ha

Thanks for the help.
Ok so I got all of that working and good. Now quick question regarding the same thing. How do I make it so that the ball running around on the screen when it hits another ball BOTH balls just stop dead in their tracks? I tried setting the ball restitution to -1 and 0 but no go.
jfreak53 wrote:
Ok so I got all of that working and good. Now quick question regarding the same thing. How do I make it so that the ball running around on the screen when it hits another ball BOTH balls just stop dead in their tracks? I tried setting the ball restitution to -1 and 0 but no go.


Ok use a contact event.

private function stopObjectsOnColide(e:ContactEvent)
{
BodyShape(e.currentTarget).linearVelocityX = 0;
BodyShape(e.currentTarget).linearVelocityY = 0;
BodyShape(e.relatedObject).linearVelocityX = 0;
BodyShape(e.relatedObject).linearVelocityY = 0;
}

Then assign that to the objects.

ball.addEventListener(ContactEvent.BEGIN_CONTACT, stopObjectsOnColide);

Then when the objects hit, they'll both stop and fall due to gravity.

In my mind, that would work. But I'm at school and can't test it.

EDIT:
Also make sure to have reportBeginContact=true; on the objects
Beautiful!! Thank you. I had it working using linearDamping and setting it to 100 on the collision event, that was working but not perfectly the way I wanted. Don't know why I didn't think of using velocity, since that's what I set when the game starts to get them going ha ha

Thanks again for the help!
jfreak53 wrote:
Beautiful!! Thank you. I had it working using linearDamping and setting it to 100 on the collision event, that was working but not perfectly the way I wanted. Don't know why I didn't think of using velocity, since that's what I set when the game starts to get them going ha ha

Thanks again for the help!


Haha, any time.

Original thread: https://box2d.org/forum/viewtopic.php?t=6968