Skip to content

Commit 9b789e2

Browse files
committed
Fixed horizontal slowness on improved Time-based Animation
1 parent 0ce07d8 commit 9b789e2

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

QtFPSvsTIMEAnimation/time_canvas.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,23 @@ void TimeCanvas::move(float dt)
2222
{
2323
/* Update position of square */
2424

25-
_x += std::ceil((_dx * dt * 60) / 1000); // (2 * 16 * 60) / 1000 --> 1.92
25+
/* Rounding _dx up/down can affect the horizontal movement of the square.
26+
* Rounding down when _dx is negative is essential to fix the slowness
27+
* that happens when going from right to left.
28+
*/
29+
if (_dx > 0)
30+
_x += std::ceil((_dx * dt * 60) / 1000); // (2 * 16 * 60) / 1000 = 1.92 = 2
31+
else
32+
_x += std::floor((_dx * dt * 60) / 1000); // (-2 * 16 * 60) / 1000 = -1.92 = -2
2633

34+
/* Rounding _dy up/down can also affect the vertical move of the square.
35+
* If _dy is positive, round up, else the square will move sideways only.
36+
* If _dy is negative, round down, else the square won't go up after colliding.
37+
*/
2738
if (_dy > 0)
28-
{
29-
// If _dy is positive, round up, else animation will move sideways only
3039
_y += std::ceil((_dy * dt * 60) / 1000); // (1 * 16 * 60) / 1000 = 0.96 = 1
31-
}
32-
else
33-
{
34-
// If _dy is negative, round down, else animation will not bounce up
35-
_y += std::floor((_dy * dt * 60) / 1000); // (-1 * 16 * 60) / 1000 = -0.96 = -1
36-
}
40+
else
41+
_y += std::floor((_dy * dt * 60) / 1000); // (-1 * 16 * 60) / 1000 = -0.96 = -1
3742

3843
if ( _x <= 0 || (_x >= (width()-1) - _sq_sz) ) {
3944
_dx *= -1;

0 commit comments

Comments
 (0)