kikito / anim8

An animation library for LÖVE
MIT License
730 stars 88 forks source link

Wrong frame duration for first and last frames #32

Open eduardomezencio opened 6 years ago

eduardomezencio commented 6 years ago

This is a problem that will only happen if increments to timer are discrete units, so it does not happen in the demo, where dt is used for increment.

Create an animation with frame duration 4, and then update it each frame with animation:update(1). Each frame in the animation should be displayed for 4 game frames. With the current anim8 code, while every inner frame shows with the correct duration, the first frame always shows for one frame more and the last frame for one frame less than expected. Let's say I have this animation with 4 frames, with each frame with duration 4. Instead of seeing the expected 1111 2222 3333 4444 1111 ... loop, I see 11111 2222 3333 444 11111 ... .

I corrected this for use in my personal project, and I think my modification could be also applied in the official anim8 code . The change is as follows:

This line in Animation:update: local loops = math.floor(self.timer / self.totalDuration)

Becomes: local loops = math.ceil(self.timer / self.totalDuration) - 1

(I also changed the next line if loops ~= 0 then to if loops > 0 then to make it clearer, but it's not necessary)

floor(x) is the same as ceil(x) - 1, except for integer numbers. (floor(1.2) == ceil(1.2) - 1, but floor(1.0) != ceil(1.0) - 1) And that's exactly what causes this problem, because the frame should change only when loops is greater than 1 and not when it's exactly equal to 1. And that's also what makes it not appear when dt is not given as discrete units, since chances are you'll never get an exact number if you keep adding dt.

martin-braun commented 6 years ago

Could you please make a pull request out of this? I think kikito seem to be pretty busy at the moment, a pull request would make things easier on his side.

eduardomezencio commented 6 years ago

Sure, I just wanted to be sure he would approve it. I'll do it later tonight.

eduardomezencio commented 6 years ago

I still haven't done the pull request because I found a little conflict between what I did and the commit 902b418966bd04cbee0acc9ac9cf9c0b50124336. They are related, but it seems like it does not solve my problem and the two changes together also don't work correctly. I'll have to wait until I have some time to check this carefully and ensure the logic is correct.