Skip to content

Commit 97335a5

Browse files
committed
ContactListener: add the onContactConceived() method (issue #49)
1 parent 6f956d4 commit 97335a5

12 files changed

Lines changed: 182 additions & 3 deletions

File tree

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
The src/main/java/com/jme3 and src/main/native/glue software
2-
are Copyright (c) 2009-2025 jMonkeyEngine
2+
are Copyright (c) 2009-2026 jMonkeyEngine
33
All rights reserved.
44

55
The src/main/native/v-hacd software except vhacdMutex.h

src/main/java/com/jme3/bullet/PhysicsSpace.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,27 @@ public void removeCollisionObject(PhysicsCollisionObject pco) {
10031003
// *************************************************************************
10041004
// ContactListener methods
10051005

1006+
/**
1007+
* Invoked by native code immediately before a contact point is added to a
1008+
* manifold. Skipped if stepSimulation() was invoked without the
1009+
* {@code contactConceived} flag set.
1010+
* <p>
1011+
* Override this method to customize how contacts are handled.
1012+
*
1013+
* @param pointId the native ID of the {@code btManifoldPoint} (not zero)
1014+
* @param manifoldId the native ID of the {@code btPersistentManifold} (not
1015+
* zero)
1016+
* @param pcoA the "A" collision object (not null)
1017+
* @param pcoB the "B" collision object (not null)
1018+
* @return true to accept the contact, or false to reject it
1019+
*/
1020+
@Override
1021+
public boolean onContactConceived(long pointId, long manifoldId,
1022+
PhysicsCollisionObject pcoA, PhysicsCollisionObject pcoB) {
1023+
assert NativeLibrary.jniEnvId() == jniEnvId() : "wrong thread";
1024+
return true;
1025+
}
1026+
10061027
/**
10071028
* Invoked by native code immediately after a contact manifold is destroyed.
10081029
* Skipped if stepSimulation() was invoked without the {@code contactEnded}

src/main/java/com/jme3/bullet/StepFlag.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ final public class StepFlag {
5757
* enable {@code onContactStarted()} callbacks
5858
*/
5959
final public static int contactStarted = 0x4;
60+
/**
61+
* enable {@code onContactConceived()} callbacks
62+
*/
63+
final public static int contactConceived = 0x8;
6064
/**
6165
* message logger for this class
6266
*/
@@ -81,6 +85,9 @@ private StepFlag() {
8185
*/
8286
public static String describe(int flags) {
8387
Collection<String> flagList = new ArrayList<>(3);
88+
if ((flags & contactConceived) != 0x0) {
89+
flagList.add("contactConceived");
90+
}
8491
if ((flags & contactStarted) != 0x0) {
8592
flagList.add("contactStarted");
8693
}

src/main/java/com/jme3/bullet/collision/ContactListener.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022 jMonkeyEngine
2+
* Copyright (c) 2022-2026 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -41,6 +41,19 @@
4141
* @author Stephen Gold sgold@sonic.net
4242
*/
4343
public interface ContactListener {
44+
/**
45+
* Invoked immediately before a contact point is added to a manifold
46+
*
47+
* @param pointId the native ID of the {@code btManifoldPoint} (not zero)
48+
* @param manifoldId the native ID of the {@code btPersistentManifold} (not
49+
* zero)
50+
* @param pcoA the "A" collision object (not null)
51+
* @param pcoB the "B" collision object (not null)
52+
* @return true to accept the contact, or false to reject it
53+
*/
54+
boolean onContactConceived(long pointId, long manifoldId,
55+
PhysicsCollisionObject pcoA, PhysicsCollisionObject pcoB);
56+
4457
/**
4558
* Invoked immediately after a contact manifold is removed.
4659
*

src/main/native/bullet3/BulletCollision/CollisionDispatch/btManifoldResult.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ subject to the following restrictions:
2121

2222
///This is to allow MaterialCombiner/Custom Friction/Restitution values
2323
ContactAddedCallback gContactAddedCallback = 0;
24+
ContactConceivedCallback gContactConceivedCallback = 0;// stephengold added 2026-03-22
2425

2526
CalculateCombinedCallback gCalculateCombinedRestitutionCallback = &btManifoldResult::calculateCombinedRestitution;
2627
CalculateCombinedCallback gCalculateCombinedFrictionCallback = &btManifoldResult::calculateCombinedFriction;
@@ -192,6 +193,12 @@ void btManifoldResult::addContactPoint(const btVector3& normalOnBInWorld, const
192193
newPt.m_index1 = m_index1;
193194
}
194195
//printf("depth=%f\n",depth);
196+
if (gContactConceivedCallback) {// stephengold added 2026-03-22
197+
const btCollisionObject* pco0 = getBody0Internal();// stephengold added 2026-03-22
198+
const btCollisionObject* pco1 = getBody1Internal();// stephengold added 2026-03-22
199+
bool accept = (*gContactConceivedCallback)(newPt, m_manifoldPtr, pcoA, pcoB);// stephengold added 2026-03-22
200+
if (!accept) return;// stephengold added 2026-03-22
201+
}// stephengold added 2026-03-22
195202
///@todo, check this for any side effects
196203
if (insertIndex >= 0)
197204
{

src/main/native/bullet3/BulletCollision/CollisionDispatch/btManifoldResult.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class btManifoldPoint;
3030

3131
typedef bool (*ContactAddedCallback)(btManifoldPoint& cp, const btCollisionObjectWrapper* colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper* colObj1Wrap, int partId1, int index1);
3232
extern ContactAddedCallback gContactAddedCallback;
33+
typedef bool (*ContactConceivedCallback)(btManifoldPoint&, btPersistentManifold*, const btCollisionObject* pBodyA, const btCollisionObject* pBodyB);// stephengold added 2026-03-22
34+
extern ContactConceivedCallback gContactConceivedCallback;// stephengold added 2026-03-22
3335

3436
//#define DEBUG_PART_INDEX 1
3537

src/main/native/bullet3/BulletDynamics/Dynamics/btDiscreteDynamicsWorld.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -905,13 +905,14 @@ void btDiscreteDynamicsWorld::createPredictiveContactsInternal(btRigidBody** bod
905905
bool isSwapped = false; // stephengold added 2026-03-21
906906
btManifoldPoint newPoint(btVector3(0, 0, 0), localPointB, sweepResults.m_hitNormalWorld, distance, isPredictive, isSwapped); // stephengold modified 2026-03-21
907907

908-
//bool isPredictive = true; // stephengold commented out 2026-03-21
908+
if (gContactConceivedCallback == NULL || (*gContactConceivedCallback)(newPoint, manifold, body, sweepResults.m_hitCollisionObject)) {// stephengold modified 2026-03-22
909909
int index = manifold->addManifoldPoint(newPoint, isPredictive);
910910
btManifoldPoint& pt = manifold->getContactPoint(index);
911911
pt.m_combinedRestitution = 0;
912912
pt.m_combinedFriction = gCalculateCombinedFrictionCallback(body, sweepResults.m_hitCollisionObject);
913913
pt.m_positionWorldOnA = body->getWorldTransform().getOrigin();
914914
pt.m_positionWorldOnB = worldPointB;
915+
}// stephengold added 2026-03-22
915916
}
916917
}
917918
}

src/main/native/glue/com_jme3_bullet_StepFlag.h

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/native/glue/jmeClasses.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ jmethodID jmeClasses::CustomConvexShape_locateSupport;
5151

5252
jmethodID jmeClasses::PhysicsSpace_preTick;
5353
jmethodID jmeClasses::PhysicsSpace_postTick;
54+
jmethodID jmeClasses::PhysicsSpace_onContactConceived;
5455
jmethodID jmeClasses::PhysicsSpace_onContactEnded;
5556
jmethodID jmeClasses::PhysicsSpace_onContactProcessed;
5657
jmethodID jmeClasses::PhysicsSpace_onContactStarted;
@@ -248,6 +249,9 @@ void jmeClasses::initJavaClasses(JNIEnv *pEnv) {
248249
EXCEPTION_CHK(pEnv,);
249250
GLOBAL_METHOD(PhysicsSpace_preTick, physicsSpace, "preTick", "(F)V");
250251
GLOBAL_METHOD(PhysicsSpace_postTick, physicsSpace, "postTick", "(F)V");
252+
GLOBAL_METHOD(PhysicsSpace_onContactConceived,
253+
physicsSpace, "onContactConceived",
254+
"(JJLcom/jme3/bullet/collision/PhysicsCollisionObject;Lcom/jme3/bullet/collision/PhysicsCollisionObject;)Z");
251255
GLOBAL_METHOD(PhysicsSpace_onContactEnded,
252256
physicsSpace, "onContactEnded", "(J)V");
253257
GLOBAL_METHOD(PhysicsSpace_onContactProcessed,

src/main/native/glue/jmeClasses.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class jmeClasses {
7878

7979
static jmethodID PhysicsSpace_preTick;
8080
static jmethodID PhysicsSpace_postTick;
81+
static jmethodID PhysicsSpace_onContactConceived;
8182
static jmethodID PhysicsSpace_onContactEnded;
8283
static jmethodID PhysicsSpace_onContactProcessed;
8384
static jmethodID PhysicsSpace_onContactStarted;

0 commit comments

Comments
 (0)