From 6492ab27e8418f3d4a7b10ea41be1ece0e955429 Mon Sep 17 00:00:00 2001 From: mdgriffith Date: Mon, 25 May 2020 09:23:12 -0400 Subject: [PATCH] Guess the time for bezier interpolation to reduce number of iterations drastically. If the curve personality is linear, don't factor in existing velocity --- src/Internal/Interpolate.elm | 46 +++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/src/Internal/Interpolate.elm b/src/Internal/Interpolate.elm index 07016a6..dbaa6be 100644 --- a/src/Internal/Interpolate.elm +++ b/src/Internal/Interpolate.elm @@ -420,7 +420,15 @@ afterMove lookup target future = {-| -} -springInterpolation : Milliseconds -> Maybe Movement -> Movement -> Milliseconds -> Milliseconds -> Maybe (Timeline.LookAhead Movement) -> State -> State +springInterpolation : + Milliseconds + -> Maybe Movement + -> Movement + -> Milliseconds + -> Milliseconds + -> Maybe (Timeline.LookAhead Movement) + -> State + -> State springInterpolation prevEndTime _ target targetTime now _ state = let wobble = @@ -533,7 +541,7 @@ interpolateBetween startTimeInMs maybePrevious target targetTimeInMs now maybeLo -- jumpSize 0.25 -- starting t - 0.5 + (guessTime now curve) -- depth 0 @@ -617,7 +625,22 @@ interpolateBetween startTimeInMs maybePrevious target targetTimeInMs now maybeLo } -newVelocityAtTarget : Movement -> Milliseconds -> Timeline.LookAhead Movement -> PixelsPerSecond +guessTime : Float -> Spline -> Float +guessTime now (Spline one two three four) = + if (four.x - one.x) == 0 then + 0.5 + + else + (now - one.x) / (four.x - one.x) + + +{-| Prfer this function over `velocityAtTarget`! +-} +newVelocityAtTarget : + Movement + -> Milliseconds + -> Timeline.LookAhead Movement + -> PixelsPerSecond newVelocityAtTarget target targetTime lookAhead = let targetPosition = @@ -646,7 +669,11 @@ newVelocityAtTarget target targetTime lookAhead = velocityBetween targetPosition (Time.millis targetTime) (Pixels.pixels (toX 0)) (Time.millis lookAhead.time) -velocityAtTarget : (event -> Movement) -> Timeline.Occurring event -> Maybe (Timeline.Occurring event) -> PixelsPerSecond +velocityAtTarget : + (event -> Movement) + -> Timeline.Occurring event + -> Maybe (Timeline.Occurring event) + -> PixelsPerSecond velocityAtTarget lookup ((Timeline.Occurring target targetTime targetEndTime) as t) maybeLookAhead = -- This is the velocity we're shooting for. -- case maybeTargetDwell of @@ -737,7 +764,11 @@ createSpline config = startVelocity = if config.departure.departSlowly == 0 then - config.startVelocity + -- this is linear, + -- we don't care how fast we were going previously. + { x = 0 + , y = 0 + } else if ((config.startVelocity.x - zeroPoint.x) == 0) @@ -757,7 +788,10 @@ createSpline config = endVelocity = if config.arrival.arriveSlowly == 0 then - config.endVelocity + -- if this is 0, this is linear, + { x = 0 + , y = 0 + } else if ((config.endVelocity.x - zeroPoint.x) == 0)