Skip to content

Commit

Permalink
Pendulum without gravity
Browse files Browse the repository at this point in the history
  • Loading branch information
ftripier committed Aug 31, 2018
1 parent 58d946e commit 13d5b16
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
interface Map<T> {
[key: string]: T;
}

interface State {
startTime: number;
time: number;
dt: number;
width: number;
height: number;
system: Map<any>;
}

class Engine {
Expand All @@ -19,7 +24,8 @@ class Engine {
time: 0,
dt: 0,
width: 0,
height: 0
height: 0,
system: {}
};

const resizeCanvas = () => {
Expand Down Expand Up @@ -69,15 +75,16 @@ function getCenter(state: State) {
return [state.width >> 1, state.height >> 1];
}

function pendulumPosition(state: State) {
return 0;
}

function draw(state: State, ctx: CanvasRenderingContext2D) {
const center = getCenter(state);
const ballSize = 10;
const rodLength = 75;
const ballPosition = [center[0], center[1] + ballSize + rodLength];

const ballPosition = [
Math.cos(state.system.alpha) * (rodLength + ballSize) + center[0],
Math.sin(state.system.alpha) * (rodLength + ballSize) + center[1]
];
ctx.clearRect(0, 0, state.width, state.height);
ctx.beginPath();
ctx.fillStyle = "black";
ctx.arc(ballPosition[0], ballPosition[1], ballSize, 0, 2 * Math.PI);
Expand All @@ -88,7 +95,22 @@ function draw(state: State, ctx: CanvasRenderingContext2D) {
ctx.stroke();
}

function update(state: State) {
if (!state.system.alpha) {
state.system.alpha = 0;
}
const ballMass = 1;
const rodLength = 1;
const angluarVelocity = Math.PI * (1 / 128);

state.system.alpha += angluarVelocity;
if (state.system.alpha == Math.PI * 2) {
state.system.alpha = 0;
}
}

const loop = (state: State, ctx: CanvasRenderingContext2D) => {
update(state);
draw(state, ctx);
engine.nextFrame(loop);
};
Expand Down

0 comments on commit 13d5b16

Please sign in to comment.