From 7628b0f9e34447d81ab9f21f2467762b3675350f Mon Sep 17 00:00:00 2001 From: Fadorico <> Date: Wed, 9 Mar 2016 15:08:56 -0500 Subject: [PATCH 1/4] Fixed collision group listeners not being notified --- .../src/main/java/com/jme3/bullet/PhysicsSpace.java | 13 +++++++++++++ 1 file changed, 13 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 edea485ef..51fa0fcea 100644 --- a/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java +++ b/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java @@ -334,6 +334,19 @@ public class PhysicsSpace { private void addCollisionEvent_native(PhysicsCollisionObject node, PhysicsCollisionObject node1, long manifoldPointObjectId) { // System.out.println("addCollisionEvent:"+node.getObjectId()+" "+ node1.getObjectId()); collisionEvents.add(eventFactory.getEvent(PhysicsCollisionEvent.TYPE_PROCESSED, node, node1, manifoldPointObjectId)); + + // Notify group listeners + if((node.getCollideWithGroups() & node1.getCollisionGroup()) > 0 + || (node1.getCollideWithGroups() & node.getCollisionGroup()) > 0){ + PhysicsCollisionGroupListener listener = collisionGroupListeners.get(node.getCollisionGroup()); + PhysicsCollisionGroupListener listener1 = collisionGroupListeners.get(node1.getCollisionGroup()); + if(listener != null){ + listener.collide(node, node1); + } + if(listener1 != null && node.getCollisionGroup() != node1.getCollisionGroup()){ + listener1.collide(node, node1); + } + } } /** From ef626b095958f1ca4dc536d0eda3f7c4a06270c1 Mon Sep 17 00:00:00 2001 From: Fadorico Date: Wed, 23 Mar 2016 02:37:03 -0400 Subject: [PATCH 2/4] Fixed group collision check in native broadphase --- .../src/native/cpp/jmeClasses.cpp | 2 ++ .../src/native/cpp/jmeClasses.h | 1 + .../src/native/cpp/jmePhysicsSpace.cpp | 24 +++++++++++++++++-- .../java/com/jme3/bullet/PhysicsSpace.java | 23 +++++++++--------- 4 files changed, 37 insertions(+), 13 deletions(-) diff --git a/jme3-bullet-native/src/native/cpp/jmeClasses.cpp b/jme3-bullet-native/src/native/cpp/jmeClasses.cpp index 6b7caa0e9..ae7784c82 100644 --- a/jme3-bullet-native/src/native/cpp/jmeClasses.cpp +++ b/jme3-bullet-native/src/native/cpp/jmeClasses.cpp @@ -40,6 +40,7 @@ jclass jmeClasses::PhysicsSpace; jmethodID jmeClasses::PhysicsSpace_preTick; jmethodID jmeClasses::PhysicsSpace_postTick; jmethodID jmeClasses::PhysicsSpace_addCollisionEvent; +jmethodID jmeClasses::PhysicsSpace_notifyCollisionGroupListeners; jclass jmeClasses::PhysicsGhostObject; jmethodID jmeClasses::PhysicsGhostObject_addOverlappingObject; @@ -137,6 +138,7 @@ void jmeClasses::initJavaClasses(JNIEnv* env) { PhysicsSpace_preTick = env->GetMethodID(PhysicsSpace, "preTick_native", "(F)V"); PhysicsSpace_postTick = env->GetMethodID(PhysicsSpace, "postTick_native", "(F)V"); PhysicsSpace_addCollisionEvent = env->GetMethodID(PhysicsSpace, "addCollisionEvent_native","(Lcom/jme3/bullet/collision/PhysicsCollisionObject;Lcom/jme3/bullet/collision/PhysicsCollisionObject;J)V"); + PhysicsSpace_notifyCollisionGroupListeners = env->GetMethodID(PhysicsSpace, "notifyCollisionGroupListeners_native","(Lcom/jme3/bullet/collision/PhysicsCollisionObject;Lcom/jme3/bullet/collision/PhysicsCollisionObject;)Z"); if (env->ExceptionCheck()) { env->Throw(env->ExceptionOccurred()); return; diff --git a/jme3-bullet-native/src/native/cpp/jmeClasses.h b/jme3-bullet-native/src/native/cpp/jmeClasses.h index bb1b0e99a..bdead6b70 100644 --- a/jme3-bullet-native/src/native/cpp/jmeClasses.h +++ b/jme3-bullet-native/src/native/cpp/jmeClasses.h @@ -46,6 +46,7 @@ public: static jmethodID PhysicsSpace_addCollisionEvent; static jclass PhysicsGhostObject; static jmethodID PhysicsGhostObject_addOverlappingObject; + static jmethodID PhysicsSpace_notifyCollisionGroupListeners; static jclass Vector3f; static jmethodID Vector3f_set; diff --git a/jme3-bullet-native/src/native/cpp/jmePhysicsSpace.cpp b/jme3-bullet-native/src/native/cpp/jmePhysicsSpace.cpp index 34c77c407..8cb80bc87 100644 --- a/jme3-bullet-native/src/native/cpp/jmePhysicsSpace.cpp +++ b/jme3-bullet-native/src/native/cpp/jmePhysicsSpace.cpp @@ -187,8 +187,28 @@ void jmePhysicsSpace::createPhysicsSpace(jfloat minX, jfloat minY, jfloat minZ, jmeUserPointer *up0 = (jmeUserPointer*) co0 -> getUserPointer(); jmeUserPointer *up1 = (jmeUserPointer*) co1 -> getUserPointer(); if (up0 != NULL && up1 != NULL) { - collides = (up0->group & up1->groups) != 0; - collides = collides && (up1->group & up0->groups); + collides = (up0->group & up1->groups) != 0 || (up1->group & up0->groups) != 0; + + if(collides){ + jmePhysicsSpace *dynamicsWorld = (jmePhysicsSpace *)up0->space; + JNIEnv* env = dynamicsWorld->getEnv(); + jobject javaPhysicsSpace = env->NewLocalRef(dynamicsWorld->getJavaPhysicsSpace()); + jobject javaCollisionObject0 = env->NewLocalRef(up0->javaCollisionObject); + jobject javaCollisionObject1 = env->NewLocalRef(up1->javaCollisionObject); + + jboolean notifyResult = env->CallBooleanMethod(javaPhysicsSpace, jmeClasses::PhysicsSpace_notifyCollisionGroupListeners, javaCollisionObject0, javaCollisionObject1); + + env->DeleteLocalRef(javaPhysicsSpace); + env->DeleteLocalRef(javaCollisionObject0); + env->DeleteLocalRef(javaCollisionObject1); + + if (env->ExceptionCheck()) { + env->Throw(env->ExceptionOccurred()); + return collides; + } + + collides = (bool) notifyResult; + } //add some additional logic here that modified 'collides' return collides; 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 51fa0fcea..9122e1bc6 100644 --- a/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java +++ b/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java @@ -334,19 +334,20 @@ public class PhysicsSpace { private void addCollisionEvent_native(PhysicsCollisionObject node, PhysicsCollisionObject node1, long manifoldPointObjectId) { // System.out.println("addCollisionEvent:"+node.getObjectId()+" "+ node1.getObjectId()); collisionEvents.add(eventFactory.getEvent(PhysicsCollisionEvent.TYPE_PROCESSED, node, node1, manifoldPointObjectId)); - - // Notify group listeners - if((node.getCollideWithGroups() & node1.getCollisionGroup()) > 0 - || (node1.getCollideWithGroups() & node.getCollisionGroup()) > 0){ - PhysicsCollisionGroupListener listener = collisionGroupListeners.get(node.getCollisionGroup()); - PhysicsCollisionGroupListener listener1 = collisionGroupListeners.get(node1.getCollisionGroup()); - if(listener != null){ - listener.collide(node, node1); - } - if(listener1 != null && node.getCollisionGroup() != node1.getCollisionGroup()){ - listener1.collide(node, node1); + } + + private boolean notifyCollisionGroupListeners_native(PhysicsCollisionObject node, PhysicsCollisionObject node1){ + PhysicsCollisionGroupListener listener = collisionGroupListeners.get(node.getCollisionGroup()); + PhysicsCollisionGroupListener listener1 = collisionGroupListeners.get(node1.getCollisionGroup()); + if(listener != null){ + if(!listener.collide(node, node1)){ + return false; } } + if(listener1 != null && node.getCollisionGroup() != node1.getCollisionGroup()){ + return listener1.collide(node, node1); + } + return true; } /** From 83b26be45afdf7699fc48d2783b88b55bdcf4514 Mon Sep 17 00:00:00 2001 From: Fadorico Date: Thu, 31 Mar 2016 01:39:54 -0400 Subject: [PATCH 3/4] Combine result of both listeners to determine if the collision should occur --- .../src/main/java/com/jme3/bullet/PhysicsSpace.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 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 9122e1bc6..f3575bcdf 100644 --- a/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java +++ b/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java @@ -339,15 +339,16 @@ public class PhysicsSpace { private boolean notifyCollisionGroupListeners_native(PhysicsCollisionObject node, PhysicsCollisionObject node1){ PhysicsCollisionGroupListener listener = collisionGroupListeners.get(node.getCollisionGroup()); PhysicsCollisionGroupListener listener1 = collisionGroupListeners.get(node1.getCollisionGroup()); + boolean result = true; + if(listener != null){ - if(!listener.collide(node, node1)){ - return false; - } + result = listener.collide(node, node1); } if(listener1 != null && node.getCollisionGroup() != node1.getCollisionGroup()){ - return listener1.collide(node, node1); + result = listener1.collide(node, node1) && result; } - return true; + + return result; } /** From f61b1439cc1ea63e456ea1ac6fca9c722dea2745 Mon Sep 17 00:00:00 2001 From: Fadorico Date: Thu, 31 Mar 2016 22:42:53 -0400 Subject: [PATCH 4/4] Combine result of both listeners to determine if the collision should occur (jbullet) --- .../src/main/java/com/jme3/bullet/PhysicsSpace.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java b/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java index 016440bf0..483343f72 100644 --- a/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java +++ b/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java @@ -237,12 +237,12 @@ public class PhysicsSpace { || (collisionObject1.getCollideWithGroups() & collisionObject.getCollisionGroup()) > 0) { PhysicsCollisionGroupListener listener = collisionGroupListeners.get(collisionObject.getCollisionGroup()); PhysicsCollisionGroupListener listener1 = collisionGroupListeners.get(collisionObject1.getCollisionGroup()); - if (listener != null) { - return listener.collide(collisionObject, collisionObject1); - } else if (listener1 != null) { - return listener1.collide(collisionObject, collisionObject1); + if(listener != null){ + collides = listener.collide(collisionObject, collisionObject1); + } + if(listener1 != null && collisionObject.getCollisionGroup() != collisionObject1.getCollisionGroup()){ + collides = listener1.collide(collisionObject, collisionObject1) && collides; } - return true; } else { return false; }