aboutsummaryrefslogtreecommitdiff
path: root/src/util/physics.ts
blob: cbc82f70b34431bb587ab8a191cf181514060425 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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)
  )
}