Random crashes due to bulletphysics

So I’ve tried for hours to try to fix this. The problem is that my game would crash at random moments, and would crash from various exceptions (see below). There seems to be something in common though. It seems to have to do with the class com.bulletphysics.collision.shapes.BvhTriangleMeshShape.processAllTriangle. Also, most of the times, this line: return CollisionShapeFactory.createMeshShape((Node) Main.app.enqueue(getNode).get()); would be in the stack trace of my crashes. Here’s the relevant code:

private Callable createControl = new Callable() { //Callable task that will create a new physics mesh and return it
public Object call() throws Exception {
return CollisionShapeFactory.createMeshShape((Node) Main.app.enqueue(getNode).get());
}
};

private Callable getNode = new Callable() { //Called to safely get the chunk’s node from another thread
public Object call() throws Exception {
return node;
}
};

The reason why it’s especially difficult to debug this is because it is caused by every kind of exception. Such as CastClassException, NullPointerException, IndexOutOfBoundsException, ect.

Here’s some example stack traces that i got:

java.util.concurrent.ExecutionException: java.lang.IndexOutOfBoundsException
at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:252)
at java.util.concurrent.FutureTask.get(FutureTask.java:111)
at game.Chunk.updateCollisionShape(Chunk.java:154)
at game.Main.simpleUpdate(Main.java:181)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:241)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:228)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.IndexOutOfBoundsException
at com.bulletphysics.util.ObjectArrayList.remove(ObjectArrayList.java:78)
at com.bulletphysics.util.ObjectPool.get(ObjectPool.java:62)
at com.bulletphysics.collision.shapes.BvhTriangleMeshShape.processAllTriangles(BvhTriangleMeshShape.java:160)
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.(BvhTriangleMeshShape.java:84)
at com.bulletphysics.collision.shapes.BvhTriangleMeshShape.(BvhTriangleMeshShape.java:63)
at com.jme3.bullet.collision.shapes.MeshCollisionShape.createShape(MeshCollisionShape.java:122)
at com.jme3.bullet.collision.shapes.MeshCollisionShape.createCollisionMesh(MeshCollisionShape.java:77)
at com.jme3.bullet.collision.shapes.MeshCollisionShape.(MeshCollisionShape.java:65)
at com.jme3.bullet.util.CollisionShapeFactory.createSingleMeshShape(CollisionShapeFactory.java:214)
at com.jme3.bullet.util.CollisionShapeFactory.createCompoundShape(CollisionShapeFactory.java:112)
at com.jme3.bullet.util.CollisionShapeFactory.createCompoundShape(CollisionShapeFactory.java:134)
at com.jme3.bullet.util.CollisionShapeFactory.createMeshCompoundShape(CollisionShapeFactory.java:143)
at com.jme3.bullet.util.CollisionShapeFactory.createMeshShape(CollisionShapeFactory.java:173)
at game.Chunk$2.call(Chunk.java:37)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
… 1 more

and this

java.util.concurrent.ExecutionException: java.lang.NullPointerException
at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:252)
at java.util.concurrent.FutureTask.get(FutureTask.java:111)
at game.Chunk.updateCollisionShape(Chunk.java:154)
at game.Main.simpleUpdate(Main.java:181)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:241)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:228)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NullPointerException
at com.bulletphysics.collision.shapes.BvhTriangleMeshShape.processAllTriangles(BvhTriangleMeshShape.java:161)
at com.bulletphysics.collision.shapes.TriangleMeshShape.localGetSupportingVertex(TriangleMeshShape.java:71)
at com.bulletphysics.collision.shapes.TriangleMeshShape.recalcLocalAabb(TriangleMeshShape.java:91)
at com.bulletphysics.collision.shapes.BvhTriangleMeshShape.(BvhTriangleMeshShape.java:84)
at com.bulletphysics.collision.shapes.BvhTriangleMeshShape.(BvhTriangleMeshShape.java:63)
at com.jme3.bullet.collision.shapes.MeshCollisionShape.createShape(MeshCollisionShape.java:122)
at com.jme3.bullet.collision.shapes.MeshCollisionShape.createCollisionMesh(MeshCollisionShape.java:77)
at com.jme3.bullet.collision.shapes.MeshCollisionShape.(MeshCollisionShape.java:65)
at com.jme3.bullet.util.CollisionShapeFactory.createSingleMeshShape(CollisionShapeFactory.java:214)
at com.jme3.bullet.util.CollisionShapeFactory.createCompoundShape(CollisionShapeFactory.java:112)
at com.jme3.bullet.util.CollisionShapeFactory.createCompoundShape(CollisionShapeFactory.java:134)
at com.jme3.bullet.util.CollisionShapeFactory.createMeshCompoundShape(CollisionShapeFactory.java:143)
at com.jme3.bullet.util.CollisionShapeFactory.createMeshShape(CollisionShapeFactory.java:173)
at game.Chunk$2.call(Chunk.java:37)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
… 1 more

and sometimes just this

java.lang.NullPointerException
at com.bulletphysics.collision.shapes.BvhTriangleMeshShape.processAllTriangles(BvhTriangleMeshShape.java:161)
at com.bulletphysics.collision.dispatch.ConvexConcaveCollisionAlgorithm.processCollision(ConvexConcaveCollisionAlgorithm.java:86)
at com.bulletphysics.collision.dispatch.CompoundCollisionAlgorithm.processCollision(CompoundCollisionAlgorithm.java:120)
at com.bulletphysics.collision.dispatch.DefaultNearCallback.handleCollision(DefaultNearCallback.java:55)
at com.bulletphysics.collision.dispatch.CollisionDispatcher$CollisionPairCallback.processOverlap(CollisionDispatcher.java:236)
at com.bulletphysics.collision.broadphase.HashedOverlappingPairCache.processAllOverlappingPairs(HashedOverlappingPairCache.java:190)
at com.bulletphysics.collision.dispatch.CollisionDispatcher.dispatchAllCollisionPairs(CollisionDispatcher.java:247)
at com.bulletphysics.collision.dispatch.CollisionWorld.performDiscreteCollisionDetection(CollisionWorld.java:150)
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:324)
at com.jme3.bullet.PhysicsSpace.update(PhysicsSpace.java:311)
at com.jme3.bullet.BulletAppState.render(BulletAppState.java:185)
at com.jme3.app.state.AppStateManager.render(AppStateManager.java:268)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:250)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:228)
at java.lang.Thread.run(Thread.java:722)

Can somebody please explain what’s going on and help me fix this? Thanks.

Random crashes are usually due to either some misbehaving native code, or to threading issues.
Since you’re reporting clean stack traces and no JVM crashes, multithreading seems to be the more likely culprit.
Since Bullet is involved, my first bet would be that the software acesses Bullet in a manner that’s not threadsafe.

@toolforger said: Random crashes are usually due to either some misbehaving native code, or to threading issues. Since you're reporting clean stack traces and no JVM crashes, multithreading seems to be the more likely culprit. Since Bullet is involved, my first bet would be that the software acesses Bullet in a manner that's not threadsafe.

That’s what I was thinking too. However, the crash is coming from a static method and therefore could not be accessing the scene graph, which is what would be the cause of these Exceptions. Also, I used enqueue() to get the Node, so it can’t be that either. I have to put this in a separate thread because if I don’t, the game will pause every time I call it, which is very often. So is there any workaround to this?

You cannot create collision shapes concurrently in jbullet, it triggers a bug in the stackalloc API.

@normen said: You cannot create collision shapes concurrently in jbullet, it triggers a bug in the stackalloc API.

And there’s no known workaround for this?

@Kuru said: And there's no known workaround for this?

Except creating all collision shapes on one thread.

@Kuru said: And there's no known workaround for this?

Use the native bullet physics libraries instead of jbullet. I had to do the same thing. You may want to use my build of the native libraries, which are more up-to-date than the current jME3 builds (we are considering pushing the updated builds into the main jME3 svn):

Note that the native bullet binaries have known bugs (especially in collision) and are not release ready yet.

@normen said: Note that the native bullet binaries have known bugs (especially in collision) and are not release ready yet.

Is there an ETA on when it’s stable and ready to be released?

Also, I’m getting occasional crashes where none of my classes are in the stack trace:

SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.ClassCastException: com.bulletphysics.collision.shapes.TriangleShape cannot be cast to com.bulletphysics.collision.shapes.ConcaveShape
at com.bulletphysics.collision.dispatch.ConvexConcaveCollisionAlgorithm.processCollision(ConvexConcaveCollisionAlgorithm.java:73)
at com.bulletphysics.collision.dispatch.CompoundCollisionAlgorithm.processCollision(CompoundCollisionAlgorithm.java:120)
at com.bulletphysics.collision.dispatch.DefaultNearCallback.handleCollision(DefaultNearCallback.java:55)
at com.bulletphysics.collision.dispatch.CollisionDispatcher$CollisionPairCallback.processOverlap(CollisionDispatcher.java:236)
at com.bulletphysics.collision.broadphase.HashedOverlappingPairCache.processAllOverlappingPairs(HashedOverlappingPairCache.java:190)
at com.bulletphysics.collision.dispatch.CollisionDispatcher.dispatchAllCollisionPairs(CollisionDispatcher.java:247)
at com.bulletphysics.collision.dispatch.CollisionWorld.performDiscreteCollisionDetection(CollisionWorld.java:150)
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:324)
at com.jme3.bullet.PhysicsSpace.update(PhysicsSpace.java:311)
at com.jme3.bullet.BulletAppState.render(BulletAppState.java:185)
at com.jme3.app.state.AppStateManager.render(AppStateManager.java:268)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:250)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:228)
at java.lang.Thread.run(Thread.java:722)

and this

SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
at com.bulletphysics.collision.shapes.BvhTriangleMeshShape.processAllTriangles(BvhTriangleMeshShape.java:161)
at com.bulletphysics.collision.dispatch.ConvexConcaveCollisionAlgorithm.processCollision(ConvexConcaveCollisionAlgorithm.java:86)
at com.bulletphysics.collision.dispatch.CompoundCollisionAlgorithm.processCollision(CompoundCollisionAlgorithm.java:120)
at com.bulletphysics.collision.dispatch.DefaultNearCallback.handleCollision(DefaultNearCallback.java:55)
at com.bulletphysics.collision.dispatch.CollisionDispatcher$CollisionPairCallback.processOverlap(CollisionDispatcher.java:236)
at com.bulletphysics.collision.broadphase.HashedOverlappingPairCache.processAllOverlappingPairs(HashedOverlappingPairCache.java:190)
at com.bulletphysics.collision.dispatch.CollisionDispatcher.dispatchAllCollisionPairs(CollisionDispatcher.java:247)
at com.bulletphysics.collision.dispatch.CollisionWorld.performDiscreteCollisionDetection(CollisionWorld.java:150)
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:324)
at com.jme3.bullet.PhysicsSpace.update(PhysicsSpace.java:311)
at com.jme3.bullet.BulletAppState.render(BulletAppState.java:185)
at com.jme3.app.state.AppStateManager.render(AppStateManager.java:268)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:250)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:228)
at java.lang.Thread.run(Thread.java:722)

I have no idea what is causing this, nor how to deal with it because it does not relate to any of my code, and I can’t access the source code of where the Exception is being thrown. Also, it’s occuring at random times.

@Kuru said: Is there an ETA on when it's stable and ready to be released?
8 Ulatrons
@Kuru said: Is there an ETA on when it's stable and ready to be released?

Although it may not be fully ready for release, it works very good & I haven’t had any significant (or even minor) bugs with it in my game, http://3089game.wordpress.com/

I recommend trying it out instead of trying to get jbullet to work.

@phr00t said: Although it may not be fully ready for release, it works very good & I haven't had any significant (or even minor) bugs with it in my game, http://3089game.wordpress.com/

I recommend trying it out instead of trying to get jbullet to work.

idk what happened, but in my first run with the native physics, my FPS was on average triple than before, and all my physics is running more smoothly ._.

AND all the dozens of physics bugs I had are gone too. Even some bugs I thought were logic flaws in my own code, not from the libraries.

1 Like
@Kuru said: idk what happened, but in my first run with the native physics, my FPS was on average triple than before, and all my physics is running more smoothly ._.

AND all the dozens of physics bugs I had are gone too. Even some bugs I thought were logic flaws in my own code, not from the libraries.

Yeah, the native physics are pretty badass – glad to hear they are working better for you :slight_smile:

Are you using my build, or the built-in ones? The built-in ones will work, but be warned that the physics ray testing is swapped on Linux & Windows. If you use my builds, you won’t have that problem.

1 Like
@phr00t said: Yeah, the native physics are pretty badass -- glad to hear they are working better for you :)

Are you using my build, or the built-in ones? The built-in ones will work, but be warned that the physics ray testing is swapped on Linux & Windows. If you use my builds, you won’t have that problem.

I’m not sure, I think it’s yours. Oh yeah I lied, its more or a 5-10x FPS boost (my average of 5-15 FPS is now 50-100 FPS). What the hell is in this library?

@Kuru said: I'm not sure, I think it's yours. Oh yeah I lied, its more or a 5-10x FPS boost (my average of 5-15 FPS is now 50-100 FPS). What the hell is in this library?

hehe, ask the http://www.bulletphysics.com/ guys… it isn’t my code :stuck_out_tongue:

@phr00t said: hehe, ask the http://www.bulletphysics.com/ guys.. it isn't my code :P

Wow, wished I got this advice the second I started with physics. Anyways thanks for your help, you’ve probably improved by quality (and FPS) of my game tenfold!

1 Like

This antive one uses far superior algorithms in some places, especially when dealing with rays, this can lead to quite some performance gain.

@Kuru said: Wow, wished I got this advice the second I started with physics. Anyways thanks for your help, you've probably improved by quality (and FPS) of my game tenfold!
Obviously native bullet is faster, jbullet is like based on bullet 2.76 or so and jezek totally botched his "stack alloc" thing. Thats why we want to move to it in jME 3.1, as you see there will no API changes for the user either.

The “advice” to use the native bullet library isn’t given as the native wrapper still has issues as I mentioned and is not production ready yet. I am happy that @phr00t is so convinced of my wrapper and wants to push its usage but I know its issues and status as I wrote it :wink: The problem with the instability is that you cannot catch native crashes so your app will just halt and you won’t have any handle to report something to the user or anything. The collision shape creation issue in jbullet on the other hand has a workaround.

1 Like