aboutsummaryrefslogtreecommitdiff
path: root/src/util/physics.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/physics.ts')
-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)
+ )
+}