Skip to content

Commit

Permalink
add fixed step rate option
Browse files Browse the repository at this point in the history
  • Loading branch information
AustinEast committed Jan 10, 2024
1 parent 4fa1f14 commit cd0905d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
59 changes: 37 additions & 22 deletions echo/Echo.hx
Original file line number Diff line number Diff line change
Expand Up @@ -76,31 +76,46 @@ class Echo {
/**
* Steps a `World` forward.
* @param world
* @param dt
* @param dt The Delta Time to step the `World` Forward
* @param rate The target rate of Step-Per-Second. If set to 0, the target rate is unlimited.
*/
public static function step(world:World, dt:Float) {
// Save World State to History
if (world.history != null) world.history.add([
for (b in world.members) {
id: b.id,
x: b.x,
y: b.y,
rotation: b.rotation,
velocity_x: b.velocity.x,
velocity_y: b.velocity.y,
acceleration_x: b.acceleration.x,
acceleration_y: b.acceleration.y,
rotational_velocity: b.rotational_velocity
public static function step(world:World, dt:Float, rate:Float = 0) {
function step_world(world:World, dt:Float) {
// Save World State to History
if (world.history != null) world.history.add([
for (b in world.members) {
id: b.id,
x: b.x,
y: b.y,
rotation: b.rotation,
velocity_x: b.velocity.x,
velocity_y: b.velocity.y,
acceleration_x: b.acceleration.x,
acceleration_y: b.acceleration.y,
rotational_velocity: b.rotational_velocity
}
]);

// Step the World incrementally based on the number of iterations
var fdt = dt / world.iterations;
for (i in 0...world.iterations) {
Physics.step(world, fdt);
Collisions.query(world);
Physics.separate(world);
Collisions.notify(world);
}
]);
}

// Step the World incrementally based on the number of iterations
var fdt = dt / world.iterations;
for (i in 0...world.iterations) {
Physics.step(world, fdt);
Collisions.query(world);
Physics.separate(world);
Collisions.notify(world);
if (rate > 0) {
world.accumulatedTime += dt;
var step = 1.0 / rate;
while (world.accumulatedTime >= step) {
world.accumulatedTime -= step;
step_world(world, step);
}
}
else {
step_world(world, dt);
}
}
/**
Expand Down
2 changes: 2 additions & 0 deletions echo/World.hx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class World implements Disposable {

public var history:Null<History<Array<BodyState>>>;

public var accumulatedTime:Float = 0;

var init:Bool;

public function new(options:WorldOptions) {
Expand Down
5 changes: 3 additions & 2 deletions sample/Main.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package;

import hxd.Window;
import hxd.Key;
import echo.Echo;
import echo.World;
Expand Down Expand Up @@ -86,8 +87,8 @@ class Main extends BaseApp {
var fdt = Key.isDown(Key.SHIFT) ? dt * 0.3 : dt;
// Update the current Sample State
fsm.step(fdt);
// Step the World Forward
if (playing) world.step(fdt);
// Step the World Forward, with a fixed step rate of 60 per second
if (playing) world.step(fdt, 60);

// Update GUI text
members_text.text = 'Bodies: ${world.count}';
Expand Down

0 comments on commit cd0905d

Please sign in to comment.