Node reached position LERP

Hello guys, I am using lerp to move my objects around, and the problem i am facing is, how do I tell if my node have reached the end point and be precise about it, because my update method is called at regular intervals, I was wondering how i can tell it to stop lerping because the node has reached the target point.

spatial.getposition().equals(target.getPosition()) doesnt really work :confused:

well the best i found was to do a proximity check, like when destination.distance(spatial ) < 0.1

That sounds like a good solution.

But what are you LERPing with? Generally you are interpolating between some starting position and some ending position… so you will know if you are there when the interpolation value is 1.0.

If instead you are doing something like “going halfway each time”… then your movement will be a bit strange, starting fast and ending really slow (mathematically you will never get there… but in practical terms you do).

So maybe you can say more about what you are doing.

as a general rule of thumb a float can never “equal” something, it can only be “approximately something”

for instance 2f + 3f is not 5f, it is 4.99999f (or it might be 5.0000000001f, not sure)

the only exception is when youve directly set the value

x = 5f
y = 5f

then x == y.

however 5f is not truly the number 5 exactly.

(this is why youre never supposed to use floating point numbers for currency.

Actually single-precision IEEE floating point (what Java uses for float) can represent integers precisely up to about 16 million. And (2f + 3f == 5f) always evaluates to true (try it!)

However, most fractions (even simple ones like 1.2) cannot be represented precisely as floats. So for instance (1.2f * 3f == 3.6f) always evaluates to false.

So Daniel’s point is a good one. Almost all floating-point comparisons for equality should include a tolerance.

i did not know that, i learned something new :D.

would there ever be a reason to want to utilize this in some way? or is it just some thing that happens to work out easily with the math so they included it in the spec.

But… for interpolation, all you need to do is:
[java]
if( t >= 1.0 ) {
// We’ve arrived
}
[/java]

And if it’s not that simple then it’s not really LERPing. It’s some other weird thing.

@icamefromspace said: i did not know that, i learned something new :D.

would there ever be a reason to want to utilize this in some way? or is it just some thing that happens to work out easily with the math so they included it in the spec.

Afaik it’s not explicit in the specification, just a side-effect of how numbers are represented in IEEE floating point.