DebugPhysicsGameState done / PhysicsDebugger broken

Hello again,



i just wanted to contribute this to the community. It's a drop in replacement of the PhysicsGameState which allows to render the physics debug overlay. Additionally it gives you a 'PAUSE' button for the physics simulation. (i really like the effect of being able to fly around in a frozen scene :wink: ). Anyways … here it is:


package de.fhaust.cc;

import java.util.logging.Logger;

import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.system.DisplaySystem;
import com.jmex.game.state.BasicGameState;
import com.jmex.physics.PhysicsDebugger;
import com.jmex.physics.PhysicsSpace;

/**
 * A dropin replacement to PhysicsGameState that allows debug graphics to be displayed
 * and stopping of the physics simulation.
 * Rendering is continued with the physics stopped.
 *
 * Key Mapping:
 *  O - pauses/unpauses the physics simulation
 *  V - toggles the physics debug overlay
 *
 * @author Florian Hofmann (florian@fhaust.de)
 *
 */
public class DebugPhysicsGameState extends BasicGameState {

   private PhysicsSpace physics;

   protected boolean showPhysics = false;
   protected boolean simulationPaused = false;

   /** Strings for commands */
   private static final String DEBUG_COMMAND = "toggle_physics_debug";
   private static final String PAUSE_COMMAND = "toggle_physics_pause";

   /** private logger */
   private static final Logger log = Logger
         .getLogger(DebugPhysicsGameState.class.getName());

   /**
    * Creates a new DebugPhysicsGamestate
    * handles input by default
    * @param name
    */
   public DebugPhysicsGameState(String name) {
      this(name, true);
      physics = PhysicsSpace.create();
   }

   /**
    * Creates a new DebugPhysicsGamestate
    * @param name
    * @param handleInput should we handle input?
    */
   public DebugPhysicsGameState(String name, boolean handleInput) {
      super(name);

      /** create key bindings if input handling is true */
      if (handleInput) {
         initKeyBindings();
      }

   }

   private void initKeyBindings() {
      /** Assign key V to action "toggle_physics_debug". */
      KeyBindingManager.getKeyBindingManager().set(DEBUG_COMMAND,
            KeyInput.KEY_V);

      /** Assign key O to action "toggle_physics_pause". */
      KeyBindingManager.getKeyBindingManager().set(PAUSE_COMMAND,
            KeyInput.KEY_O);

      log.info("press V to display physic debug graphicsn" +
            "press O to pause the simulation");
   }

   public void update(float tpf) {
      super.update(tpf);

      /** Run simulation if it is not paused */
      if (!simulationPaused)
         physics.update(tpf);

      /**
       * If toggle_physics_debug is a valid command (via key V), change
       * physics.
       */
      if (KeyBindingManager.getKeyBindingManager().isValidCommand(
            DEBUG_COMMAND, false)) {
         showPhysics = !showPhysics;
      }

      /**
       * If toggle_physics_pause is a valid command (via key V), change
       * physics.
       */
      if (KeyBindingManager.getKeyBindingManager().isValidCommand(
            PAUSE_COMMAND, false)) {
         simulationPaused = !simulationPaused;
      }
   }

   @Override
   public void render(float tpf) {
      super.render(tpf);

      /** display debug graphics for physics */
      if (showPhysics)
         PhysicsDebugger.drawPhysics(physics, DisplaySystem
               .getDisplaySystem().getRenderer());
   }

   /**
    * return the physics space
    * @return
    */
   public PhysicsSpace getPhysicsSpace() {
      return physics;
   }

   public void pauseSimulation(boolean simulationPaused) {
      this.simulationPaused = simulationPaused;
   }
   
   public void showPhysics(boolean showPhysics) {
      this.showPhysics = showPhysics;
   }

}



As you can see it doesn't do much so i'd consider it as quite stable. Hopefully someone could integrate it into the repository with a appropriate license section (and with a spellchecking of the comments ;) ).


In related news:

I have a big problem with the PhysicsDebugger: as long as the simulation is running all joints and forces are shown in the world origin (0,0,0) ... as soon as i stop it they snap right back where they belong. You can see this behavior in the attached picture. As far as i know the overlay works fine in SimpleGame and other Tests ... so i think this is related somehow to StandardGame

So far ...
Goliat

[edit:] added setter methods for showPhysics and pausePhysics