Ray collision bug

Hello everyone.



I believe I have found a bug in the collision system of jME3. I have reported it in the Google issue tracker some time ago but didn’t get a response, so I’m posting it here since I really need a workaround.

The bug in the issue tracker is here: http://code.google.com/p/jmonkeyengine/issues/detail?id=176



Please let me know in case you have a workaround for this one… Until this is resolved I can’t implement picking for more than one object. This bug still occurs in the 06/Aug build of jME.



Thank you,

  Aaron

I just had a look at your code attached to the issue, and I get the same result as you. I think I've found a workaround but I don't know whether it'll create other issues. Basically, as you said, the problem is that the CollisionResults is sorted before the indexed result is picked. The only change I made is to CollisionResults where I get the indexed result before sorting.



if (!sorted){
    Collections.sort(results);
    sorted = true;
}
return results.get(index);


to


CollisionResult result = results.get(index);
if (!sorted){
    Collections.sort(results);
    sorted = true;
}
return result;



Here's the patch if you want it:

Index: src/core/com/jme3/collision/CollisionResults.java
===================================================================
--- src/core/com/jme3/collision/CollisionResults.java   (revision 5672)
+++ src/core/com/jme3/collision/CollisionResults.java   (working copy)
@@ -56,12 +56,12 @@
     }
 
     public CollisionResult getCollision(int index){
-        if (!sorted){
+       CollisionResult result = results.get(index);
+       if (!sorted){
             Collections.sort(results);
             sorted = true;
         }
-
-        return results.get(index);
+        return result;
     }
 
 }