Code:
I have a function as follows:
bool Action::approach (img_comp &mover, sf::Vector2f start, sf::Vector2f end, int speed)
{
//Get the change in x and y
float delta_x = (end.x - start.x) / speed;
float delta_y = (end.y - start.y) / speed;
//Move the sprite
mover.sprite.move(delta_x, delta_y);
//Check if the sprite has met the end
if (mover.sprite.getPosition() == end)
return true;
return false;
}
(Where sf::Vector2f
is basically a struct with an x
and y
float parameter, e.g. a point on an x-y grid)
Question:
Unsurprisingly, this function has a possibility to never return true after passing the end point due to the rounding of float
. What algorithm do I use in this scenario to have my sprite land exactly on the end
point, regardless of rounding?
Note: Tolerance is not an answer to my question. It is merely a compromise. I want the algorithm that picks the points perfectly the first time, regardless of any rounding. If this is impossible, let me know.