Created
May 24, 2024 22:32
-
-
Save berkaytumal/9d26e23dbefa9173eede22e127757665 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
const iSpring = { | |
helpers: { | |
transition: (firstInput, secondInput, transitionValue) => { | |
if (typeof firstInput == "number") { | |
return firstInput + (secondInput - firstInput) * transitionValue; | |
} else { | |
var result = []; | |
for (var i = 0; i < firstInput.length; i++) { | |
result[i] = firstInput[i] + (secondInput[i] - firstInput[i]) * transitionValue; | |
} | |
return result; | |
} | |
}, | |
}, | |
customSpring: function (initial_velocity = 0, initial_position = 1, object_mass = 1, friction_coefficient = 2, spring_constant = 2, duration = 1000) { | |
function clc(m, b, k, x0, v0) { | |
return function calculate(t) { | |
t *= 5000 / duration | |
var omega = Math.sqrt( | |
((4 * m * k - Math.pow(b, 2)) > 0 ? (4 * m * k - Math.pow(b, 2)) : 0) / | |
(2 * m) | |
) | |
const a = b / 2 * m | |
if (omega == 0) { | |
return Math.pow(Math.E, -a * t) * (x0 + ((v0 + a * x0) / (1 / Number.MAX_SAFE_INTEGER)) * Math.sin((1 / Number.MAX_SAFE_INTEGER) * t)) | |
} else { | |
return Math.pow(Math.E, -a * t) * (x0 * Math.cos(omega * t) + ((v0 + a * x0) / omega) * Math.sin(omega * t)) | |
} | |
} | |
} | |
if (typeof initial_position == "number") { | |
var [m, b, k, x0, v0] = [Number(object_mass), Number(friction_coefficient), Number(spring_constant), Number(initial_position), Number(initial_velocity)] | |
return { calculate: clc(m, b, k, x0, v0) } | |
} else { | |
var calculations = [] | |
initial_velocity.forEach((value, index) => { | |
calculations.push(clc(initial_velocity[index], initial_position[index], object_mass, friction_coefficient, spring_constant, duration)) | |
}); | |
return { | |
calculate: function (t) { | |
var result = [] | |
calculations.forEach(calc => { | |
result.push(calc.calculate(t)) | |
}); | |
return result; | |
} | |
} | |
} | |
}, | |
} | |
window.iSpring = iSpring |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment