SFML community forums
Help => Graphics => Topic started by: Orezar on October 29, 2011, 08:03:25 pm
-
Hello,
can someone explain me how I can move a Sprite from
a Point A to a Point B?
I didn't found a good described example...
Thanks.
-
Hi,
You need to interpolate the position of the sprite at each step of time, using a function like this :
sf::Vector2f Interpolate(
const sf::Vector2& pointA,
const sf::Vector2& pointB,
float factor
) {
if( factor > 1.f )
factor = 1.f;
else if( factor < 0.f )
factor = 0.f;
return pointA + (pointB - pointA) * factor;
}
sf::Vector2f pointA = ..., pointB = ...;
float factor = 0.f, speed = .1f;
sf::Sprite sprite;
sprite.SetPosition(pointA);
while( App.IsOpened() )
{
factor+=(App.GetFrameTime() * speed);
sprite.SetPosition(Interpolate(pointA, pointB, factor));
...
}
-
Hey, thanks for your answere, but this what you described set
only the Position of the Sprite with the Point A on Point B directly.
I want that the Sprite moves from Point A to Point B
like a torpedo that search the Position of a ship to destroy it.
EDIT:
WORKS, Thanks you so much! :)
My wrong was that the Speed was to high!