The Way Towards Animation, Step One:
Linear Interpolation
by Thomas 'ThamasTah' van Dijk
Introduction
My first step towards animating the system illustrated in Skeletal Modelling is Linear Interpolation (LI). It is actually quite easily done, but I thought I'd write a tutorial on it anyways. Have fun.
Implementation
Okay, firstly, what is Linear Interpolation? And what is it for?
We use it to gradually change from one value to the next. We build a class and
call in 'interpolator'.
We use the following variables (you might want to make'em public. I made 'em
private and provided subs to set 'em):
Private startTick As Long
Private startValue As Double
Private endTick As Long
Private endValue As Double
startTick and endTick as longs indicating the tick count (using timeGetTime, getTickCount, or something similar), but it actually also could be anything else: what it really is, is the amount of (possible) steps between startValue and endValue.
The important function goes as follows:
Public Function getValue(longTick As Long) As Double
If longTick > endTick Then
If longTick is higher than endTick, getValue will allways be endValue, so don't even bother calculating...getValue = endValue
ElseIf longTick < startTick Then
Similar...getValue = startValue
Else
Now here's the real stuff:First, a temp step (you could integrate the to steps)
getValue = (longTick - startTick) / (endTick - startTick)
We divide ticks since startTick by length in ticks, giving us a value between 0 and 1, meaning how far into the fade we are.
getValue = startValue + ((endValue - startValue) * getValue)
Now, we take startValue, and add to that: the difference between start and end value, multiplied by the factor of how far into the fade we are.
Geddit?End If
End Function
That's all there is to it. If you don't get it, that's prolly because of my crap explanation: just build it yourself and experiment with it. This is an all purpose LI-er, which can be used for all kinds of stuff in games, from fades to moving platforms to enemy movement.
A typical way to use it would be:
Dim a As New interpolator
a.setStartValue 0
a.setEndValue 255
a.setStartTick timeGetTime
a.setDuration 5000
Do
theValue = getValue( timeGetTime )
e.g. blend pics using theValue as alphaVal!
Decide when to exit the loop...Loop
Conclusion
Not really much to conclude here ;)
[download]
Thomas 'ThamasTah' van Dijk
dec 2 2000