Class BulletAppState

All Implemented Interfaces:
AppState, PhysicsTickListener

public class BulletAppState extends AbstractAppState implements PhysicsTickListener
An app state to manage a single Bullet physics space.

This class is shared between JBullet and Native Bullet.

  • Field Details

    • isRunning

      protected volatile boolean isRunning
      true if-and-only-if the physics simulation is running (started but not yet stopped)
    • app

      protected Application app
    • stateManager

      protected AppStateManager stateManager
      manager that manages this state, set during attach
    • executor

      protected ScheduledThreadPoolExecutor executor
      executor service for physics tasks, or null if parallel simulation is not running
    • pSpace

      protected PhysicsSpace pSpace
      physics space managed by this state, or null if no simulation running
    • threadingType

      protected BulletAppState.ThreadingType threadingType
      threading mode to use (not null)
    • broadphaseType

      protected PhysicsSpace.BroadphaseType broadphaseType
      broadphase collision-detection algorithm for the physics space to use (not null)
    • worldMin

      protected Vector3f worldMin
      minimum coordinate values for the physics space when using AXIS_SWEEP broadphase algorithms (not null)
    • worldMax

      protected Vector3f worldMax
      maximum coordinate values for the physics space when using AXIS_SWEEP broadphase algorithms (not null)
    • speed

      protected float speed
      simulation speed multiplier (default=1, paused=0)
    • active

      protected boolean active
      true if-and-only-if this state is enabled
    • debugEnabled

      protected boolean debugEnabled
      true if-and-only-if debug visualization is enabled
    • debugAppState

      protected BulletDebugAppState debugAppState
      app state to manage the debug visualization, or null if none
    • tpf

      protected float tpf
      time interval between frames (in seconds) from the most recent update
    • physicsFuture

      protected Future physicsFuture
      current physics task, or null if none
  • Constructor Details

    • BulletAppState

      public BulletAppState()
      Instantiate an app state to manage a new PhysicsSpace with DBVT collision detection.

      Use getStateManager().addState(bulletAppState) to start physics.

    • BulletAppState

      public BulletAppState(PhysicsSpace.BroadphaseType broadphaseType)
      Instantiate an app state to manage a new PhysicsSpace.

      Use getStateManager().addState(bulletAppState) to start physics.

      Parameters:
      broadphaseType - which broadphase collision-detection algorithm to use (not null)
    • BulletAppState

      public BulletAppState(Vector3f worldMin, Vector3f worldMax)
      Instantiate an app state to manage a new PhysicsSpace with AXIS_SWEEP_3 collision detection.

      Use getStateManager().addState(bulletAppState) to start physics.

      Parameters:
      worldMin - the desired minimum coordinate values (not null, unaffected, default=-10k,-10k,-10k)
      worldMax - the desired maximum coordinate values (not null, unaffected, default=10k,10k,10k)
    • BulletAppState

      public BulletAppState(Vector3f worldMin, Vector3f worldMax, PhysicsSpace.BroadphaseType broadphaseType)
      Instantiate an app state to manage a new PhysicsSpace.

      Use getStateManager().addState(bulletAppState) to enable physics.

      Parameters:
      worldMin - the desired minimum coordinate values (not null, unaffected, default=-10k,-10k,-10k)
      worldMax - the desired maximum coordinate values (not null, unaffected, default=10k,10k,10k)
      broadphaseType - which broadphase collision-detection algorithm to use (not null)
  • Method Details

    • getPhysicsSpace

      public PhysicsSpace getPhysicsSpace()
      Access the PhysicsSpace managed by this state. Normally there is none until the state is attached.
      Returns:
      the pre-existing instance, or null if no simulation running
    • startPhysics

      public void startPhysics()
      Allocate a physics space and start physics.

      Physics starts automatically after the state is attached. To start it sooner, invoke this method.

    • stopPhysics

      public void stopPhysics()
      Stop physics after this state is detached.
    • initialize

      public void initialize(AppStateManager stateManager, Application app)
      Initialize this state prior to its 1st update. Should be invoked only by a subclass or by the AppStateManager.
      Specified by:
      initialize in interface AppState
      Overrides:
      initialize in class AbstractAppState
      Parameters:
      stateManager - the manager for this state (not null)
      app - the application which owns this state (not null)
    • setDebugEnabled

      public void setDebugEnabled(boolean debugEnabled)
      Alter whether debug visualization is enabled.
      Parameters:
      debugEnabled - true → enable, false → disable
    • isDebugEnabled

      public boolean isDebugEnabled()
      Test whether debug visualization is enabled.
      Returns:
      true if enabled, otherwise false
    • stateAttached

      public void stateAttached(AppStateManager stateManager)
      Transition this state from detached to initializing. Should be invoked only by a subclass or by the AppStateManager.
      Specified by:
      stateAttached in interface AppState
      Overrides:
      stateAttached in class AbstractAppState
      Parameters:
      stateManager - (not null)
    • update

      public void update(float tpf)
      Update this state prior to rendering. Should be invoked only by a subclass or by the AppStateManager. Invoked once per frame, provided the state is attached and enabled.
      Specified by:
      update in interface AppState
      Overrides:
      update in class AbstractAppState
      Parameters:
      tpf - the time interval between frames (in seconds, ≥0)
    • render

      public void render(RenderManager rm)
      Render this state. Should be invoked only by a subclass or by the AppStateManager. Invoked once per frame, provided the state is attached and enabled.
      Specified by:
      render in interface AppState
      Overrides:
      render in class AbstractAppState
      Parameters:
      rm - the render manager (not null)
    • postRender

      public void postRender()
      Update this state after all rendering commands are flushed. Should be invoked only by a subclass or by the AppStateManager. Invoked once per frame, provided the state is attached and enabled.
      Specified by:
      postRender in interface AppState
      Overrides:
      postRender in class AbstractAppState
    • cleanup

      public void cleanup()
      Transition this state from terminating to detached. Should be invoked only by a subclass or by the AppStateManager. Invoked once for each time initialize(com.jme3.app.state.AppStateManager, com.jme3.app.Application) is invoked.
      Specified by:
      cleanup in interface AppState
      Overrides:
      cleanup in class AbstractAppState
    • getThreadingType

      public BulletAppState.ThreadingType getThreadingType()
      Read which type of threading this app state uses.
      Returns:
      the threadingType (not null)
    • setThreadingType

      public void setThreadingType(BulletAppState.ThreadingType threadingType)
      Alter which type of threading this app state uses. Not allowed after attaching the app state.
      Parameters:
      threadingType - the desired type (not null, default=SEQUENTIAL)
    • setBroadphaseType

      public void setBroadphaseType(PhysicsSpace.BroadphaseType broadphaseType)
      Alter the broadphase type the physics space will use. Not allowed after attaching the app state.
      Parameters:
      broadphaseType - an enum value (not null, default=DBVT)
    • setWorldMin

      public void setWorldMin(Vector3f worldMin)
      Alter the coordinate range. Not allowed after attaching the app state.
      Parameters:
      worldMin - the desired minimum coordinate values when using AXIS_SWEEP broadphase algorithms (not null, alias created, default=-10k,-10k,-10k)
    • setWorldMax

      public void setWorldMax(Vector3f worldMax)
      Alter the coordinate range. Not allowed after attaching the app state.
      Parameters:
      worldMax - the desired maximum coordinate values when using AXIS_SWEEP broadphase algorithms (not null, alias created, default=10k,10k,10k)
    • getSpeed

      public float getSpeed()
      Read the simulation speed.
      Returns:
      speed (≥0, default=1)
    • setSpeed

      public void setSpeed(float speed)
      Alter the simulation speed.
      Parameters:
      speed - the desired speed (≥0, default=1)
    • prePhysicsTick

      public void prePhysicsTick(PhysicsSpace space, float f)
      Callback from Bullet, invoked just before the physics is stepped. A good time to clear/apply forces.
      Specified by:
      prePhysicsTick in interface PhysicsTickListener
      Parameters:
      space - the space that is about to be stepped (not null)
      f - the time per physics step (in seconds, ≥0)
    • physicsTick

      public void physicsTick(PhysicsSpace space, float f)
      Callback from Bullet, invoked just after the physics is stepped. A good time to clear/apply forces.
      Specified by:
      physicsTick in interface PhysicsTickListener
      Parameters:
      space - the space that is about to be stepped (not null)
      f - the time per physics step (in seconds, ≥0)