Movement interpolation - alpha ruins floating point and makes jerky moves

I’m facing a huge problem and I have no idea what’s causing it and how to fix this. I’m creating a game where I draw objects in Java. For drawing I use linear interpolation which uses ‘alpha’ value to move the object with a specific time value in one frame.

Here is how the interpolation algorithm looks like:

draw.x = current.getX() * alpha + previous.getX() * (1.0f - alpha);

Let’s assume that I’m standing in place and my positions: current and previous are 100 and 100.

No matter what ‘alpha’ is (0.234234, 0.9994234, 0.324235325) it should always return draw.x == 100.

But it doesn’t cause the compiler sometimes cuts 0.00000001 from the value and draw.x becomes 99.99999999.
Next, I need to use it to render the object and I don’t know what to do (round or cast) that it doesn’t break in the future.
If I cast it to INT (cause I can’t render objects on floats) - it becomes ‘99’ which makes the jerk (animation jumps).

If course I could round it to 100, but wouldn’t it break the app when moving (make unexpected jerks later?)?

If someone could help me - this is the last thing that I can’t handle!

Thanks :wink:

Floating point imprecision is something you have to deal with. Instead of comparing directly with ==, use an epsilon value.


if(Math.abs(b - a) < epsilon)

where epsilon is a small value like 0.000001.

I believe re-arranging the expression so only the delta is multiplied by alpha will (in this situation) avoid the issue.
e.g.

drawX = prevX + (currentX-prevX)*alpha;

floating point calculations are inherently unstable. For sufficiently different values, A-B can yield
a result with no valid bits at all. Even for “normal” values, you’re likely to get into trouble if any
calculation’s value approaches zero.

Its better to use fixed point, or rationals, or structure your calculations to not depend on the normal
laws of arithmetic. Putting a little slop into floating point comparisons can help, but it’s only papering
over the problem. The slop will build up and eventually your threshold will be exceeded.

chrislo27, ddyer

Is this:

epsilon = 0.00001
if(Math.abs(b - a) < epsilon)
//interpolate

the fixed point solution?
Should I really use it?
I mean - I don’t understand the whole term.

As I read - fixed point calculations are based on bitwise operations like

x >> 16

, not just by checking the epsilon.