SFML community forums

Help => Graphics => Topic started by: Orezar on October 29, 2011, 08:03:25 pm

Title: Move Object from Point A to Point B
Post 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.
Title: Move Object from Point A to Point B
Post by: Zinlibs on October 29, 2011, 08:29:10 pm
Hi,

You need to interpolate the position of the sprite at each step of time, using a function like this :

Code: [Select]
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;
}


Code: [Select]
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));

    ...
}
Title: Move Object from Point A to Point B
Post by: Orezar on October 30, 2011, 08:09:51 am
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!