NPE when objects collides with terrain

Hello,

i try to implement the physics and createded a small test. This is the code:

[java]

/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.

    */

    package TestCases;



    import com.jme3.app.SimpleApplication;

    import com.jme3.bullet.BulletAppState;

    import com.jme3.bullet.collision.shapes.CollisionShape;

    import com.jme3.bullet.control.RigidBodyControl;

    import com.jme3.bullet.util.CollisionShapeFactory;

    import com.jme3.light.DirectionalLight;

    import com.jme3.math.ColorRGBA;

    import com.jme3.math.Vector3f;

    import com.jme3.renderer.RenderManager;

    import com.jme3.scene.Node;

    import com.jme3.scene.Spatial;

    import com.jme3.terrain.Terrain;

    import com.jme3.terrain.geomipmap.TerrainQuad;



    /**

    *
  • @author zzuegg

    */

    public class TankTest extends SimpleApplication {



    public BulletAppState bulletAppState;



    public static void main(String[] args) {

    TankTest app = new TankTest();

    app.start();

    }



    @Override

    public void simpleInitApp() {

    this.flyCam.setMoveSpeed(100f);

    bulletAppState = new BulletAppState();

    stateManager.attach(bulletAppState);

    this.bulletAppState.getPhysicsSpace().enableDebug(assetManager);



    Node terrain=(Node) this.assetManager.loadAsset("Scenes/Test/TestScene.j3o");



    CollisionShape terrainShape = CollisionShapeFactory.createMeshShape(terrain);

    RigidBodyControl landscape = new RigidBodyControl(terrainShape, 0);

    terrain.addControl(landscape);



    Spatial tank=this.assetManager.loadModel("Models/Test/tank_ultralight1.blend");

    CollisionShape modelCollisionShape= CollisionShapeFactory.createMeshShape(tank);

    RigidBodyControl tankBodyControl = new RigidBodyControl(modelCollisionShape,2f);

    tank.addControl(tankBodyControl);





    DirectionalLight sun=new DirectionalLight();

    sun.setColor(ColorRGBA.White);

    sun.setDirection(new Vector3f(-0.1f,-0.1f,-0.1f));

    this.rootNode.addLight(sun);

    this.rootNode.attachChild(terrain);

    this.rootNode.attachChild(tank);



    tank.setLocalTranslation(0, 100f, 0);

    tankBodyControl.setPhysicsLocation(tank.getWorldTranslation());

    this.bulletAppState.getPhysicsSpace().add(terrain);

    this.bulletAppState.getPhysicsSpace().add(tank);



    }



    @Override

    public void simpleUpdate(float tpf) {

    //TODO: add update code

    }



    @Override

    public void simpleRender(RenderManager rm) {

    //TODO: add render code

    }



    public BulletAppState getBulletAppState() {

    return bulletAppState;

    }



    }

    [/java]



    Nothing special in it. Loading a scene, applying the physics, loading a model, and applying the physics. This works well, the scene and model have physics applyed to it. (as i can see on the debug screen) The model falls down correctly, but as soon as the terrain gets hit i get this null pointer exeption:

    [java]

    SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]

    java.lang.NullPointerException

    at com.bulletphysics.collision.dispatch.CollisionDispatcher.freeCollisionAlgorithm(CollisionDispatcher.java:119)

    at com.bulletphysics.collision.dispatch.CompoundCollisionAlgorithm.destroy(CompoundCollisionAlgorithm.java:76)

    at com.bulletphysics.collision.dispatch.CollisionDispatcher.freeCollisionAlgorithm(CollisionDispatcher.java:120)

    at com.bulletphysics.collision.dispatch.CompoundCollisionAlgorithm.destroy(CompoundCollisionAlgorithm.java:76)

    at com.bulletphysics.collision.dispatch.CollisionDispatcher.freeCollisionAlgorithm(CollisionDispatcher.java:120)

    at com.bulletphysics.collision.broadphase.HashedOverlappingPairCache.cleanOverlappingPair(HashedOverlappingPairCache.java:219)

    at com.bulletphysics.collision.broadphase.HashedOverlappingPairCache.removeOverlappingPair(HashedOverlappingPairCache.java:91)

    at com.bulletphysics.collision.broadphase.DbvtBroadphase.collide(DbvtBroadphase.java:145)

    at com.bulletphysics.collision.broadphase.DbvtBroadphase.calculateOverlappingPairs(DbvtBroadphase.java:235)

    at com.bulletphysics.collision.dispatch.CollisionWorld.performDiscreteCollisionDetection(CollisionWorld.java:139)

    at com.bulletphysics.dynamics.DiscreteDynamicsWorld.internalSingleStepSimulation(DiscreteDynamicsWorld.java:378)

    at com.bulletphysics.dynamics.DiscreteDynamicsWorld.stepSimulation(DiscreteDynamicsWorld.java:339)

    at com.jme3.bullet.PhysicsSpace.update(PhysicsSpace.java:342)

    at com.jme3.bullet.PhysicsSpace.update(PhysicsSpace.java:329)

    at com.jme3.bullet.BulletAppState.render(BulletAppState.java:189)

    at com.jme3.app.state.AppStateManager.render(AppStateManager.java:169)

    at com.jme3.app.SimpleApplication.update(SimpleApplication.java:262)

    at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:149)

    at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)

    at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:223)

    at java.lang.Thread.run(Thread.java:722)

    [/java]



    Anyone knows why?
1 Like

I don’t know why, but with this test case we might be able to track the issue

Thanks.

@nehon: here is the asses folder including the scene and the model. http://www.file-upload.net/download-3827554/assets.zip.html

You probably remove or add an object twice to the physicsspace.

normen said:
You probably remove or add an object twice to the physicsspace.

No, the code above is all i use in this test

You are using a mesh shape for a dynamic object. :roll: Use createDynamicMeshShape for the tank or just let the constructor do that.

1 Like
normen said:
You are using a mesh shape for a dynamic object. :roll: Use createDynamicMeshShape for the tank or just let the constructor do that.

Indeed, i thought that as long as i do not move change the Mesh (animations...) i don't need to use a dynamic mesh shape

As stated everywhere, only static objects can have mesh collision shapes, use GImpact shapes if hull shapes aren’t enough.