Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Move Object from Point A to Point B  (Read 5267 times)

0 Members and 1 Guest are viewing this topic.

Orezar

  • Newbie
  • *
  • Posts: 38
    • View Profile
Move Object from Point A to Point B
« 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.

Zinlibs

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Move Object from Point A to Point B
« Reply #1 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));

    ...
}
Zoost & Zoom libraries : An easy way to create and handle geometric objets, animate and use them for better graphics !

Orezar

  • Newbie
  • *
  • Posts: 38
    • View Profile
Move Object from Point A to Point B
« Reply #2 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!

 

anything