aboutsummaryrefslogtreecommitdiff
path: root/src/Physics.elm
diff options
context:
space:
mode:
authorJoris Guyonvarch2014-09-02 21:35:58 +0200
committerJoris Guyonvarch2014-09-02 21:35:58 +0200
commitc4ae3b0ee4bd338995cfecf34e0aeb49f05fa70e (patch)
tree71530e9e2cfcbe888cb086593ea1e4d0bcc269a6 /src/Physics.elm
Initial commit
Diffstat (limited to 'src/Physics.elm')
-rw-r--r--src/Physics.elm33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/Physics.elm b/src/Physics.elm
new file mode 100644
index 0000000..b59b3e1
--- /dev/null
+++ b/src/Physics.elm
@@ -0,0 +1,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