Interpolated position animation slows down with distance

Viewed 184

I am rendering cards that are being dealt. Player cards are at the bottom of the screen, dealer cards are more at the top. Cards are dealt from "offscreen" at the top. Thus the distance between cards starting position and player hand is longer than the distance between starting position and dealer hand.

I am calculating the cards current position using the interpolated_position value which is between 0.0 and 1.0. Because of the different distances, the animations have different animation speed though.

/// Updates the first animation in the queue then deletes it
void Update(float delta_time)
{
    if (pause == false && !animation_confs.empty())
    {
        interpolated_position += delta_time * (animation_confs[0].speed); // animation speed is 1.3 here
        if (interpolated_position >= 1.0f) // Finished animation reset values
        {
            interpolated_position = 0.0f;
            if (!animation_confs.empty())
            {
                animation_confs.erase(animation_confs.begin());
                return;
            }
        }
        InterpolateFrame();
    }
}

// Interpolates card position between two points based on interpolation factor
void InterpolateFrame()
{
    if (!animation_confs.empty())
    {
        float t = interpolated_position;

        float x1 = animation_confs[0].start.x;
        float x2 = animation_confs[0].goal.x;
        position.x = x1 + t * (x2 - x1);

        float y1 = animation_confs[0].start.y;
        float y2 = animation_confs[0].goal.y;
        position.y = y1 + t * (y2 - y1);
    }
}

What might be the reason for this behaviour? I thought that applying delta_time to the interpolation would keep the speed constant.

1 Answers

You want a miracle from us by providing a small snippet of code without sufficient additional details.

  • What is animation_confs[0].speed and is it constant for all cards?
  • What is delta_time and does it depend on how far a card is to be moved?

Now, as far as I can understand, you want to achieve the effect of constant speed of the cards, don't you? If so, I would pass the velocity as the argument of update and check whether the card has already made its due distance. But let's stick to your "interpolation" variable t that varies between 0 and 1. (of course it is confusing to call distance by t, pls change it). You measure the distance to a card in units of its distance between to the top of the screen and its final position. This is ok, but may be confusing. I suppose you do not want to introduce separate times for each object, so time is universal. If you measure distance in object-dependent units and the time is universal, then the velocity has to be object-dependent. Say, there are two shops near you, one 100 m, the other 1000 m from you. You walk 1m/s. If your unit of length is 100 m, then the 1st shop is 1 unit from you (just like your t) and your speed is 0.01 units of length per second. Take the other shop. Now the length unit is 1000 m, but your speed in these new unit system is 0.001 unit per second! Nominally, ten times slower (but the trick is that now I describe the same velocity in two different unit systems).

What you need to do is either abandon the "interpolated distance" and work in natural units of time, length and velocity, assumming that the velocity is constant for each object, or stick to your "interpolated distance", but make the velocity inversely proportional to the actual length that an object (a card) has to make in a fixed amount of time.

In other words, if the r.h.s of

interpolated_position += delta_time * (animation_confs[0].speed);

is object-independent, and the accumulated value of 1 for the l.h.s. signals the end of animation, you've achieved the constant time of animation of each object, no matter how far they had to make.

Related