Saturday, February 6, 2016

xna - How can I create a bomb explosion in Farseer that pushes nearby objects away?


I'm trying to create a bomb explosion in Farseer. I created a circle for the bomb. However, if the bomb explodes, the objects (bodies) that are around it should fly away because they're pushed back by the enormous force of the explosion. I'm not sure how to create that force, though - how can I do it?



Answer




I would recommend checking out the explosion iforce2d page. It does a great job going over the various ways of handling explosions with the performance vs. accuracy tradeoffs. It goes over 3 methods which I will summarize here. So this is simply me summarizing information I found while researching and none of it is my own work.


Also you want to apply impulses here not forces. To understand why read this.


The Proximity method


The Proximity method


This is the same method that Byte56 goes over in his answer. The concept is simple. Have an explosion point and apply an impulse in the opposite direction away from the point which will affect all objects with a strength based on their distance away.


Pros:



  • Very fast

  • Easy to code.



Cons:



  • Explosions pass through immovable objects and static bodies.

  • Objects that are in the influence area of the blast may be skipped if you only check the distance to the center of mass.

  • Surface area is not taken into account.

  • Everything is affected at once.


The Raycast method


The Raycast method


This one is simple enough, cast out rays from the origin of your explosion and apply impulses when they collide into an object with an impulse dictated by the distance.



Pros:



  • Less performance intensive than the projectiles method.

  • Explosion blocked by objects.

  • Surface area taken into account.


Cons:



  • Everything is affected at once.

  • The larger the explosion the more rays you need to make sure the explosion affects everything.


  • More performance intensive than the proximity method.


The Particle method


The Particle method


My personal favorite. Similar to the RaySasting method but instead of rays you leverage the physics engine and spit out small pellets that interact with your scene.


Pros:



  • Extremely easy code-wise. Generate the balls and apply an impulse to each one. You have no code to write per tick. Just have a timer that goes though and disables/resets the bodies after the explosion is complete.

  • Explosion is blocked by objects.

  • Surface area is taken into account.


  • Explosion reflects off of surfaces.

  • Objects further from the blast respond to it more slowly.

  • You can use the balls as the basis for generating partials allowing your effects to accurately reflect the explosions impact.


Cons:



  • Adds per-tick complexity.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...