Keyframe animation is the process of creating an animation by listing specific values at particular times. For the times in between those "key" times, values are interpolated. For example, if we want to animate the z rotation of a short head nod, we might specify the following sequence of key times/values:
double headNod[][] = { {0,0}, {0.25,-0.5}, {0.5,0.5}, {0.75,0} };The above data specifies a rotation of the neck joint about the x axis that is 0.75 seconds long. At the start of the movement, the head is level. After 1/4 second, the head has rotated to a negative angle about the x axis, so as to lift the chin. After 1/2 second the head has rotated into a positive angle about the x axis, as to lower the chin. After 3/4 second (the end of the animated movement), the head has returned to a level orientation.
The question remains of how to interpolate this data, so as to define values between these key times. The simplest form of interpolation is linear interpolation, in which we draw a straight line between successive time/value pairs.
If a time lies between two particular key times time_{k} and time_{k+1}, and if the values at those two key times are, respectively, value_{k} and value_{k+1}, then linear interpolation at this time, where time_{k} ≤ time < time_{k+1}, can be computed by:
value = value_{k} + (value_{k+1} - value_{k}) * (time - time_{k}) / (time_{k+1} - time_{k})A deficiency of linear interpolation is that it produces animations which contain sudden changes in velocity at the key times. To create smoother interpolations, we would like to match the slope at the end of one interval, and the beginning of the next one. To do this, we can use cubic polynomial splines, which will allow us to specify any slopes we want at the beginning and at the end of an interval:
at^{3} + bt^{2} + ct + dCreating a smooth interpolating function by converting beginning and end values and beginning and end slopes to the four coefficients (a,b,c,d) of a cubic polynomial is called Hermite interpolation, named for the french mathematician Charles Hermite, who originated the method.
In the following discussion, we will refer to the value at t=0 as P1, the value at t=1 as P4, the derivative at t=0 as R1, the derivative at t=1 as R4. This slightly odd notation is for compatibility with other forms of splines that we will be discussing later on in the class.
As we discussed in class, this method of going from values and derivatives to the coefficients of a cubic polynomial is done by realizing that we can create the following orthogonal basis for cubic polynomials:
2t^{3}-3t^{2}+1 | -2t^{3}+3t^{2} | t^{3}-2t^{2}+t | t^{3}-t^{2} |
These four polynomials are "orthogonal" in the sense that each one affects one and only one of our four geometric constraints: The first affects only P1, the second affects only P4, the third affects only R1, and the fourth affects only R4.
The desired cubic polynomial is just a weighted sum of these four basis cubic polynomials:
at^{3} + bt^{2} + ct + d = P1 (2t^{3}-3t^{2}+1) + P4 (-2t^{3}+3t^{2}) + R1 (t^{3}-2t^{2}+t) + R4 (t^{3}-t^{2})
We can also express this as a matrix×vector product.
a | 2 | -2 | 1 | 1 | P1 | ||
b | ← | -3 | 3 | -2 | -1 | P4 | |
c | 0 | 0 | 1 | 0 | R1 | ||
d | 1 | 0 | 0 | 0 | R4 |
The above discussion only talks about splines that go between t=0 and t=1. What about the more general case where time varies between time_{k} and time_{k+1}?
To handle this we need to multiply the left and right slopes by the width of the interval:
P1 = value_at_time_{k} P4 = value_at_time_{k+1} R1 = slope_at_time_{k} × (time_{k+1} - time_{k}) R4 = slope_at_time_{k+1} × (time_{k+1} - time_{k})
Then the result will be correct when applied the cubic polynomial is evaluated at 0 ≤ t < 1, and the resulting value is used at:
time = time_{k} + t (time_{k+1} - time_{k}).
The second part of your homework, which is all due on Thursday March 22 before class begins, is to implement Hermite splines, and then use them to create an interesting animation.
Your animation should specify time/value pairs for each scalar quantity that you wish to vary over time. To approximate the slope at any time_{k} you can use:
slope_at_time_{k} = (value_{k+1} - value_{k-1}) / (time_{k+1} - time_{k-1})
For the slope at time_{0} you can use:
slope_at_time_{0} = 2 (value_{1} - value_{0}) / (time_{1} - time_{0})and for the slope at the last time_{n} you can use:
slope_at_time_{n} = 2 (value_{n} - value_{n-1}) / (time_{n} - time_{n-1})
Use the time-varying scalar quantities produced by your Hermite splines as arguments to your translate, rotate and scale method calls at each frame of your animation. to your translate, rotate and scale method calls. See if you can tell a visual story. :-)