Hello Game Devs,
I am working on a class that is supposed to smoothly translate 2D geometry beteen 2 Points over time. I am not really happy with my solution and wonder if other developers can provide some feedback in terms of other solutions or things they would do different. The following is an example of the current state of my solution.
This is the function that executes the Translation algorithm...
void TranslateAnimation::update(sf::Time time)
{
if (doUpdate)
{
// Compute distance between current position of menuElement and goal position.
float distance = vectorLength(to - menuElement->getPosition());
/* If distance is smaller than a minimum, just place menuElement at goal position
and stop the process.
*/
if (distance < minDistance)
{
menuElement->setPosition(to);
doUpdate = false;
finished = true;
currentVelocity = (to - menuElement->getPosition()) * velocityModifier;
return;
}
/* Move the menuElement relative to it's current position by the current velocity.
*/
menuElement->move(currentVelocity);
// Decrease velocity with each tick.
currentVelocity *= 0.8f;
static sf::Vector2f minVelocity{ 10.f, 10.f };
// If velocity is smaller than a minimum, reset it to it's initial value.
if (abs(currentVelocity.x) < minVelocity.x || abs(currentVelocity.y) < minVelocity.y)
{
currentVelocity = (to - menuElement->getPosition()) * velocityModifier;
}
}
}
The relevant .h and .cpp can be found here: https://github.com/Dante3085/ProjectSpace/blob/developer/src/header/TranslateAnimation.h, https://github.com/Dante3085/ProjectSpace/blob/developer/src/animation/TranslateAnimation.cpp
Thanks!