Hey folks, first a disclaimer: this is a general "gamedev" question, not an "I need help with SFML" question. I hope it's not irritating to the devs to ask this here but I just trust the regular posters here more than other boards and my code in question does use SFML.
The player controls multiple units in my game and the "camera", aka the game world sf::View, moves over to them when a unit is selected. In order to move long distances faster, I tell the view move a portion of the remaining distance between its current position and the destination.
My problem is with the last few frames of movement. I get a lot of "stuttering" which I know stems from the non-integer position of the view; but, if I ceil or floor the position of the view I get an accuracy problem: The view will be off by a large amount (like 8.f) on the x-axis when it reaches the exact desired point on the y-axis or vice versa. This causes a very unnatural jump at the end of movement.
The smoothest movement that I have been able to get is by not flooring/ceiling the floating values and telling the view to stop if the distance remaining in both the x-axis and the y-axis is less than 1.f. My code is posted below.
My questions would be: Would you consider this the ideal way to move the view from one point to another? Is there a way to get smoother movement?
void Camera::setDestination(sf::Vector2f V}
{
Difference = V - viewWorld->getCenter();
State = States::Moving;
}
void Camera::update()
{
// if State == Moving
viewWorld->move(Difference * Speed);
Difference = Destination - viewWorld->getCenter();
if ((std::abs(Difference.x) < 1.f) && (std::abs(Difference.y) < 1.f))
{
State = States::Stop;
viewWorld->setCenter(Destination);
}
}