# Interpolation

Is there some interpolation function I could use to do slow-parent-like stuff? I was thinking of does something like the average formula ((start_point + end_point) / 2) but change the 2 to something that would get the number that is 1% [between the two points] on the start point side. But I would like some input from the more experienced programmers. I was wondering if this would work with every possible pair of points, negative or positive.

Thanks

A more generic function than averaging is a linear interpolation. Below is some code to do this. factor is a value between 0 and 1.

``````
def lerp(vec1, vec2, factor):
return (1-factor) * vec1 + factor * vec2

``````

Notice that a factor of 0.5 will give you the average.

What are you interpolating? Position, rotation or both?
I would get the vector from the object to the target, scale it by something, and apply that as motion (this would only deal with position, and is very linear. You may want to factor in the way the owner is currently facing). Then I would get the vector of the target object and align the object to match using alignToVect

But there are thousands of ways of interpolating, depending on the type of movement you are looking for.

What are you interpolating? Position, rotation or both?

Maybe any list of three numbers? For Location, Rotation, or Scale.

HERE we go. . . But how is it used. I see a bunch of people using it with stuff I don’t use. Like bone rotation, or something weird.

Nevermind. Got it!

``````import bge
from mathutils import Vector

def main():
cont = bge.logic.getCurrentController()
owner = cont.owner

target = cont.actuators['hold'].object

owner.worldPosition = owner.worldPosition.lerp(target.worldPosition, .1)
``````

Wait, I have a problem now. alignAxisToVect doesn’t work correctly with lerping. . .

How are you using it?

Here is my example blend. The camera is parented to the lerping cube. So It should look at the target the whole time.

### Attachments

LERP.blend (482 KB)

In line 16, you need to get the vector from the camera to the little cube. It should be:

``````

owner.alignAxisToVect(owner.getVectTo(target.worldPosition)[1], 1, .1)

``````

Hmm. . . Thank you