mobius3 / tweeny

A modern C++ tweening library
http://mobius3.github.io/tweeny
MIT License
742 stars 53 forks source link

Fixed accuracy in some cases #42

Open sampad1370 opened 6 months ago

sampad1370 commented 6 months ago

Also, it's improved with removing dividing and multiplying with "total" in every step.

It will be fixed incorrect result in the following example:

auto tween = tweeny::from(1.0f, 1.0f) .to(7.0f, 7.0f) .during(60) .via(tweeny::easing::enumerated::linear) .onStep([](float i, float f) { printf("running: i=%f f=%f\n", i, f); fflush(stdout); return false; }); while (tween.progress() < 1.0f) { auto [value1, value2] = tween.peek(tween.progress()); printf(" progress:%f and values is: %f,%f\n", tween.progress(), value1, value2); fflush(stdout); tween.step(10); } fflush(stdout); tween.backward(); while (tween.progress() > 0.0f) { auto [value1, value2] = tween.peek(tween.progress()); printf(" progress:%f and values is: %f,%f\n", tween.progress(), value1, value2); fflush(stdout); tween.step(10); } auto [value1, value2] = tween.peek(tween.progress()); printf(" progress:%f and values is: %f,%f\n", tween.progress(), value1, value2);

I get following result on Ubuntu 22.04 with gcc-11 without this commit:

progress:0.000000 and values is: 1.000000,1.000000 running: i=2.000000 f=2.000000 progress:0.166667 and values is: 2.000000,2.000000 running: i=3.000000 f=3.000000 progress:0.333333 and values is: 3.000000,3.000000 running: i=4.000000 f=4.000000 progress:0.500000 and values is: 4.000000,4.000000 running: i=5.000000 f=5.000000 progress:0.666667 and values is: 5.000000,5.000000 running: i=6.000000 f=6.000000 progress:0.833333 and values is: 6.000000,6.000000 running: i=7.000000 f=7.000000 progress:1.000000 and values is: 7.000000,7.000000 running: i=6.000000 f=6.000000 progress:0.833333 and values is: 6.000000,6.000000 running: i=4.900000 f=4.900000 progress:0.666667 and values is: 4.900000,4.900000 running: i=3.900000 f=3.900000 progress:0.500000 and values is: 3.900000,3.900000 running: i=2.900000 f=2.900000 progress:0.333333 and values is: 2.900000,2.900000 running: i=1.900000 f=1.900000 progress:0.166667 and values is: 1.900000,1.900000 running: i=1.000000 f=1.000000 progress:0.000000 and values is: 1.000000,1.000000

but it must be:

progress:0.000000 and values is: 1.000000,1.000000 running: i=2.000000 f=2.000000 progress:0.166667 and values is: 2.000000,2.000000 running: i=3.000000 f=3.000000 progress:0.333333 and values is: 3.000000,3.000000 running: i=4.000000 f=4.000000 progress:0.500000 and values is: 4.000000,4.000000 running: i=5.000000 f=5.000000 progress:0.666667 and values is: 5.000000,5.000000 running: i=6.000000 f=6.000000 progress:0.833333 and values is: 6.000000,6.000000 running: i=7.000000 f=7.000000 progress:1.000000 and values is: 7.000000,7.000000 running: i=6.000000 f=6.000000 progress:0.833333 and values is: 6.000000,6.000000 running: i=5.000000 f=5.000000 progress:0.666667 and values is: 5.000000,5.000000 running: i=4.000000 f=4.000000 progress:0.500000 and values is: 4.000000,4.000000 running: i=3.000000 f=3.000000 progress:0.333333 and values is: 3.000000,3.000000 running: i=2.000000 f=2.000000 progress:0.166667 and values is: 2.000000,2.000000 running: i=1.000000 f=1.000000 progress:0.000000 and values is: 1.000000,1.000000

The result of direct stepping and backward stepping is different! And I think there are many similar cases. And I think extra divining and multiplying can be removed. Please check my commit.