Simulation Run Modes¶
Choose the execution path based on who advances the simulation:
KangEngine App:
app.start()orapp.update_and_draw()usesFixedStepClockto schedulefixed_update()callbacks from elapsed wall time.KELab:
env.step()advances the simulation explicitly.SimulationRunConfigselects rendering and wall-clock waiting behavior.
An App can also display an externally stepped simulation using
process_events() and draw_frame(). See
Fixed Timestep and Rendering
for the distinction between SimulationTimingConfig rates and
SimulationRunConfig execution policy.
KangEngine App¶
App-scheduled updates¶
App.start() repeatedly calls update_and_draw(). FixedStepClock accumulates
wall time and may invoke fixed_update() zero or more times per rendered frame.
Put App-owned physics updates in fixed_update(); SimulationRunConfig does
not select or replace this scheduler.
Do not use SimulationPacer inside an App fixed_update() callback. Let
FixedStepClock schedule the updates.
Manually driven frames¶
Choose the frame method based on who owns simulation updates:
Method |
Behavior |
|---|---|
|
Process events, call |
|
Poll window events and handle camera input and simulation hotkeys. No update or render callbacks. |
|
Call |
When your loop or an external simulator steps physics explicitly, sync its
visual state, then call process_events() and draw_frame(). This path does not
run the App fixed-update scheduler. For a responsive paused viewer, continue
calling both methods while simulation is paused. If simulation hotkeys are
enabled, consume_simulation_step() consumes a paused single-step request
exactly once; it does not step physics or require a fixed-update rate.
Manual loops call setup() themselves if their App subclass needs it. Render
callbacks may change state if user code explicitly does so. Do not call frame
execution methods recursively from these callbacks or from fixed_update().
Input timing is independent of drawing; draw-only calls do not reset the App update clock. Use one scheduling owner consistently to avoid wall-time catch-up when returning to App-driven updates.
Paced playback without KELab¶
Assume app is an initialized windowed ke.App, world is a
ke.sim.KangSimWorld with sim_dt=1.0 / 120.0, and visual is its populated
ke.visual.sim.SimWorldVisualizer. This loop advances two physics steps per
frame and targets 60 frames per second:
run_config: ke.SimulationRunConfig = ke.SimulationRunConfig(mode="paced")
pacer: ke.SimulationPacer = ke.SimulationPacer(run_config=run_config)
step_dt = 2 * world.sim_dt
while not app.should_close():
app.process_events()
if app.should_close():
break
world.step(substeps=2)
visual.sync()
app.draw_frame()
pacer.wait(step_dt=step_dt)
Call pacer.wait() after stepping and drawing so the steady-state wait accounts
for both costs. This loop owns simulation updates and does not call
update_and_draw() or fixed_update().
App recording policy¶
app.configure_run() currently selects how App video recording handles frames:
Mode |
Recording behavior |
|---|---|
|
Select or repeat frames to match elapsed wall time |
|
Record one video frame per rendered frame |
|
Raise |
It does not change renderer initialization, schedule simulation updates, or
wait between frames. Without an explicit policy, App uses PACED for a visible
window and OFFSCREEN_FAST for headless=True. The windowed example above
therefore needs no configure_run() call. Use
app.configure_run(config=run_config) to override the recording policy. See
Video Recording for FPS and capture
settings.
KELab¶
KELab owns simulation stepping through env.step() and applies the selected
run policy to its viewer. Its viewer calls SimulationPacer for paced playback;
do not add another pacer around env.step().
Defaults and environment stepping¶
DirectRLEnv chooses a mode from Gym’s render_mode when no explicit
run_config is supplied:
render_mode=None -> HEADLESS_FAST
render_mode="human" -> PACED
render_mode="rgb_array" -> OFFSCREEN_FAST
Each mode has one rendering configuration. HEADLESS_FAST requires
render_mode=None, OFFSCREEN_FAST requires render_mode="rgb_array", and
PACED requires render_mode="human". Human rendering is performed
automatically after each reset and environment step. RGB array rendering
occurs without wall-clock waiting when the caller invokes env.render().
PACED renders per environment step, not per physics substep:
environment step
├─ apply action
├─ physics step x decimation
├─ observations, rewards, termination, and reset
├─ render latest state
└─ wait until step_dt has elapsed
Timing settings and derived values¶
App-driven loops configure SimulationTimingConfig.physics_hz and
fixed_update_hz directly; physics_dt, fixed_dt, and their ratio
decimation are derived properties. See
Fixed Timestep and Rendering for configuration examples.
KELab starts from environment configuration fields instead:
Value |
Defined by |
Meaning |
|---|---|---|
|
|
Seconds per physics step |
|
|
Integer physics steps per environment step |
|
Derived as |
Physics rate in the environment’s timing config |
|
Derived as |
Seconds per environment step; exposed as |
|
Derived as |
Environment/control rate; exposed on |
KELab builds its SimulationTimingConfig from these fields, with
fixed_update_hz = 1 / step_dt. For example, sim.dt = 1 / 120 and
decimation = 2 give a 60 Hz environment step and paced playback target.
Migration from render_frame_once()¶
render_frame_once() has been removed. Replace it with update_and_draw() to
retain App scheduling, or with process_events() and draw_frame() for external
stepping. Event polling now precedes input processing in update_and_draw().
User callback names are unchanged.