Math: Ease In, ease Out a displacement using Hermite curve with time constraint
First, let's make a cubic hermite spline function:
/*
t - in interval <0..1>
p0 - Start position
p1 - End position
m0 - Start tangent
m1 - End tangent
*/
double CubicHermite(double t, double p0, double p1, double m0, double m1) {
t2 = t*t;
t3 = t2*t;
return (2*t3 - 3*t2 + 1)*p0 + (t3-2*t2+t)*m0 + (-2*t3+3*t2)*p1 + (t3-t2)*m1;
}
Now your task is to calculate the p0, p1, m0 and m1 for both ease-in and ease-out portions. Let us add a few variables to make the math a bit easier to write:
double Interpolate(
double timeToAccel, double timeCruising, double timeToDecel,
double finalPosition,
double currentTime) {
double t1 = timeToAccel;
double t2 = timeCruising;
double t3 = timeToDecel;
double x = finalPosition;
double t = currentTime;
We need to specify where should the object be when it stops accelerating and starts decelerating. You can specify these however you please and still produce a smooth movement, however, we would like a somewhat "natural" solution.
Let's assume that the cruising speed is v
. During crusing, the object travels distance x2 = v * t2
. Now, when the object accelerates from 0 to speed v, it travels distance x1 = v * t1 / 2
. Same for deceleration x3 = v * t3 / 2
. Put all together:
x1 + x2 + x3 = x
v * t1 / 2 + v * t2 + v * t3 / 2 = x
From that we can calculate our speed and the distances:
double v = x / (t1/2 + t2 + t3/2);
double x1 = v * t1 / 2;
double x2 = v * t2;
double x3 = v * t3 / 2;
And now that we know everything, we just feed it into our cubic hermite spline interpolator
if(t <= t1) {
// Acceleration
return CubicHermite(t/t1, 0, x1, 0, v*t1);
} else if(t <= t1+t2) {
// Cruising
return x1 + x2 * (t-t1) / t2;
} else {
// Deceleration
return CubicHermite((t-t1-t2)/t3, x1+x2, x, v*t3, 0);
}
}
I tested this in Excel, here's the equivalent VBA code to play with. There are some divisions by zero for boundary conditions, I leave fix to this as an excercise to the reader
Public Function CubicHermite(t As Double, p0 As Double, p1 As Double, _
m0 As Double, m1 As Double) As Double
t2 = t * t
t3 = t2 * t
CubicHermite = (2 * t3 - 3 * t2 + 1) * p0 + _
(t3 - 2 * t2 + t) * m0 + (-2 * t3 + 3 * t2) * p1 + (t3 - t2) * m1
End Function
Public Function Interpolate(t1 As Double, t2 As Double, t3 As Double, _
x As Double, t As Double) As Double
Dim x1 As Double, x2 As Double, x3 As Double
v = x / (t1 / 2 + t2 + t3 / 2)
x1 = v * t1 / 2
x2 = v * t2
x3 = v * t3 / 2
If (t <= t1) Then
Interpolate = CubicHermite(t / t1, 0, x1, 0, v*t1)
ElseIf t <= t1 + t2 Then
Interpolate = x1 + x2 * (t - t1) / t2
Else
Interpolate = CubicHermite((t-t1-t2)/t3, x1+x2, x, v*t3, 0)
End If
End Function
This is straightforward using normal constant acceleration. Then the question becomes what velocity (v) do you need to accelerate to in order to complete the trip in the right amount of time, and this will tell you the acceleration you need to get to that velocity.
If the total time is t_t and the time of acceleration is t_a, then you have the distance traveled as the two accelerating and decelerating parts, and the constant velocity part:
x = 2*(a*t_a*t_a/2) + v*(t_t-2*t_a)
This can be solved for the acceleration by subbing in v=a*t_a, to find
a = x/(t_a*(t_t - t_a))
Here's some Python code that uses and plots the result of these equations, that shows both how to use the equations and what the result looks like:
from pylab import *
t_a, t_t, D = 3., 10., 1. # input values
a = D/(t_a*(t_t - t_a))
segments = (t_a, a), (t_t-2*t_a, 0.), (t_a, -a) # durations and accelerations for each segment
t0, x0, v0 = 0.0, 0.0, 0.0 #initial values for the segment
tdata, xdata = [], []
for t_segment, a in segments: # loop over the three segments
times = arange(0, t_segment, .01)
x = x0 + v0*times + .5*a*times*times
xdata.append(x)
tdata.append(times+t0)
x0 = x[-1] # the last x calculated in the segment above
v0 += a*t_segment
t0 += t_segment
plot(tdata[0], xdata[0], 'r', tdata[1], xdata[1], 'r', tdata[2], xdata[2], 'r')
xlabel("time")
ylabel("position")
show()