aboutsummaryrefslogtreecommitdiff
path: root/src/Utils/Physics.elm
blob: 88a4434668cbedb3c14811ba066ed893908ba93b (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
module Utils.Physics exposing
  ( getNewPosAndSpeed
  , getMove
  , getWaveMove
  , getSawToothMove
  )

import Model.Vec2 exposing (..)

getNewPosAndSpeed : Float -> Vec2 -> (Float -> Float) -> (Vec2, Vec2) -> (Vec2, Vec2)
getNewPosAndSpeed dt dir computeSpeed (pos, speed) =
  let move = getMove (computeSpeed dt) dir
      acc = getAcc move speed
      newPos = getNewPos dt acc speed pos
      newSpeed = getNewSpeed dt acc speed
  in  ( newPos
      , newSpeed
      )

getMove : Float -> Vec2 -> Vec2
getMove dist dir =
  if (isNull dir)
    then {x = 0, y = 0}
    else
      let angle = atan2 dir.y dir.x
      in  { x = dist * cos angle
          , y = dist * sin angle
          }

getWaveMove : Float -> Vec2 -> Float -> Float -> Vec2
getWaveMove dist dir amplitude period =
  let move = getMove dist dir
      perpendMove =
        getMove
          (amplitude * (sin ((norm move) / period)))
          (clockwiseRotate90 move)
  in  move `add` perpendMove

getSawToothMove : Float -> Vec2 -> Float -> Float -> Vec2
getSawToothMove dist dir amplitude period =
  let move = getMove dist dir
      perpendMove =
        getMove
          ( let max = 1000 / period
                middle = max / 2
                modulo = toFloat <| round (norm move) % round max
            in  amplitude * (if modulo < middle then modulo else max - modulo) / middle
          )
          (clockwiseRotate90 move)
  in  move `add` perpendMove

getAcc : Vec2 -> Vec2 -> Vec2
getAcc move speed = (move `div` 300) `sub` (speed `div` 300)

getNewPos : Float -> Vec2 -> Vec2 -> Vec2 -> Vec2
getNewPos dt acc speed pos =
  ((dt^2 / 2) `mul` acc) `add` ((dt `mul` speed) `add` pos)

getNewSpeed : Float -> Vec2 -> Vec2 -> Vec2
getNewSpeed dt acc speed = add (mul dt acc) speed