It would be cool if we could have a member function within sf::Vector2f that takes a Vector2 parameter and normalizes the member data x/y and the paramaters x/y. Here's what i'm using, it can be adapted to sf::Vector2f pretty easily.
Vector2.h
namespace gtp
{
struct Vector2
{
Vector2();
Vector2(float x, float y);
Vector2& operator=(const Vector2& param);
float x;
float y;
};
Vector2 normalize(Vector2 Target, Vector2 Current);
}
Vector2.cpp
namespace gtp
{
Vector2 gtp::normalize(Vector2 Target, Vector2 Current)
{
Vector2 _Normal;
float SlopeX = Target.x - Current.x;
float SlopeY = Target.y - Current.y;
if ((SlopeY == 1) || (SlopeY == -1))
{
_Normal.y = SlopeY;
_Normal.x = SlopeX;
}
else
{
_Normal.y = (SlopeY / SlopeY);
_Normal.x = (SlopeX / SlopeY);
}
return _Normal;
}
}
Normalization is basically finding the delta between 2 coords and making it a unit of one (for unit moving).