Android/Physics - Comparison Violates General Contract

Howdy friends,

I’ve been upgrading some of my old applications to the new Android SDK / JME 3.2.4. This error does not occur on PC.

I’ve gotten the following error on open for one of my games:

JNI DETECTED ERROR IN APPLICATION: JNI Throw called with pending exception java.lang.IllegalArgumentException: Comparison method violates its general contract!
at void java.util.TimSort.mergeHi(int, int, int, int) (
at void java.util.TimSort.mergeAt(int) (
at void java.util.TimSort.mergeCollapse() (
at void java.util.TimSort.sort(java.lang.Object[], int, int, java.util.Comparator, java.lang.Object[], int, int) (
at void java.util.Arrays.sort(java.lang.Object[], int, int, java.util.Comparator) (
at void java.util.Collections.sort(java.util.List, java.util.Comparator) (
at java.util.List com.jme3.bullet.PhysicsSpace.rayTest(com.jme3.math.Vector3f, com.jme3.math.Vector3f, java.util.List) (
at java.util.List com.jme3.bullet.PhysicsSpace.rayTest(com.jme3.math.Vector3f, com.jme3.math.Vector3f) (
at void com.jme3.bullet.control.BetterCharacterControl.checkOnGround() (
at void com.jme3.bullet.control.BetterCharacterControl.prePhysicsTick(com.jme3.bullet.PhysicsSpace, float) (
at void com.jme3.bullet.PhysicsSpace.preTick_native(float) (
at void com.jme3.bullet.PhysicsSpace.stepSimulation(long, float, int, float) (
at void com.jme3.bullet.PhysicsSpace.update(float, int) (
at void com.jme3.bullet.PhysicsSpace.update(float) (
at void com.jme3.bullet.BulletAppState.render(com.jme3.renderer.RenderManager) (
at void (
at void (
at void (
at void (
at void android.opengl.GLSurfaceView$GLThread.guardedRun() (
at void android.opengl.GLSurfaceView$ (
in call to Throw
from void com.jme3.bullet.PhysicsSpace.stepSimulation(long, float, int, float)
at com.jme3.bullet.PhysicsSpace.stepSimulation(Native method)
at com.jme3.bullet.PhysicsSpace.update(
at com.jme3.bullet.PhysicsSpace.update(
at com.jme3.bullet.BulletAppState.render(
at android.opengl.GLSurfaceView$GLThread.guardedRun(
at android.opengl.GLSurfaceView$
art/runtime/] Runtime aborting…
art/runtime/] Aborting thread:

If this stack trace makes sense to anyone it help would be much appreciated.

Thanks for reading,


1 Like

It could be random so it’s possible that it happens on desktop and you’ve just never seen it.

It’s usually an indication that the criteria for the sort has changed during the sort… normally I’d suspect changing Geometries and stuff during the sort. In this case, it’s strange, though… since it’s part of sorting the Ray Test results.

Collections.sort(results, hitFractionComparator);

…so then the other case would be that the comparator returns inconsistent results. So compare(a, b) may return a < b but compare(b, a) returns b <= a.

Interestingly, an aside: it looks like we somehow let phroot checkin C#/VisualBasic style method names: Native bullet raytest m_flags now accessible to Java, updated bullet … · jMonkeyEngine/jmonkeyengine@614bffa · GitHub

1 Like

Looking at the comparator:

Most likely cause is that one or the other hit fraction is NaN. Then comp will be NaN and comp > 0 will be false no matter which way r1 and r2 are compared.

1 Like
return comp != comp ? -1 : comp > 0 ? 1 : -1;

just me guessing?

No. Because then if you compare(r1, r2) you will get -1 and if you compare(r2, r1) you will also get -1… which is telling the sort() method that a < b and b < a… which is what it’s complaining about.

Probably, OP should figure out why collisions are resulting in NaNs… but the proper place to fix it is probably in the collision results which shouldn’t allow NaN in the first place… and maybe treat it like +Infinity instead.

1 Like

Hit fraction should always be between 0 and 1.