I am trying to create an animation algorithm in C++ for a UI framework, but I encounter an issue where the movement speed is not consistent—it gradually slows down over time.
Here’s how I have implemented the algorithm:
static std::map<wgUI_Base*, std::unordered_map<unsigned int,
wgUI_Value7_Initial<float, bool, wgUI_2D, wgUI_2D, wgUI_3D, wgUI_3D, wgUI_4D>>>Animation;
void wgUI_Animation::Update() {
for (auto& i : Animation) {
for (auto& j : i.second) {
if (j.second.second) {
float time = wgUI_Time::GetTimeInCountTime(j.first);
if (time <= j.second.first) {
float ratio = time / j.second.first;
if (j.second.third != wgUI_2D()) {
wgUI_2D move = j.second.third * ratio;
j.second.third = j.second.third - move;
i.first->SetPoint(i.first->GetPoint() + move);
}
if (j.second.fourth != wgUI_2D()) {
wgUI_2D size = j.second.fourth * ratio;
j.second.fourth = j.second.fourth - size;
i.first->SetSize(i.first->GetSize() + size);
}
if (j.second.fifth != wgUI_3D()) {
wgUI_3D bg = j.second.fifth * ratio;
j.second.fifth = j.second.fifth - bg;
i.first->SetBackground(i.first->GetBackground() + bg);
}
if (j.second.sixth != wgUI_3D()) {
wgUI_3D bd = j.second.sixth * ratio;
j.second.sixth = j.second.sixth - bd;
i.first->SetBorder(i.first->GetBorder() + bd);
}
if (j.second.seventh != wgUI_4D()) {
wgUI_4D bdt = j.second.seventh * ratio;
j.second.seventh = j.second.seventh - bdt;
i.first->SetBorderThickness(i.first->GetBorderThickness() + bdt);
}
i.first->ApplyAlignment();
i.first->ApplyOrientations();
}
else {
wgUI_Time::StopCountTime(j.first);
j.second.second = false;
j.second.third = j.second.thirdInitial;
j.second.fourth = j.second.fourthInitial;
j.second.fifth = j.second.fifthInitial;
j.second.sixth = j.second.sixthInitial;
j.second.seventh = j.second.seventhInitial;
}
}
}
}
}
When I apply this algorithm, the animation’s movement speed becomes inconsistent, gradually slowing down over time. The ratio calculation seems to cause the issue, making the speed decrease as time progresses.
And the question is.
Why is the animation speed slowing down over time?
How can I ensure that the animation moves at a consistent speed?
Are there any improvements or best practices to handle animation timing and interpolation in C++?
added
wgUI_Time::GetTimeInCountTime(j.first) returns the current time for the animation.
The ratio is calculated as time / j.second.first.
The properties such as j.second.third, j.second.fourth, etc., are adjusted based on this ratio.
Please provide any insights or suggestions to help resolve this issue effectively. Thank you!