Bouncing a ball against another ball

Hi guys,

I have two rounded objects, ball A and ball B.

When ball A collides with ball B, I want ball A to ‘bounce away’ from ball B, i.e. get an impulse relative to ball B x,y (imagine if you touch a bomb you would blow away of the bomb x,y).

The impulse amount would be a value provided by me, to determine the bouncing amount.

I’m using applyLinearImpulse inside my collision event in ball A, but I don’t know what math should I use to calculate the 4 parameters applyLinearImpulse needs (forcex, forcey, bodyx, bodyy) to get it right.

Could you guys help me out?

Thanks!
[import]uid: 37667 topic_id: 35228 reply_id: 335228[/import]

Hi @ramis2K,
Sure thing, this is relatively easy. You should find the positional difference (mathematical) between the two balls at collision (both X and Y of course) and then apply those values as an impulse to ball A… just heavily scaled down because linear impulses use very small fractional force values.

So, at collision, lets say ball A is at 0,0 and ball B is at 10,5 :

X difference = 0 - 10 … so, -10
Y difference = 0 - 5 … so, -5

Scale both down by maybe 1000 (you must experiment to determine the proper value for your scenario and the amount of bounce-off) : -10/1000 = -0.01, and -5/1000 = -0.005.

Apply those impulses to ball A, and it “springs off” in the direction opposite to ball B’s center.

The last two params for the impulse are just ball A’s center, since you’re applying basic central impulse, not an impulse on/near the ball’s outer edge. So, just “ballA.x” and “ballA.y”.

Brent [import]uid: 200026 topic_id: 35228 reply_id: 140053[/import]

Hi @ramis2K,
Sure thing, this is relatively easy. You should find the positional difference (mathematical) between the two balls at collision (both X and Y of course) and then apply those values as an impulse to ball A… just heavily scaled down because linear impulses use very small fractional force values.

So, at collision, lets say ball A is at 0,0 and ball B is at 10,5 :

X difference = 0 - 10 … so, -10
Y difference = 0 - 5 … so, -5

Scale both down by maybe 1000 (you must experiment to determine the proper value for your scenario and the amount of bounce-off) : -10/1000 = -0.01, and -5/1000 = -0.005.

Apply those impulses to ball A, and it “springs off” in the direction opposite to ball B’s center.

The last two params for the impulse are just ball A’s center, since you’re applying basic central impulse, not an impulse on/near the ball’s outer edge. So, just “ballA.x” and “ballA.y”.

Brent [import]uid: 200026 topic_id: 35228 reply_id: 140053[/import]