Created
February 28, 2016 08:48
-
-
Save aleclarson/fc42b4c587c82e0b2782 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
initialPosition = 0 | |
module.exports = | |
decay = | |
equation: "react" | |
dynamicsThreshold: 0.01 | |
minimalVelocityFactor: 10 | |
getElapsedTime: (initialVelocity, deceleration) -> | |
k = @dynamicsThreshold * @minimalVelocityFactor | |
velocity = Math.log Math.abs k / initialVelocity | |
velocity / Math.log deceleration | |
getFinalPosition: (initialVelocity, deceleration) -> | |
elapsedTime = @getElapsedTime initialVelocity, deceleration | |
@[@equation].getPosition initialVelocity, deceleration, elapsedTime | |
# Is this `velocity` high enough to reach this `finalPosition` at this `deceleration`? | |
isVelocityEnough: (initialVelocity, deceleration, finalPosition) -> | |
{ position } = @getFinalPosition initialVelocity, deceleration | |
if initialVelocity > 0 then position >= finalPosition | |
else position <= finalPosition | |
react: | |
getPosition: (initialVelocity, deceleration, elapsedTime) -> | |
kv = Math.exp 0 - elapsedTime * (1 - deceleration) | |
position: initialPosition + (initialVelocity / (1 - deceleration)) * (1 - kv) | |
velocity: initialVelocity * kv | |
pop: | |
getPosition: (initialVelocity, deceleration, elapsedTime) -> | |
kv = Math.pow deceleration, elapsedTime | |
kx = deceleration * (1 - kv) / (1 - deceleration) | |
position: initialPosition + initialVelocity * kx | |
velocity: initialVelocity * kv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment