diff --git a/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java b/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java index debd36959..821bf7a09 100644 --- a/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java +++ b/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java @@ -49,10 +49,12 @@ import com.jme3.scene.Spatial; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Comparator; import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; @@ -780,15 +782,27 @@ public class PhysicsSpace { public void removeCollisionGroupListener(int collisionGroup) { collisionGroupListeners.remove(collisionGroup); } - + /** * Performs a ray collision test and returns the results as a list of - * PhysicsRayTestResults + * PhysicsRayTestResults ordered by it hitFraction (lower to higher) */ public List rayTest(Vector3f from, Vector3f to) { - List results = new LinkedList(); + List results = new ArrayList(); rayTest(from, to, results); - return (List) results; + + return results; + } + + /** + * Performs a ray collision test and returns the results as a list of + * PhysicsRayTestResults without performing any sort operation + */ + public List rayTestRaw(Vector3f from, Vector3f to) { + List results = new ArrayList(); + rayTestRaw(from, to, results); + + return results; } /** @@ -808,13 +822,33 @@ public class PhysicsSpace { return rayTestFlags; } + private static Comparator hitFractionComparator = new Comparator() { + @Override + public int compare(PhysicsRayTestResult r1, PhysicsRayTestResult r2) { + float comp = r1.getHitFraction() - r2.getHitFraction(); + return comp > 0 ? 1 : -1; + } + }; + /** * Performs a ray collision test and returns the results as a list of - * PhysicsRayTestResults + * PhysicsRayTestResults ordered by it hitFraction (lower to higher) */ public List rayTest(Vector3f from, Vector3f to, List results) { results.clear(); rayTest_native(from, to, physicsSpaceId, results, rayTestFlags); + + Collections.sort(results, hitFractionComparator); + return results; + } + + /** + * Performs a ray collision test and returns the results as a list of + * PhysicsRayTestResults without performing any sort operation + */ + public List rayTestRaw(Vector3f from, Vector3f to, List results) { + results.clear(); + rayTest_native(from, to, physicsSpaceId, results, rayTestFlags); return results; }