Skip to content

Commit 79de38e

Browse files
committed
PhysicsSpace: alter how flags pass to update() and stepSimulation()
1 parent f228673 commit 79de38e

7 files changed

Lines changed: 191 additions & 37 deletions

File tree

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

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -727,8 +727,8 @@ public void update(float timeInterval) {
727727
}
728728

729729
/**
730-
* Update the space. This method should be invoked on the thread that
731-
* created the space.
730+
* Update the space, enabling the default callbacks. This method should be
731+
* invoked on the thread that created the space.
732732
*
733733
* @param timeInterval the time interval to simulate (in seconds, ≥0)
734734
* @param maxSteps the maximum number of steps of size {@code accuracy}
@@ -737,29 +737,50 @@ public void update(float timeInterval) {
737737
public void update(float timeInterval, int maxSteps) {
738738
assert Validate.nonNegative(timeInterval, "time interval");
739739
assert Validate.nonNegative(maxSteps, "max steps");
740-
741-
boolean doEnded = false;
742-
boolean doProcessed = false;
743-
boolean doStarted = false;
744-
update(timeInterval, maxSteps, doEnded, doProcessed, doStarted);
740+
update(timeInterval, maxSteps, 0x0);
745741
}
746742

747743
/**
748-
* Update the space. This method should be invoked from the thread that
749-
* created the space.
744+
* Update the space, enabling the specified callbacks. This method should be
745+
* invoked on the thread that created the space.
750746
*
751747
* @param timeInterval the time interval to simulate (in seconds, ≥0)
752748
* @param maxSteps the maximum number of steps of size {@code accuracy}
753749
* (≥1) or 0 for a single step of size {@code timeInterval}
754750
* @param doEnded true to enable {@code onContactEnded()} callbacks, false
755-
* to skip them
751+
* to skip them (default=false)
756752
* @param doProcessed true to enable {@code onContactProcessed()} callbacks,
757-
* false to skip them
753+
* false to skip them (default=false)
758754
* @param doStarted true to enable {@code onContactStarted()} callbacks,
759-
* false to skip them
755+
* false to skip them (default=false)
760756
*/
761757
public void update(float timeInterval, int maxSteps, boolean doEnded,
762758
boolean doProcessed, boolean doStarted) {
759+
int stepFlags = 0x0;
760+
if (doEnded) {
761+
stepFlags |= StepFlag.contactEnded;
762+
}
763+
if (doProcessed) {
764+
stepFlags |= StepFlag.contactProcessed;
765+
}
766+
if (doStarted) {
767+
stepFlags |= StepFlag.contactStarted;
768+
}
769+
770+
update(timeInterval, maxSteps, stepFlags);
771+
}
772+
773+
/**
774+
* Update the space with the specified callbacks. This method should be
775+
* invoked on the thread that created the space.
776+
*
777+
* @param timeInterval the time interval to simulate (in seconds, ≥0)
778+
* @param maxSteps the maximum number of steps of size {@code accuracy}
779+
* (≥1) or 0 for a single step of size {@code timeInterval}
780+
* @param stepFlags the desired flags, ORed together (default=0x0)
781+
* @see com.jme3.bullet.StepFlag
782+
*/
783+
public void update(float timeInterval, int maxSteps, int stepFlags) {
763784
assert Validate.nonNegative(timeInterval, "time interval");
764785
assert Validate.nonNegative(maxSteps, "max steps");
765786

@@ -769,8 +790,7 @@ public void update(float timeInterval, int maxSteps, boolean doEnded,
769790

770791
long spaceId = nativeId();
771792
assert accuracy > 0f : accuracy;
772-
stepSimulation(spaceId, timeInterval, maxSteps, accuracy, doEnded,
773-
doProcessed, doStarted);
793+
stepSimulation(spaceId, timeInterval, maxSteps, accuracy, stepFlags);
774794
}
775795

776796
/**
@@ -985,7 +1005,8 @@ public void removeCollisionObject(PhysicsCollisionObject pco) {
9851005

9861006
/**
9871007
* Invoked by native code immediately after a contact manifold is destroyed.
988-
* Skipped if stepSimulation() was invoked with doEnded=false.
1008+
* Skipped if stepSimulation() was invoked without the {@code contactEnded}
1009+
* flag set.
9891010
* <p>
9901011
* Override this method to customize how contacts are handled.
9911012
*
@@ -1000,7 +1021,8 @@ public void onContactEnded(long manifoldId) {
10001021
/**
10011022
* Invoked by native code immediately after a contact point is refreshed
10021023
* without being destroyed. Skipped for Sphere-Sphere contacts. Skipped if
1003-
* stepSimulation() was invoked with doProcessed=false.
1024+
* stepSimulation() was invoked without the {@code contactProcessed} flag
1025+
* set.
10041026
* <p>
10051027
* Override this method to customize how contacts are handled.
10061028
*
@@ -1016,7 +1038,8 @@ public void onContactProcessed(PhysicsCollisionObject pcoA,
10161038

10171039
/**
10181040
* Invoked by native code immediately after a contact manifold is created.
1019-
* Skipped if stepSimulation() was invoked with doStarted=false.
1041+
* Skipped if stepSimulation() was invoked without the
1042+
* {@code contactStarted} flag.
10201043
* <p>
10211044
* Override this method to customize how contacts are handled.
10221045
*
@@ -1272,6 +1295,5 @@ native private long createPhysicsSpace(
12721295
setSpeculativeContactRestitution(long spaceId, boolean apply);
12731296

12741297
native private static void stepSimulation(long spaceId, float timeInterval,
1275-
int maxSubSteps, float accuracy, boolean enableContactEnded,
1276-
boolean enableContactProcessed, boolean enableContactStarted);
1298+
int maxSubSteps, float accuracy, int stepFlags);
12771299
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright (c) 2026 jMonkeyEngine
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
package com.jme3.bullet;
33+
34+
import java.util.ArrayList;
35+
import java.util.Collection;
36+
import java.util.logging.Logger;
37+
38+
/**
39+
* Named flags used when stepping a {@code PhysicsSpace} simulation.
40+
*
41+
* @author Stephen Gold sgold@sonic.net
42+
* @see com.jme3.bullet.PhysicsSpace#update(float, int, int)
43+
*/
44+
final public class StepFlag {
45+
// *************************************************************************
46+
// constants and loggers
47+
48+
/**
49+
* enable {@code onContactEnded()} callbacks
50+
*/
51+
final public static int contactEnded = 0x1;
52+
/**
53+
* enable {@code onContactProcessed()} callbacks
54+
*/
55+
final public static int contactProcessed = 0x2;
56+
/**
57+
* enable {@code onContactStarted()} callbacks
58+
*/
59+
final public static int contactStarted = 0x4;
60+
/**
61+
* message logger for this class
62+
*/
63+
final public static Logger logger
64+
= Logger.getLogger(StepFlag.class.getName());
65+
// *************************************************************************
66+
// constructors
67+
68+
/**
69+
* A private constructor to inhibit instantiation of this class.
70+
*/
71+
private StepFlag() {
72+
}
73+
// *************************************************************************
74+
// new methods exposed
75+
76+
/**
77+
* Generate a textual description of the specified flags.
78+
*
79+
* @param flags the step flags to describe, ORed together
80+
* @return description (not null, may be empty)
81+
*/
82+
public static String describe(int flags) {
83+
Collection<String> flagList = new ArrayList<>(3);
84+
if ((flags & contactStarted) != 0x0) {
85+
flagList.add("contactStarted");
86+
}
87+
if ((flags & contactProcessed) != 0x0) {
88+
flagList.add("contactProcessed");
89+
}
90+
if ((flags & contactEnded) != 0x0) {
91+
flagList.add("contactEnded");
92+
}
93+
94+
StringBuilder result = new StringBuilder(40);
95+
boolean addSeparators = false;
96+
for (String flagName : flagList) {
97+
if (addSeparators) {
98+
result.append(',');
99+
} else {
100+
addSeparators = true;
101+
}
102+
result.append(flagName);
103+
}
104+
105+
return result.toString();
106+
}
107+
// *************************************************************************
108+
// native private methods
109+
110+
native private static void generateJniHeaderFile(); // never invoked
111+
}

src/main/native/glue/com_jme3_bullet_PhysicsSpace.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -522,17 +522,14 @@ JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_setSpeculativeContactRe
522522
/*
523523
* Class: com_jme3_bullet_PhysicsSpace
524524
* Method: stepSimulation
525-
* Signature: (JFIFZZZ)V
525+
* Signature: (JFIFI)V
526526
*/
527527
JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_stepSimulation
528528
(JNIEnv *pEnv, jclass, jlong spaceId, jfloat tpf, jint maxSteps,
529-
jfloat accuracy, jboolean enableContactEndedCallback,
530-
jboolean enableContactProcessedCallback,
531-
jboolean enableContactStartedCallback) {
529+
jfloat accuracy, jint stepFlags) {
532530
jmePhysicsSpace * const
533531
pSpace = reinterpret_cast<jmePhysicsSpace *> (spaceId);
534532
NULL_CHK(pEnv, pSpace, "The physics space does not exist.",)
535533

536-
pSpace->stepSimulation(tpf, maxSteps, accuracy, enableContactEndedCallback,
537-
enableContactProcessedCallback, enableContactStartedCallback);
534+
pSpace->stepSimulation(tpf, maxSteps, accuracy, stepFlags);
538535
}

src/main/native/glue/com_jme3_bullet_PhysicsSpace.h

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

src/main/native/glue/com_jme3_bullet_StepFlag.h

Lines changed: 27 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/jmePhysicsSpace.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#if BT_THREADSAFE
3737
#include "BulletDynamics/Dynamics/btDiscreteDynamicsWorldMt.h"
3838
#endif
39+
40+
#include "com_jme3_bullet_StepFlag.h"
3941
#include "jmePhysicsSpace.h"
4042
#include "jmeClasses.h"
4143
#include "jmeUserInfo.h"
@@ -437,23 +439,20 @@ void jmePhysicsSpace::preTickCallback(btDynamicsWorld *pWorld,
437439
}
438440

439441
void jmePhysicsSpace::stepSimulation(jfloat timeInterval, jint maxSteps,
440-
jfloat accuracy, jboolean enableContactEndedCallback,
441-
jboolean enableContactProcessedCallback,
442-
jboolean enableContactStartedCallback) {
443-
444-
if ((bool) enableContactEndedCallback) {
442+
jfloat accuracy, jint stepFlags) {
443+
if (stepFlags & com_jme3_bullet_StepFlag_contactEnded) {
445444
gContactEndedCallback = &contactEndedCallback;
446445
} else {
447446
gContactEndedCallback = NULL;
448447
}
449448

450-
if ((bool) enableContactProcessedCallback) {
449+
if (stepFlags & com_jme3_bullet_StepFlag_contactProcessed) {
451450
gContactProcessedCallback = &contactProcessedCallback;
452451
} else {
453452
gContactProcessedCallback = NULL;
454453
}
455454

456-
if ((bool) enableContactStartedCallback) {
455+
if (stepFlags & com_jme3_bullet_StepFlag_contactStarted) {
457456
gContactStartedCallback = &contactStartedCallback;
458457
} else {
459458
gContactStartedCallback = NULL;

src/main/native/glue/jmePhysicsSpace.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ class jmePhysicsSpace : public jmeCollisionSpace {
101101

102102
void
103103
stepSimulation(jfloat timeInterval, jint maxSteps, jfloat accuracy,
104-
jboolean enableContactEndedCallback,
105-
jboolean enableContactProcessedCallback,
106-
jboolean enableContactStartedCallback);
104+
jint stepFlags);
107105
};
108106

109107
#endif // JME_PHYSICS_SPACE_H

0 commit comments

Comments
 (0)