Physicsgamestate example + GameControl Basic example

Hello friends,



I wanted to use BasicGameState with PhysicsGameState. I used this examples:



http://wiki.jmephysics.irrisor.net/tiki-index.php?page=PhysicsGameState



http://www.jmonkeyengine.com/wiki/doku.php/gamecontrol_basics



In original PhysicsGameState example is "falling box" above floor, in my program is under a falling left. I don`t understand why. I just put "everything" in  "SnakeGameState extends BasicGameState" constructor.


package pokus1;

import com.jmex.game.StandardGame;
import com.jmex.game.state.GameStateManager;

public class Main
{

   public static void main(String [] args)
   {

      // Create and start the OpenGL thread.
      final StandardGame game = new StandardGame("Physics tutorial");
      game.start();

      
      final SnakeGameState normalGameState = new SnakeGameState();

      // Now let jME know about the game states we've created.
      GameStateManager.getInstance().attachChild(normalGameState);
      normalGameState.setActive(true);

             
      
   }
}

package pokus1;

import com.jme.math.Vector3f;
import com.jme.scene.shape.Box;
import com.jmex.game.state.BasicGameState;
import com.jmex.game.state.GameStateManager;
import com.jmex.physics.DynamicPhysicsNode;
import com.jmex.physics.StaticPhysicsNode;
import com.jmex.physics.util.states.PhysicsGameState;


public class SnakeGameState extends BasicGameState {

    public SnakeGameState(){
        super("snakeGameState");
        // Create the PhysicsGameState object. This will track all state related to the physics
        // interactions.
        final PhysicsGameState physicsGameState = new PhysicsGameState("Physics tutorial");

        GameStateManager.getInstance().attachChild(physicsGameState);
        physicsGameState.setActive(true);

        // first we will create the floor
        // as the floor can't move we create a _static_ physics node
        StaticPhysicsNode staticNode = physicsGameState.getPhysicsSpace().createStaticNode();

        // attach the node to the root node to have it updated each frameit
        getRootNode().attachChild(staticNode);

        // now we do not create a collision geometry but a visual box
        final Box visualFloorBox = new Box("floor", new Vector3f(), 5, 0.25f, 5);
        // note: we have used the constructor (name, center, xExtent, yExtent, zExtent)
        // thus our box is centered at (0,0,0) and has size (10, 0.5f, 10)

        // we have to attach it to our node
        staticNode.attachChild(visualFloorBox);

        // now we let jME Physics 2 generate the collision geometry for our box
        staticNode.generatePhysicsGeometry();

        // second we create a box that should fall down on the floor
        // as the new box should move we create a _dynamic_ physics node
        DynamicPhysicsNode dynamicNode = physicsGameState.getPhysicsSpace().createDynamicNode();
        getRootNode().attachChild(dynamicNode);

        // again we create a visual box
        final Box visualFallingBox = new Box("falling box", new Vector3f(), 0.5f, 0.5f, 0.5f);
        // note: again we have used the constructor (name, center, xExtent, yExtent, zExtent)
        // thus our box is centered at (0,0,0) and has size (1, 1, 1)
        // the center is really important here because we want the center of the box to lie in the
        // center
        // of the dynamic physics node - which is the center of gravity!

        // attach it to the dynamic node
        dynamicNode.attachChild(visualFallingBox);

        // and generate collision geometries again
        dynamicNode.generatePhysicsGeometry();

        // we have to move our dynamic node upwards such that is does not start in but above the
        // floor
        dynamicNode.getLocalTranslation().set(0, 5, 0);
        // note: we do not move the collision geometry but the physics node!

        // now we have visuals for the physics and don't necessarily need to activate the physics
        // debugger
        // though you can do it (V key) to see physics in the app

        // Once all this is set up, we need to update the render state on the root node.
    

        getRootNode().updateRenderState();
    }
}

Adding



        getRootNode().updateWorldBound();

        getRootNode().updateGeometricState(0.0f, true);



helped.  :slight_smile: