Last active
November 29, 2016 10:19
-
-
Save DefinitelyMaybe/9d321a9371142f20e20b1c615a2e9dfb to your computer and use it in GitHub Desktop.
matplotlib can be installed via pip
This file contains 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
import matplotlib.pyplot as plt | |
import math | |
def SmoothUpdate(objPos, objVel, finPos, deltaTime, dampingRatio, angularFrequency): | |
""" | |
This function is a derivative of work done by: | |
Copyright (c) 2008-2012 Ryan Juckett | |
http://www.ryanjuckett.com/ | |
This software is provided 'as-is', without any express or implied | |
warranty. In no event will the authors be held liable for any damages | |
arising from the use of this software. | |
Permission is granted to anyone to use this software for any purpose, | |
including commercial applications, and to alter it and redistribute it | |
freely, subject to the following restrictions: | |
1. The origin of this software must not be misrepresented; you must not | |
claim that you wrote the original software. If you use this software | |
in a product, an acknowledgment in the product documentation would be | |
appreciated but is not required. | |
2. Altered source versions must be plainly marked as such, and must not be | |
misrepresented as being the original software. | |
3. This notice may not be removed or altered from any source | |
distribution. | |
""" | |
if dampingRatio < 0 or angularFrequency < 0: | |
print("The damp ratio and angular frequency must be greater than 0.") | |
return | |
#initial position and velocity relative to the finish position | |
x0 = objPos - finPos | |
v0 = objVel | |
if dampingRatio > 1: | |
#over-damped equation | |
za = -angularFrequency * dampingRatio | |
zb = angularFrequency * math.sqrt(dampingRatio*dampingRatio - 1) | |
z1 = za - zb | |
z2 = za + zb | |
exp1 = math.exp( z1 * deltaTime ) | |
exp2 = math.exp( z2 * deltaTime ) | |
#update motion | |
c1 = (v0 - x0 * z2) / (-2 * zb) # z1 - z2 = -2*zb | |
c2 = x0 - c1 | |
return finPos + c1*exp1 + c2*exp2 #objPos = | |
#objVel = c1*z1*exp1 + c2*z2*exp2 | |
elif dampingRatio == 1: | |
#critically damped equation | |
exp1 = math.exp( -angularFrequency * deltaTime ) | |
#update motion | |
c1 = v0 + angularFrequency * x0 | |
c2 = x0 | |
c3 = (c1*deltaTime + c2) * exp1 | |
return finPos + c3 #objPos = | |
#objVel = (c1*exp1) - (c3*angularFrequency) | |
else: | |
#under-damped equation | |
w = angularFrequency * dampingRatio #omegaZeta | |
a = angularFrequency * math.sqrt(1 - dampingRatio*dampingRatio) | |
exp1 = math.exp( -w * deltaTime ) | |
cos1 = math.cos( a * deltaTime ) | |
sin1 = math.sin( a * deltaTime ) | |
#update motion | |
c1 = x0 | |
c2 = (v0 + w*x0) / a | |
return finPos + exp1*(c1*cos1 + c2*sin1) #objPos = | |
#objVel = -exp1*( (c1*w - c2*a)*cos1 + (c1*a + c2*w)*sin1) | |
plt.grid(True) | |
#x axis | |
xrange1 = [(2*math.pi*i)/32 for i in range(64)] | |
#y axis | |
yrange1 = [math.cos(i) for i in xrange1] | |
#yrange2 = [0 for i in xrange1] | |
#yrange2 = [math.cos(i)/math.pow(math.e, i) for i in xrange1] | |
#yrange3 = [-i/(2*math.pi) + 1 for i in xrange1] | |
#yrange4 = [math.cos(i)/math.pow(math.e, i) for i in xrange1] | |
yrange2 = [SmoothUpdate(1, 0, 0, i, 2, 1) for i in xrange1] | |
yrange3 = [SmoothUpdate(1, 0, 0, i, 1, 1) for i in xrange1] | |
yrange4 = [SmoothUpdate(1, 0, 0, i, .75, 1) for i in xrange1] | |
yrange5 = [SmoothUpdate(1, 0, 0, i, .5, 1) for i in xrange1] | |
plt.plot(xrange1, yrange1, "-", xrange1, yrange2, "-", xrange1, yrange3, "-", xrange1, yrange4, "-", xrange1, yrange5, "-") | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment