Example:
*
atan2,
cos and
sin are from math.h
// rotate to waypoint:
myObj.rotation = atan2(waypoint.y - myObj.y, waypoint.x - myObj.x);
// or just calculate this value into a variable if you don't want to rotate your object
// move forward (into the waypoint, based on the current object rotation)
myObj.position.x = cos(myObj.rotation) * SPEED * deltaTime;
myObj.position.y = sin(myObj.rotation) * SPEED * deltaTime;
Note that
SFML uses degrees and the
cos/sin/atan2 functions use radians. Make the conversion first.
If you want to stop/change waypoint using the distance between your object and the waypoint here is a simple distance function:
float distance(sf::Vector2f p1, sf::Vector2f p2) {
float xb = p2.x - p1.x;
float yb = p2.y - p1.y;
return sqrt((xb * xb) + (yb * yb));
}
I'm not sure if SFML has built in functions for this (i'm new here), but this is how I do it in most game development environments.