blob: b59b3e199f175e61dad928a094b06d72edfd359e (
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
|
module Physics where
import Vec2 (..)
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 speed dir =
if (isNull dir)
then {x = 0, y = 0}
else
let angle = atan2 dir.y dir.x
in { x = speed * cos angle
, y = speed * sin angle
}
getAcc : Vec2 -> Vec2 -> Vec2
getAcc move speed = (move `div` 800) `sub` (speed `div` 1000)
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
|