Player falls through terrain

Hey - I have a problem.



My player falls through my terrain.



This is my terrain code:



private PhysicsNode setupPlayer() {
      Spatial board = assetManager
            .loadModel("resources/models/snowboardf.mesh.xml");

   
      DirectionalLight dl = new DirectionalLight();
      
      dl.setColor(ColorRGBA.White);
      dl.setDirection(new Vector3f(0,-1,-0));
      
      BoundingBox boundingBox = new BoundingBox();
      board.setModelBound(boundingBox);
      playerNode = new PhysicsNode(board, new BoxCollisionShape(new Vector3f(boundingBox.getXExtent(), boundingBox.getYExtent(), boundingBox.getZExtent())), 1f);
      playerNode.setName("player");
      playerNode.setFriction(0.1f);
      playerNode.setLocalTranslation(new Vector3f(0, 30, 0));
   
      playerNode.addLight(dl);
      // SnowParticle snow = new SnowParticle(assetManager);
      // playerNode.attachChild(snow);
      snowUtils.rotateModel180ZAxis(playerNode);
      getPhysicsSpace().add(playerNode);
      rootNode.attachChild(playerNode);

      return playerNode;
   }



This is my player code:


private PhysicsNode setupPlayer() {
      Spatial board = assetManager
            .loadModel("resources/models/snowboardf.mesh.xml");

   
      DirectionalLight dl = new DirectionalLight();
      
      dl.setColor(ColorRGBA.White);
      dl.setDirection(new Vector3f(0,-1,-0));
      
      BoundingBox boundingBox = new BoundingBox();
      board.setModelBound(boundingBox);
      playerNode = new PhysicsNode(board, new BoxCollisionShape(new Vector3f(boundingBox.getXExtent(), boundingBox.getYExtent(), boundingBox.getZExtent())), 1f);
      playerNode.setName("player");
      playerNode.setFriction(0.1f);
      playerNode.setLocalTranslation(new Vector3f(0, 30, 0));
   
      playerNode.addLight(dl);
      // SnowParticle snow = new SnowParticle(assetManager);
      // playerNode.attachChild(snow);
      snowUtils.rotateModel180ZAxis(playerNode);
      getPhysicsSpace().add(playerNode);
      rootNode.attachChild(playerNode);

      return playerNode;
   }



Can somebody please tell me how to stop this from happening! This Physics is starting to frustrate me!

You linked your setupPlayer code twice

post your terrain init

apologies, terrain:



private Node setupHill() {

      Spatial scene = assetManager
            .loadModel("resources/models/slope1.mesh.xml");
      

      // assetManager.
      DirectionalLight sun = new DirectionalLight();
      sun.setDirection(new Vector3f(-0.4790551f, -0.39247334f, -0.7851566f));
      sun.setColor(ColorRGBA.White.clone().multLocal(2));
      scene.addLight(sun);

      CollisionShape hillCollisionShape = CollisionShapeFactory
            .createMeshShape(scene);
      hillNode = new PhysicsNode(scene, hillCollisionShape, 0);
      hillNode.setName("hill");
      hillNode.setFriction(0.1f);
      hillNode.setLocalScale(0.1f);
      snowUtils.rotateModel90YAxis(hillNode);
      hillNode.setLocalTranslation(new Vector3f(0,-60,0));
      //hillNode.center();
   

      hillNode.updateGeometricState();
      hillNode.updateModelBound();
      hillNode.updatePhysicsState();
      rootNode.attachChild(hillNode);
      getPhysicsSpace().add(hillNode);
      return hillNode;
   }

The problem is the setlocalscale, the PhysicsNode CollisionShape will not scale with this call as it could be used by another physics node as well. Scale the geometry before and then create the collisionshape from it.



Hope this helps,

Normen

AHHHH!!! That makes sense! You have made my evening - thank you! :smiley: :smiley:

What about things like changing the models rotation?? If i change that after making the physics node using .setLocalRotation does that screw stuff over too?



I now have a new problem - I have changed both the terrain and player init - so these are pasted below.



I have included a pic of the problem - basically the board starts in the air, floats to the surface of the terrain and then starts sliding rapidly towards the edge before everything going insane …could this be gravity going in the wrong direction!? Im confused by my ability to tell what way up things are sometimes ?



Cheers,



Andy



Terrain:


private Node setupHill() {

      Spatial scene = assetManager
            .loadModel("resources/models/slope1.mesh.xml");
      scene.setLocalTranslation(new Vector3f(0,-10,0));
      snowUtils.rotateModelOnAxis(scene,90,"x");

      CollisionShape hillCollisionShape = CollisionShapeFactory
            .createDynamicMeshShape(scene);
      hillNode = new PhysicsNode(scene, hillCollisionShape, 0);
      hillNode.setName("hill");
      hillNode.setFriction(0.5f);
      DirectionalLight sun = new DirectionalLight();
      sun.setDirection(new Vector3f(-0.4790551f, -0.39247334f, -0.7851566f));
      sun.setColor(ColorRGBA.White.clone().multLocal(2));
      hillNode.addLight(sun);
      hillNode.updateGeometricState();
      hillNode.updateModelBound();
      hillNode.updatePhysicsState();
      rootNode.attachChild(hillNode);
      getPhysicsSpace().add(hillNode);
      return hillNode;
   }



Player


   private PhysicsNode setupPlayer() {
      Node board = (Node) assetManager
            .loadModel("resources/models/snowboardf.mesh.xml");
      board.setLocalTranslation(new Vector3f(60, 1, 40));
      
      snowUtils.rotateModelOnAxis(board,180,"z");
   
      CollisionShape cl = CollisionShapeFactory.createBoxCompoundShape(board);
   
      playerNode = new PhysicsNode(board, cl, 1f);
      playerNode.setName("player");
      
      DirectionalLight dl = new DirectionalLight();

      dl.setColor(ColorRGBA.White);
      dl.setDirection(new Vector3f(0,-1,0));
   

      playerNode.addLight(dl);
      // SnowParticle snow = new SnowParticle(assetManager);
      // playerNode.attachChild(snow);
      
      playerNode.updateGeometricState();
      playerNode.updateModelBound();
      playerNode.updatePhysicsState();
      rootNode.attachChild(playerNode);
      getPhysicsSpace().add(playerNode);
      return playerNode;
   }



Image of whats happening:

Looks pretty good to me, i don't understand the problem.



the board make contact with the terrain, and slide down the slope… that's snowbording no?  :wink:



If you want it to stop when making contact with the slope use the setFriction() method.

Set it to 100 and it won't slide at all…

In this case its wrong to rotate the Geometry before or even at all. The visible geometry should have a local rotation and location of zero. The PhysicsNode is the thing that moves, if you attach the geometry with a local rotation it will be wrong as this rotation is added to the rotation of the physics object.



Again: You have the PhysicsNode that moves, you give it a collision shape you derive from a Geometry and then add the Geometry as a child node to the PhysicsNode. The example for center of mass here visualizes this relation a bit, maybe that helps.



Also, you should not use GImpact collision shapes (“dynamic” or “movable” mesh collision shapes) for terrain, use createMeshCollisionShape.

So - if i need to rotate my terrain becuase its the wrong way up - how do i do this correctly so it doesnt screw over the physics?

The PhysicsNode has the CollisionShape and the Geometry is a child node that rotates with it - guess.

Well im confused - how do I solve this problem?



private Node setupHill() {

      Spatial scene = assetManager
            .loadModel("resources/models/slope1.mesh.xml");

      CollisionShape hillCollisionShape = CollisionShapeFactory
            .createMeshCollisionShape(scene);

      hillNode = new PhysicsNode(scene, hillCollisionShape, 0);
      hillNode.setLocalTranslation(new Vector3f(0,-10,0));
      snowUtils.rotateModelOnAxis(hillNode,90,"x");
      DirectionalLight sun = new DirectionalLight();
      sun.setDirection(new Vector3f(-0.4790551f, -0.39247334f, -0.7851566f));
      sun.setColor(ColorRGBA.White.clone().multLocal(2));
      hillNode.addLight(sun);
      hillNode.updateGeometricState();
      hillNode.updateModelBound();
      hillNode.updatePhysicsState();
      rootNode.attachChild(hillNode);
      getPhysicsSpace().add(hillNode);
      return hillNode;
   }


   private PhysicsNode setupPlayer() {
      Node board = (Node) assetManager
            .loadModel("resources/models/snowboardf.mesh.xml");
      
      CollisionShape cl = CollisionShapeFactory.createBoxCompoundShape(board);
   
      playerNode = new PhysicsNode(board, cl, 1f);
      playerNode.setName("player");
      playerNode.setLocalTranslation(new Vector3f(60, 1, 40));
      snowUtils.rotateModelOnAxis(playerNode,180,"z");
      
      DirectionalLight dl = new DirectionalLight();

      dl.setColor(ColorRGBA.White);
      dl.setDirection(new Vector3f(0,-1,0));

      playerNode.addLight(dl);
      // SnowParticle snow = new SnowParticle(assetManager);
      // playerNode.attachChild(snow);
      
      playerNode.updateGeometricState();
      playerNode.updateModelBound();
      playerNode.updatePhysicsState();
      rootNode.attachChild(playerNode);
      getPhysicsSpace().add(playerNode);
      return playerNode;
   }

Right, well the method createMeshCollisionShape doesnt actually exist in the CollisionShapeFactory… Im presuming you mean createMeshShape - but using this my board continues to fall through the terrain again.