From 8c1ab4c5756ac43d52bc8773f5e72dde90f79e77 Mon Sep 17 00:00:00 2001 From: Joris Date: Tue, 5 Jul 2022 22:11:00 +0200 Subject: WIP --- src/util/physics.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/util/physics.ts (limited to 'src/util') diff --git a/src/util/physics.ts b/src/util/physics.ts new file mode 100644 index 0000000..cbc82f7 --- /dev/null +++ b/src/util/physics.ts @@ -0,0 +1,40 @@ +import * as Vec2 from 'model/vec2' + +interface AccParams { + dt: number, + speed: Vec2.Vec2, + dir: Vec2.Vec2 +} + +// max_acc = v_length(board_size) / 90000 +const maxAcc: number = 0.04 +const inertia: number = 200 + +export function acc({ dt, speed, dir }: AccParams): Vec2.Vec2 { + const v = Vec2.applyOnLength((x) => x * maxAcc, dir) + return Vec2.div(Vec2.sub(Vec2.scale(v, dt), speed), inertia) +} + +interface SpeedParams { + dt: number, + acc: Vec2.Vec2, + speed: Vec2.Vec2 +} + +export function speed({ dt, acc, speed }: SpeedParams): Vec2.Vec2 { + return Vec2.add(Vec2.scale(acc, dt), speed) +} + +interface PosParams { + dt: number, + acc: Vec2.Vec2, + speed: Vec2.Vec2, + pos: Vec2.Vec2 +} + +export function pos({ dt, acc, speed, pos }: PosParams): Vec2.Vec2 { + return Vec2.add( + Vec2.scale(acc, (dt ** 2) / 2), + Vec2.add(Vec2.scale(speed, dt), pos) + ) +} -- cgit v1.2.3