1
votes

I have a ball and a bomb. All I need to do is, when the ball collides with the bomb the bomb explodes pushing the ball away from the bomb in the opposite direction the ball was thrown in.

For instance, if the ball was thrown at a 45 degree angle and hits the bomb then, the bomb explodes and pushes the ball away from the bomb at a -45 degree angle.

Is that even possible? and if so could someone lead me in the right direction?

3

3 Answers

2
votes

It's possible. Implement your own b2ContactListener to find the bomb-ball collision. Then apply linear impulse to the ball according to it's and bomb positions.

0
votes

If you want to abandon the physics simulation altogether (which is pretty much what you wrote) then you can either just not use Box2D or you can use it only for collision detection.

To use it only for collision detection you just set your bodies as sensors and when a collision is detected you send it off at the angle you would like (subtract or add angles together, rolling over when the maximum of a range is met).

If you would like to use the physics simulation (makes much more sense to me) then just set them both with the correct masks to collide with each other and set up your restitution (bounciness) as you would like it. It sounds like you may want your ball to be a dynamic object and your bomb to be a static (I think that is what they call it).

Obviously, in either case you are going to need to set up any animations or particle emitters to trigger on collision.

0
votes

When the ball and the bomb collide, instead of applying linear impulse to the ball, you can try enlarging the bomb's size for a very short time. After enlarging the bomb's size you can destroy the bomb body.

Enlarged bomb body would push the ball in proper direction.