diff --git a/src/main/java/com/github/stephengold/joltjni/PhysicsSystem.java b/src/main/java/com/github/stephengold/joltjni/PhysicsSystem.java index f3259ec9..143563ce 100644 --- a/src/main/java/com/github/stephengold/joltjni/PhysicsSystem.java +++ b/src/main/java/com/github/stephengold/joltjni/PhysicsSystem.java @@ -46,6 +46,10 @@ public class PhysicsSystem extends NonCopyable { * cached reference to the system's no-lock {@code BodyInterface} */ final private BodyInterface bodyInterfaceNoLock; + /** + * protect the contact listener (if any) from garbage collection + */ + private ContactListener contactListener; /** * protect the BroadPhaseLayerInterface from garbage collection */ @@ -292,23 +296,12 @@ public Constraints getConstraints() { } /** - * Access the system's {@code ContactListener}. + * Access the (application-provided) contact listener. * - * @return a new JVM object with the pre-existing native object assigned, or - * {@code null} if none + * @return the pre-existing instance, or {@code null} if none */ public ContactListener getContactListener() { - long systemVa = va(); - long listenerVa = getContactListener(systemVa); - - ContactListener result; - if (listenerVa == 0L) { - result = null; - } else { - result = new ContactListener(listenerVa, false); - } - - return result; + return contactListener; } /** @@ -558,6 +551,7 @@ public void setCombineRestitution(CombineFunction function) { * @param listener the desired listener */ public void setContactListener(ContactListener listener) { + this.contactListener = listener; long systemVa = va(); long listenerVa = listener.va(); setContactListener(systemVa, listenerVa); @@ -646,8 +640,6 @@ native private static void getActiveBodies( native private static long getConstraints(long systemVa); - native private static long getContactListener(long systemVa); - native private static float getGravityX(long systemVa); native private static float getGravityY(long systemVa); diff --git a/src/main/native/glue/p/PhysicsSystem.cpp b/src/main/native/glue/p/PhysicsSystem.cpp index a387bc7c..67875a56 100644 --- a/src/main/native/glue/p/PhysicsSystem.cpp +++ b/src/main/native/glue/p/PhysicsSystem.cpp @@ -242,19 +242,6 @@ JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_PhysicsSystem_getCon return reinterpret_cast (pResult); } -/* - * Class: com_github_stephengold_joltjni_PhysicsSystem - * Method: getContactListener - * Signature: (J)J - */ -JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_PhysicsSystem_getContactListener - (JNIEnv *, jclass, jlong systemVa) { - const PhysicsSystem * const pSystem - = reinterpret_cast (systemVa); - ContactListener * const pResult = pSystem->GetContactListener(); - return reinterpret_cast (pResult); -} - /* * Class: com_github_stephengold_joltjni_PhysicsSystem * Method: getGravityX