NullPointerException when objects collide

Hi there, first time poster, really like the engine, all that stuff :wink:



I’m testing a simple case with the physics, a box is falling down to hit the terrain (other boxes). But every time I get this exception:

[java]2011-3-13 15:24:39 com.jme3.app.Application handleError

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.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:341)

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

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

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

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

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

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

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

at java.lang.Thread.run(Thread.java:662)[/java]





Here is my code :

When you run it, wait for a sec or two to observe the box hitting the terrain.



[java]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.material.Material;

import com.jme3.material.RenderState.FaceCullMode;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.shape.Box;

import com.jme3.math.ColorRGBA;

import com.jme3.scene.Node;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Random;

import jme3tools.optimize.GeometryBatchFactory;





public class CollisionProblem extends SimpleApplication {



private BulletAppState bulletAppState;

private RigidBodyControl landscape;

RigidBodyControl rbc;



public static void main(String[] args) {

CollisionProblem app = new CollisionProblem();

app.start();

}



@Override

public void simpleInitApp() {

flyCam.setMoveSpeed(10);



bulletAppState = new BulletAppState();

stateManager.attach(bulletAppState);



createTerrain();



Box box1 = new Box(new Vector3f(45, 100, 45), 2, 1, 2);

Geometry blue = new Geometry(“Box”, box1);

Material mat1 = new Material(assetManager, “Common/MatDefs/Misc/SolidColor.j3md”);

mat1.setColor(“m_Color”, ColorRGBA.Blue);

blue.setMaterial(mat1);

rootNode.attachChild(blue);



CollisionShape shape = CollisionShapeFactory.createMeshShape(blue);

rbc = new RigidBodyControl(shape, 1);

blue.addControl(rbc);





cam.setLocation(new Vector3f(5, 4, 5));

cam.setDirection(new Vector3f(4, 2, 5).normalize());





bulletAppState.getPhysicsSpace().add(blue);

bulletAppState.getPhysicsSpace().add(landscape);

}



void createTerrain() {

Random r = new Random();



ArrayList list = new ArrayList();

Node terrain = new Node();



for (int y = 0; y < 50; y++) {

for (int x = 0; x < 50; x++) {

Box box1 = new Box(new Vector3f(x, 0, y), 0.50f, 0.50f, 0.50f);

Geometry geom = new Geometry(“Box”, box1);

geom.setMaterial(getMaterialForInt(r.nextInt(3)));

list.add(geom);

}

}



final List<Geometry> makeBatches = GeometryBatchFactory.makeBatches(list);

for (final Geometry geometry : makeBatches) {

terrain.attachChild(geometry);

}

CollisionShape sceneShape = CollisionShapeFactory.createMeshShape((Node) terrain);

landscape = new RigidBodyControl(sceneShape, 0);

terrain.addControl(landscape);

rootNode.attachChild(terrain);



}

private HashMap mats = new HashMap();



private Material getMaterialForInt(int i) {

if (!mats.containsKey(i)) {

Material mat1 = new Material(assetManager, “Common/MatDefs/Misc/SolidColor.j3md”);

mat1.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Back);

mat1.setColor(“Color”, ColorRGBA.randomColor());

mats.put(i, mat1);

return mat1;

}

return (Material) mats.get(i);

}

}[/java]

You add the physics object twice.

Sorry but I can’t see where. I add each physics object once:

[java] bulletAppState.getPhysicsSpace().add(blue);

bulletAppState.getPhysicsSpace().add(landscape);[/java]

If I comment either of these lines, there is no collision.

Can you elaborate?

Oh, sorry. The problem is you try to collide mesh vs mesh. MeshCollisionShape can only be used for static objects.

1 Like

Thanks, that was it. :slight_smile:

Now I have to figure out how to make the object to not fall through.

Setting the terrain Node as Kinematic didn’t help, neither CcdMotionThreshold on the falling object.

Maybe the problem is that my terrain is composite, but I don’s see gaps.

Use physicsSpace.enableDebug(assetManager) to see whats happening.

1 Like