From 3a1cbfe23a3d06c3c30828c623a089868cff0670 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 2 Oct 2016 15:34:27 +0200 Subject: Add saw-tooth move and multiple moves per level --- src/Utils/Color.elm | 23 +++++++++++++++++++++++ src/Utils/Physics.elm | 24 +++++++++++++++++++----- 2 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 src/Utils/Color.elm (limited to 'src/Utils') diff --git a/src/Utils/Color.elm b/src/Utils/Color.elm new file mode 100644 index 0000000..f9e344f --- /dev/null +++ b/src/Utils/Color.elm @@ -0,0 +1,23 @@ +module Utils.Color exposing + ( merge + , spin + ) + +import Color as Color exposing (Color) + +merge : Float -> Color -> Color -> Color +merge ratio c1 c2 = + let rgb1 = Color.toRgb c1 + rgb2 = Color.toRgb c2 + mergePartFloat x y = ratio * x + (1 - ratio) * y + mergePartInt x y = truncate <| ratio * (toFloat x) + (1 - ratio) * (toFloat y) + in Color.rgba + (mergePartInt rgb1.red rgb2.red) + (mergePartInt rgb1.green rgb2.green) + (mergePartInt rgb1.blue rgb2.blue) + (mergePartFloat rgb1.alpha rgb2.alpha) + +spin : Float -> Color -> Color +spin d color = + let { hue, saturation, lightness, alpha } = Color.toHsl color + in Color.hsla (hue + degrees d) saturation lightness alpha diff --git a/src/Utils/Physics.elm b/src/Utils/Physics.elm index 5da3737..88a4434 100644 --- a/src/Utils/Physics.elm +++ b/src/Utils/Physics.elm @@ -2,6 +2,7 @@ module Utils.Physics exposing ( getNewPosAndSpeed , getMove , getWaveMove + , getSawToothMove ) import Model.Vec2 exposing (..) @@ -17,24 +18,37 @@ getNewPosAndSpeed dt dir computeSpeed (pos, speed) = ) getMove : Float -> Vec2 -> Vec2 -getMove speed dir = +getMove dist 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 + in { x = dist * cos angle + , y = dist * sin angle } getWaveMove : Float -> Vec2 -> Float -> Float -> Vec2 -getWaveMove speed dir amplitude period = - let move = getMove speed dir +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) -- cgit v1.2.3