Skip to content

Commit

Permalink
BulletAppState: implement debug visualization of collision shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Jun 22, 2024
1 parent 3dff755 commit 79c4670
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions library/src/main/java/com/jme3/bullet/BulletAppState.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.jme3.app.Application;
import com.jme3.app.state.AppStateManager;
import com.jme3.app.state.BaseAppState;
import com.jme3.bullet.debug.BulletDebugAppState;
import com.jme3.bullet.debug.DebugConfiguration;
import com.jme3.bullet.util.NativeLibrary;
import com.jme3.renderer.RenderManager;
Expand Down Expand Up @@ -64,6 +65,10 @@ public class BulletAppState
* yet stopped)
*/
private volatile boolean isRunning = false;
/**
* AppState to manage the debug visualization, or null if none
*/
private BulletDebugAppState debugAppState;
/**
* configuration for debug visualization
*/
Expand Down Expand Up @@ -189,6 +194,16 @@ public void startPhysics() {
// *************************************************************************
// new protected methods

/**
* Create the configured debug-visualization app state.
*
* @return a new instance (not null)
*/
protected BulletDebugAppState createDebugAppState() {
BulletDebugAppState appState = new BulletDebugAppState(debugConfig);
return appState;
}

/**
* Create the configured {@code PhysicsSpace}.
*
Expand Down Expand Up @@ -249,6 +264,8 @@ protected void cleanup(Application app) {
*/
@Override
protected void initialize(Application app) {
debugConfig.initialize(app);
startPhysics();
}

/**
Expand Down Expand Up @@ -307,6 +324,18 @@ public void stateAttached(AppStateManager stateManager) {
public void update(float tpf) {
super.update(tpf);
this.tpf = tpf;

boolean enable = debugConfig.isEnabled();
if (enable && debugAppState == null) {
// Start debug visualization.
this.debugAppState = createDebugAppState();
getStateManager().attach(debugAppState);

} else if (!enable && debugAppState != null) {
// Stop debug visualization.
getStateManager().detach(debugAppState);
this.debugAppState = null;
}
}
// *************************************************************************
// PhysicsTickListener methods
Expand Down

0 comments on commit 79c4670

Please sign in to comment.