[LibGDX] Interpolation

Hello, it’s me again… I never had such a headache using LibGDX in the past few months, as I ran into another weird behavior :smiley:

I want to use Interpolation between two vectors. The thing is, they dont work so far.

I use the following code to do the interpolation:

Initialization


realPos = new Vector2();
targetPos = new Vector2();
realPos.set(80.0f, 80.0f);
targetPos.set(280.0f, 80.0f);
interpolation = Interpolation.swing;

Update code


if (interpolation != null) {
            realPos.interpolate(targetPos, 0.1f, interpolation);
            Gdx.app.log("Position", realPos.x + "/" + realPos.y + " - " + targetPos.x + "/" + targetPos.y);
}

And this is what I get with logging… The first one is the real position vector, the second after the “-” mark is the target.
I basically goes to negative infinity…


Position: 71.200005/80.00001 - 280.0/80.0
Position: 62.01281/80.000015 - 280.0/80.0
Position: 52.42138/80.00002 - 280.0/80.0
Position: 42.40792/80.00003 - 280.0/80.0
Position: 31.953873/80.00004 - 280.0/80.0
Position: 21.039845/80.000046 - 280.0/80.0
Position: 9.645598/80.00005 - 280.0/80.0
Position: -2.2499952/80.00006 - 280.0/80.0
Position: -14.668996/80.00007 - 280.0/80.0
Position: -27.634434/80.00008 - 280.0/80.0
Position: -41.17035/80.000084 - 280.0/80.0
Position: -55.301846/80.00009 - 280.0/80.0
Position: -70.05513/80.0001 - 280.0/80.0
Position: -85.45756/80.00011 - 280.0/80.0
Position: -101.53769/80.000114 - 280.0/80.0

I must do something very stupid here ???

Look at how the interpolation you are using (Swing) is implemented:


Using an interpolation argument a of 0.1f with a default scale of 1.5f results in an interpolation factor of -0.02f when running that code.
That “Swing” interpolation curve looks like this:
0.0 -> -0.0 0.1 -> -0.02 0.2 -> -0.04 0.3 -> 0.0 0.4 -> 0.16 0.5 -> 0.5 0.6 -> 0.84 0.7 -> 1.0 0.8 -> 1.04 0.9 -> 1.02 1.0 -> 1.0

Thanks for the insight and the links!.. I’ll stick to linear for now, until I properly understand interpolation :smiley: (I had the assumption, that nearly all of the interpolations do the “same” thing, but with different “speed” matching the curves posted on the libgdx wiki.) It seems I was totally wrong :smiley:

Linear interpolation is just straight line from point a to b.

Swing goes away then swings past point b before swinging to destination.

The other interpolation aren’t the speed, but how they will travel… like bounce will go from a to b but then bounce a bit when it gets to the destination…

Here, this will show the change in relative distances by frame for each interpolation type.

Thanks for the explanation! I think I got it now :slight_smile: