Skip to content

Commit

Permalink
Cleaner initial state on gravity reset
Browse files Browse the repository at this point in the history
  • Loading branch information
ftripier committed Sep 3, 2018
1 parent ab37a27 commit 62a3c47
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ function evolveSystem(state: State, dt: number) {
correctEnergy(state);

// to prevent floating point overflow, we clamp theta between [0, PI*2]

if (Math.abs(state.system.theta) > Math.PI * 2) {
const sign = state.system.theta > 0 ? -1 : 1;
state.system.theta +=
Expand All @@ -148,13 +147,6 @@ function evolveSystem(state: State, dt: number) {
}

function update(state: State) {
if (state.system.theta == null) {
state.system.theta = INITIAL_THETA;
}
if (state.system.angularVelocity == null) {
state.system.angularVelocity = INITIAL_ANGULAR_VELOCITY;
}

let dt = state.dt;
while (dt > 0) {
const expectedFrameTime = 16 + 2 / 3;
Expand All @@ -167,25 +159,30 @@ function update(state: State) {
const canvas = <HTMLCanvasElement>document.getElementById("canvas");
const engine = new Engine(canvas);

const loop = (state: State, ctx: CanvasRenderingContext2D) => {
update(state);
draw(state, ctx);
engine.nextFrame(loop);
};
engine.nextFrame(loop);
window.requestAnimationFrame(engine.run.bind(engine));

const energyInput = new Input("Energy", String(INITIAL_ENERGY), (e?: Event) => {
engine.state.system.energy = energyInput.getValue();
});
engine.state.system.energy = energyInput.getValue();

const gravityInput = new Input(
"Gravity",
String(INITIAL_GRAVITY),
(e?: Event) => {
engine.state.system.gravity = gravityInput.getValue();
engine.state.system.angularVelocity = INITIAL_ANGULAR_VELOCITY;
engine.state.system.theta = INITIAL_THETA;
energyInput.setValue(getCurrentEnergy(engine.state));
}
);

engine.state.system.energy = energyInput.getValue();
engine.state.system.gravity = gravityInput.getValue();
engine.state.system.theta = INITIAL_THETA;
engine.state.system.angularVelocity = INITIAL_ANGULAR_VELOCITY;

const loop = (state: State, ctx: CanvasRenderingContext2D) => {
update(state);
draw(state, ctx);
engine.nextFrame(loop);
};
engine.nextFrame(loop);
window.requestAnimationFrame(engine.run.bind(engine));

0 comments on commit 62a3c47

Please sign in to comment.