Explosions

Anyone know what happened to the explosion manager mentioned here:



http://www.jmonkeyengine.com/jmeforum/index.php?topic=2587.0

http://www.jmonkeyengine.com/jmeforum/index.php?topic=1997.0



I can’t seem to find it in jMEphysics2. If it was removed, is there a replacement or code snippet for it?

I guess its not what your looking for, but there is an ExplosionFactory in jmetest.TutorialGuide but its just a convenience class to create the particle effect.



If you want that the particles appear physical, you can add a particle influence which adds a gravity effect.

You can implement a new explosion manager as PhysicsUpdateCallback. Have a look at FrictionCallback for inspiration.

Thanks for the quick responses. I’m putting together a simplified version of PAIN (http://pain.us.playstation.com/default.aspx) just to play around. Exploding crates seems like a good next step now that the ragdoll seems somewhat stable. Slow going for the new guy…



@Core-Dump: I’ll definitely need something cool looking once I get the physics worked out  :slight_smile: Looks like that class is jME 2 though as I don’t see it in 1.



@irrisor: Thanks, but the continual force (assuming I understand it correctly) looks like overkill for me at the moment. I’ll probably just create a bounding sphere and apply a one-time force to everything in it based on distance from the center.

Got a basic explosion working. After trying to get a BoundingSphere working all morning, I gave up and went to the jMEphysics1 source and updated ExplosionManager from Per Thulin. Here is the updated code if anyone is interested.


import com.jme.math.FastMath;
import com.jme.math.Vector3f;
import com.jmex.physics.DynamicPhysicsNode;
import com.jmex.physics.PhysicsNode;
import com.jmex.physics.PhysicsSpace;

/**
 * <code>ExplosionManager</code> contains a static method to simulate an explosion with a given
 * <b>force</b> and <b>radius</b> at a given <b>coordinate</b>.
 *
 * @author Per Thulin
 */
public class ExplosionManager {
   /** Temp variable to flatline memory usage. */
   private static final Vector3f distance = new Vector3f();

   /** Temp variable to flatline memory usage. */
   private static final Vector3f direction = new Vector3f();

   /** Temp variable to flatline memory usage. */
   private static final Vector3f forceToApply = new Vector3f();

   /**
   * A static method to simulate an explosion with a given
   * <b>force</b> and <b>radius</b> at a given <b>coordinate</b>.
   *
   * @param position
   *     The explosion center.
   * @param force
   *     The force of which an object right in the explosion center
   *     will be affected by. The applied force attenuates relative
   *     to the distance between the object and the explosion center.
   * @param radius
   *     The explosion radius. Objects outside this radius will
   *     not get affected.
   */
   public static void createExplosion(PhysicsSpace space, Vector3f position, float force, float radius) {
      // Loop through all the objects in the physics world and apply an
      // explosion force.
        for (PhysicsNode obj : space.getNodes()) {
         // Escape 1: if the object is static.
           if (obj.isStatic()) continue;

         // Calculate the distance between the object and the explosion centre.
         obj.getChild(0).getWorldTranslation().subtract(position, distance);

         // Calculate the direction vector between the explosion centre
         // and the object.
         direction.set(distance);
         direction.normalizeLocal();

         distance.x = FastMath.abs(distance.x);
         distance.y = FastMath.abs(distance.y);
         distance.z = FastMath.abs(distance.z);

         // Escape 2: if the object is outside of the explosion radius. Maybe
         // this is a little unnecessary, but will save computations in a
         // scene with many objects spread out.
         if (distance.x > radius || distance.y > radius || distance.z > radius) continue;

         // Calculate the force to apply. The force should attenuate
         // relative to the distance between the object and the
         // explosion center.
         forceToApply.x = (1 - (distance.x/radius)) * force;
         forceToApply.y = (1 - (distance.y/radius)) * force;
         forceToApply.z = (1 - (distance.z/radius)) * force;
         forceToApply.multLocal(direction);

         // Apply the force.
         ((DynamicPhysicsNode)obj).addForce(forceToApply);
        }
   }
}



Here is an example of how I am invoking. I'm using a SyntheticButton action. If anyone has a better solution, please let me know. I haven't added any visual feedback yet (other than things around it getting thrown).

Synthetic Button setup:


// Build an explosive crate.
DynamicPhysicsNode explosive = util.buildDynamicBlock("Crate", -2f, 4f, -50f,
    4f, 4f, 4f, Material.WOOD, null, tsCrate, getPhysicsSpace());
rootNode.attachChild(explosive);
// Listen for this block being hit.
final SyntheticButton collisionEventHandler = explosive.getCollisionEventHandler();
input.addAction( new ExplosionAction(explosive), collisionEventHandler, false );



Action:


/** This action will produce an explosion which destroys the given node. */
private class ExplosionAction extends InputAction {
       private DynamicPhysicsNode node = null;
       private boolean hasBeenInvoked = false;

       public ExplosionAction(DynamicPhysicsNode node) {
          this.node = node;
       }

        public void performAction(InputActionEvent evt) {
           // Here to prevent multiple entries for the same instance.
           // Seem to get 5 of these (including original) for same explosion. No sure why.
           // Maybe different parts of ragdoll hitting it.
           if (hasBeenInvoked) return;
           hasBeenInvoked = true;

           // Cache values.
           PhysicsSpace space = node.getSpace();
           Vector3f center = node.getWorldTranslation();

           // Remove the node that has just exploded.
           node.setActive(false);
           node.removeFromParent();
           rootNode.updateGeometricState(evt.getTime(), true);

           // Cause explosion.
           ExplosionManager.createExplosion(space, center, 10000f, 50f);
        }
    }


thats cool, its fun to play around :slight_smile:



http://de.youtube.com/watch?v=CyyDnXhpBoI

:smiley:



I'd mixed some of the ExplosionFactory particle stuff as well. Still tweaking the parms, but it looks cool.



You may want to call the rest method on your boxes so they don't slide around before the explosion hits. I just found that yesterday and it keeps my ice cubes in place  :slight_smile: