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

Author Topic: moveTo method for Vector2  (Read 3122 times)

0 Members and 1 Guest are viewing this topic.

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
moveTo method for Vector2
« on: October 01, 2016, 08:17:21 pm »
Hello. Very often in games there is need to move object A to object B smoothly. Can this function be implemented in SFML? Something like this:
pointA.moveTo(pointB, 1f);
//=> pointA is now closer to pointB by 1 pixel
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: moveTo method for Vector2
« Reply #1 on: October 01, 2016, 08:35:10 pm »
It's not so common to move by one pixel. And your solution wastes CPU cycles because you recompute (pointB - pointA) for every pixel to move.

What is more common is to store a velocity and compute the move with the elapsed time.

pointA += velocity * elapsed.asSeconds();
Laurent Gomila - SFML developer

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Re: moveTo method for Vector2
« Reply #2 on: October 01, 2016, 09:23:45 pm »
1 pixel I used just for example  ;)

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: moveTo method for Vector2
« Reply #3 on: October 04, 2016, 05:58:41 pm »
You can interpolate the two positions. To move a specific amount, you could multiply by a unit vector of the path.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*