aboutsummaryrefslogtreecommitdiff
path: root/src/Utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/Utils')
-rw-r--r--src/Utils/Geometry.elm27
-rw-r--r--src/Utils/Physics.elm46
2 files changed, 73 insertions, 0 deletions
diff --git a/src/Utils/Geometry.elm b/src/Utils/Geometry.elm
new file mode 100644
index 0000000..085026f
--- /dev/null
+++ b/src/Utils/Geometry.elm
@@ -0,0 +1,27 @@
+module Utils.Geometry
+ ( polarToCartesian
+ , distance
+ , inBoard
+ ) where
+
+import Model.Vec2 (..)
+import Model.Board (boardSize)
+
+polarToCartesian : Float -> Float -> Vec2
+polarToCartesian angle dist =
+ { x = dist * (cos angle)
+ , y = dist * (sin angle)
+ }
+
+distance : Vec2 -> Vec2 -> Float
+distance v1 v2 = sqrt((v2.x - v1.x)^2 + (v2.y - v1.y)^2)
+
+inBoard : Float -> Vec2 -> Vec2
+inBoard size pos =
+ let leftX = -boardSize.x / 2 + size
+ rightX = boardSize.x / 2 - size
+ bottomY = -boardSize.y / 2 + size
+ topY = boardSize.y / 2 - size
+ in { x = clamp leftX rightX pos.x
+ , y = clamp bottomY topY pos.y
+ }
diff --git a/src/Utils/Physics.elm b/src/Utils/Physics.elm
new file mode 100644
index 0000000..751af6c
--- /dev/null
+++ b/src/Utils/Physics.elm
@@ -0,0 +1,46 @@
+module Utils.Physics
+ ( getNewPosAndSpeed
+ , getMove
+ , getWaveMove
+ ) where
+
+import Model.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
+ }
+
+getWaveMove : Float -> Vec2 -> Float -> Float -> Vec2
+getWaveMove speed dir amplitude period =
+ let move = getMove speed dir
+ perpendMove =
+ getMove
+ (amplitude * (sin ((norm move) / period)))
+ (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