From 2a44aa4aecccabec6d96ed5f7ea0d4614b967c89 Mon Sep 17 00:00:00 2001 From: NemesisMate Date: Tue, 5 Apr 2016 08:39:53 +0100 Subject: [PATCH 1/5] Reversed raytest results when needed In some cases, native-bullet returns the ray results on a reversed order so it leads to unexpected bugs. --- jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java | 6 ++++++ 1 file changed, 6 insertions(+) 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 f3575bcdf..88f0fdd6f 100644 --- a/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java +++ b/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java @@ -49,6 +49,7 @@ 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; @@ -788,6 +789,11 @@ public class PhysicsSpace { public List rayTest(Vector3f from, Vector3f to) { List results = new LinkedList(); rayTest(from, to, results); + + if(results.getFirst().getHitFraction() > results.getLast().getHitFraction()) { + Collections.reverse(results); + } + return (List) results; } From 513eedaea1df73dd7378ca8abbd4f51f4059b74e Mon Sep 17 00:00:00 2001 From: NemesisMate Date: Tue, 5 Apr 2016 08:58:30 +0100 Subject: [PATCH 2/5] Fixed a little bug I left on reversing the list :S: --- jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 88f0fdd6f..d8bf4ba00 100644 --- a/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java +++ b/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java @@ -787,14 +787,14 @@ public class PhysicsSpace { * PhysicsRayTestResults */ public List rayTest(Vector3f from, Vector3f to) { - List results = new LinkedList(); + LinkedList results = new LinkedList(); rayTest(from, to, results); if(results.getFirst().getHitFraction() > results.getLast().getHitFraction()) { Collections.reverse(results); } - return (List) results; + return results; } /** From 4cd424ad03f3a07b23b479d54114ef5ee141c786 Mon Sep 17 00:00:00 2001 From: NemesisMate Date: Thu, 19 May 2016 11:25:19 +0100 Subject: [PATCH 3/5] Ordering physics rayTest returned list instead of reversing it. Added raw results ray tests too. --- .../java/com/jme3/bullet/PhysicsSpace.java | 39 ++++++++++++++++--- 1 file changed, 33 insertions(+), 6 deletions(-) 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 d8bf4ba00..46eff032c 100644 --- a/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java +++ b/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java @@ -781,18 +781,25 @@ 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) { LinkedList results = new LinkedList(); rayTest(from, to, results); - if(results.getFirst().getHitFraction() > results.getLast().getHitFraction()) { - Collections.reverse(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) { + LinkedList results = new LinkedList(); + rayTestRaw(from, to, results); return results; } @@ -814,13 +821,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; } From 3b0e0766c553ad868e79dafcc395d1e47253cf27 Mon Sep 17 00:00:00 2001 From: NemesisMate Date: Thu, 19 May 2016 11:47:06 +0100 Subject: [PATCH 4/5] Oops! --- jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java | 1 + 1 file changed, 1 insertion(+) 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 46eff032c..cae59d8ef 100644 --- a/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java +++ b/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java @@ -54,6 +54,7 @@ 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; From 8a058c61f14a92e2e426f295afbf6e28aa4c5637 Mon Sep 17 00:00:00 2001 From: NemesisMate Date: Fri, 20 May 2016 10:00:08 +0100 Subject: [PATCH 5/5] Changed LinkedList for ArrayList in rayTests --- jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 cae59d8ef..c84308a4f 100644 --- a/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java +++ b/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java @@ -788,7 +788,7 @@ public class PhysicsSpace { * PhysicsRayTestResults ordered by it hitFraction (lower to higher) */ public List rayTest(Vector3f from, Vector3f to) { - LinkedList results = new LinkedList(); + List results = new ArrayList(); rayTest(from, to, results); return results; @@ -799,7 +799,7 @@ public class PhysicsSpace { * PhysicsRayTestResults without performing any sort operation */ public List rayTestRaw(Vector3f from, Vector3f to) { - LinkedList results = new LinkedList(); + List results = new ArrayList(); rayTestRaw(from, to, results); return results;