aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorJoris2022-07-05 22:11:00 +0200
committerJoris2022-07-05 22:11:00 +0200
commit8c1ab4c5756ac43d52bc8773f5e72dde90f79e77 (patch)
treede0db8c859e4204c90ddaa762f4d743fab1dedb2 /src/util
parent1d855b7a50b61650f7ddf40f604c788de1c9e914 (diff)
WIP
Diffstat (limited to 'src/util')
-rw-r--r--src/util/physics.ts40
1 files changed, 40 insertions, 0 deletions
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)
+ )
+}