I have a formula which returns a Lerp
Vector3
value in integers but the problem is it never reaches the desired target value. It's converting to pixel values first by multiplying by PPM which is float
32f.
private Vector3 lerp(final Vector3 source, final Vector3 target, float alpha) {
Vector3 sourcePPM = new Vector3(source.x * PPM, source.y * PPM, 0);
Vector3 targetPPM = new Vector3(target.x * PPM, target.y * PPM, 0);
sourcePPM.x += Math.round(alpha * (targetPPM.x - sourcePPM.x));
sourcePPM.y += Math.round(alpha * (targetPPM.y - sourcePPM.y));
source.x = sourcePPM.x / PPM;
source.y = sourcePPM.y / PPM;
source.z = 0;
return source;
}
I initialize my vector variable source
to new Vector3(0.0, 0.0, 0.0)
. Then, each frame I update it like this:
source = lerp(source, new Vector3(26.0, 29.0, 0.0), 0.06f);
But instead of eventually reaching the target, the value plateaus at a certain point:
Target destination: (26.0,29.0,0.0)
Actual final destination: (25.75,28.75,0.0)
Falling short of the target destination. If I don't round the values then it gets there fine but I need it to be in whole pixels.
The final line of intermediate values (too many to add all of them):
(25.0,27.90625,0.0)
(25.0625,27.96875,0.0)
(25.125,28.03125,0.0)
(25.1875,28.09375,0.0)
(25.25,28.15625,0.0)
(25.28125,28.21875,0.0)
(25.3125,28.28125,0.0)
(25.34375,28.3125,0.0)
(25.375,28.34375,0.0)
(25.40625,28.375,0.0)
(25.4375,28.40625,0.0)
(25.46875,28.4375,0.0)
(25.5,28.46875,0.0)
(25.53125,28.5,0.0)
(25.5625,28.53125,0.0)
(25.59375,28.5625,0.0)
(25.625,28.59375,0.0)
(25.65625,28.625,0.0)
(25.6875,28.65625,0.0)
(25.71875,28.6875,0.0)
(25.75,28.71875,0.0)
(25.75,28.75,0.0)
Math.round(alpha * (targetPPM.x - sourcePPM.x))
is zero, leadingsource
to not be modified. \$\endgroup\$