[SOLVED] Bullet physics randomly throws errors

Hello,
when I’m using jbullet, I have some random errors are appearing (even when a player is just walking). I have tried using different libraries from maven, such as:
cz.advel.jbullet : jbullet
com.github.stephengold : jbullet
com.github.stephengold : Libbulletjme
Now I’m using last, and I receive randomly these errors that can sometimes crash my program, or stop world generation thread:

java.lang.NullPointerException
        at com.bulletphysics.collision.dispatch.CollisionWorld$RayResultCallback.needsCollision(CollisionWorld.java:673)
        at com.bulletphysics.collision.dispatch.CollisionWorld.rayTest(CollisionWorld.java:555)
        at com.jme3.bullet.PhysicsSpace.rayTest(PhysicsSpace.java:781)
        at com.jme3.bullet.control.BetterCharacterControl.checkOnGround(BetterCharacterControl.java:503)
        at com.jme3.bullet.control.BetterCharacterControl.prePhysicsTick(BetterCharacterControl.java:199)
        at com.jme3.bullet.PhysicsSpace$3.internalTick(PhysicsSpace.java:318)
        ...

java.util.concurrent.ExecutionException: java.lang.NullPointerException
        at java.base/java.util.concurrent.FutureTask.report(FutureTask.java:122)
        at java.base/java.util.concurrent.FutureTask.get(FutureTask.java:191)
        at com.jme3.bullet.BulletAppState.postRender(BulletAppState.java:386)
        at com.jme3.app.state.AppStateManager.postRender(AppStateManager.java:381)
        at com.jme3.app.SimpleApplication.update(SimpleApplication.java:274)
        ...
Exception in thread "Chunk" java.lang.NullPointerException
        at com.jme3.bullet.PhysicsSpace.addRigidBody(PhysicsSpace.java:617)
        at com.jme3.bullet.PhysicsSpace.addCollisionObject(PhysicsSpace.java:457)
        at com.jme3.bullet.control.RigidBodyControl.setPhysicsSpace(RigidBodyControl.java:394)
        at com.jme3.bullet.PhysicsSpace.add(PhysicsSpace.java:436)
        ...
Exception in thread "pool-8-thread-1" java.lang.ClassCastException: class com.bulletphysics.collision.shapes.CompoundShape cannot be cast to class com.bulletphysics.collision.shapes.ConvexShape (com.bulletphysics.collision.shapes.CompoundShape and com.bulletphysics.collision.shapes.ConvexShape are in unnamed module of loader 'app')
        at com.bulletphysics.collision.dispatch.ConvexConvexAlgorithm.processCollision(ConvexConvexAlgorithm.java:109)
        at com.bulletphysics.collision.dispatch.ConvexTriangleCallback.processTriangle(ConvexTriangleCallback.java:163)
        at com.bulletphysics.collision.shapes.BvhTriangleMeshShape$MyNodeOverlapCallback.processNode(BvhTriangleMeshShape.java:268)
        at com.bulletphysics.collision.shapes.OptimizedBvh.walkStacklessQuantizedTree(OptimizedBvh.java:955)
        at com.bulletphysics.collision.shapes.OptimizedBvh.reportAabbOverlappingNodex(OptimizedBvh.java:703)
        at com.bulletphysics.collision.shapes.BvhTriangleMeshShape.processAllTriangles(BvhTriangleMeshShape.java:163)
        at com.bulletphysics.collision.shapes.TriangleMeshShape.localGetSupportingVertex(TriangleMeshShape.java:71)
        at com.bulletphysics.collision.shapes.TriangleMeshShape.recalcLocalAabb(TriangleMeshShape.java:88)
        at com.bulletphysics.collision.shapes.BvhTriangleMeshShape.<init>(BvhTriangleMeshShape.java:84)
        at com.bulletphysics.collision.shapes.BvhTriangleMeshShape.<init>(BvhTriangleMeshShape.java:63)
        at com.jme3.bullet.collision.shapes.MeshCollisionShape.createShape(MeshCollisionShape.java:138)
        at com.jme3.bullet.collision.shapes.MeshCollisionShape.createCollisionMesh(MeshCollisionShape.java:89)
        at com.jme3.bullet.collision.shapes.MeshCollisionShape.<init>(MeshCollisionShape.java:77)
        at com.jme3.bullet.collision.shapes.MeshCollisionShape.<init>(MeshCollisionShape.java:67)
        at com.jme3.bullet.util.CollisionShapeFactory.createSingleMeshShape(CollisionShapeFactory.java:257)
        at com.jme3.bullet.util.CollisionShapeFactory.createCompoundShape(CollisionShapeFactory.java:123)
        at com.jme3.bullet.util.CollisionShapeFactory.createCompoundShape(CollisionShapeFactory.java:143)
        at com.jme3.bullet.util.CollisionShapeFactory.createMeshCompoundShape(CollisionShapeFactory.java:158)
        at com.jme3.bullet.util.CollisionShapeFactory.createMeshShape(CollisionShapeFactory.java:200)
        ...

Also, I’m using BulletAppState.ThreadingType.PARALLEL, and not BulletAppState.ThreadingType.SEQUENTIAL because then I receive even more errors.

The BetterCharacterControl has nothing any special:

playerNode.setLocalTranslation(new Vector3f(4, 5, 2));
player = new BetterCharacterControl(0.5f, 6f, 2f);
player.setJumpForce(new Vector3f(0f, 35f, 0f));
playerNode.addControl(player);
bulletAppState.getPhysicsSpace().add(player);
bulletAppState.getPhysicsSpace().addAll(playerNode);
rootNode.attachChild(playerNode);

And for moving player: player.setWalkDirection(walkDirection.multLocal(1))

Finally, for adding physics for terrain(chunks) I use:

CollisionShape shape = CollisionShapeFactory.createMeshShape(chunkSpat)
RigidBodyControl chunkPhys = new RigidBodyControl(shape, 0);
PhysicsSpace state = Storage.bulletAppState.getPhysicsSpace();
chunkSpat.addControl(chunkPhys);
Transformation.spawnObject(chunkSpat, node, app);
state.add(chunkPhys);

I really don’t know what to do.

Are you using JMonkeyEngine?

None of the libraries you’ve tried is intended for use with JMonkeyEngine. The physics libraries commonly used with JMonkeyEngine are: Minie, jme3-bullet, and jme3-bullet. Both jMonkeyEngine and Minie provide tutorials and sample apps you can use as a starting point.

If you won’t use JMonkeyEngine for some reason, then you’ve got a difficult climb ahead. Libbulletjme provides a couple single-threaded sample apps in its README file. Have you successfully run those apps?

it is outdated lib. use jme3-bullet / minie instead

I’m using these libraries with jme3-jbullet at the same time. Using only jme3-bullet(jbullet) also works, but in Hello Physics tutorial I’ve seen that there are both jme bullet and cz.advel.jbullet : jbullet, so I have experimented changing that second library.

Now I’ve tried using Minie, and none of that issues appears.

2 Likes

I guess it’s not very clear how all the different physics libraries fit together.

  • Minie is intended for use with JMonkeyEngine. It can’t be combined with any other physics library. It depends on jme3-core, jme3-terrain, Heart, and now SimMath, but those dependencies are handled automatically if you build using Maven or Gradle.

  • jme3-jbullet is part of JMonkeyEngine. It depends on jme3-core, jme3-terrain, jbullet, stack-alloc, and vecmath. In the past, you had to explicitly provide jbullet and stack-alloc. However, since JME 3.4, those dependencies are handled automatically if you build using Maven or Gradle.

  • jme3-bullet was removed from JMonkeyEngine effective with JME 3.4. It required either jme3-bullet-native (for desktops) or jme3-bullet-native-android (for Androids) or both.

  • Libbulletjme isn’t intended for use with JMonkeyEngine. Minie makes use of Libbulletjme’s native libraries, but that’s handled behind the scenes, so app developers never see the dependency.

Because jme3-bullet, jme3-jbullet, and Minie were (at one time) intended as drop-in replacements for one another, they provide many of the same APIs. If you combine them, you may encounter all manner of strange failures.

Since you mentioned, why does Minie depend on jme3-terrain? seems that it should be specific dependency for making terrains.

2 Likes

That’s a good question.

All JMonkeyEngine physics libraries depend on “jme3-terrain”, because CollisionShapeFactory uses TerrainPatch and TerrainQuad to generate collision shapes for terrain.

1 Like