SFML community forums
General => Feature requests => Topic started by: phear- on August 01, 2009, 05:24:35 pm
-
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).
-
You know that there can be other things in an Vector2 than floats? :D
-
This is not vector normalization (google/wikipedia it if you don't know what is it exactly).
This function is a very specific one that may be useful to you, but not a good candidate for being generic and integrated into SFML.
-
It's used within the XNA framework in their Vector2 class and I thought it might be used by other people who have to find the delta of 2 coords in a 1 unit distance. (I was told its called normalization and thats what it is called in that framework so my apologies if thats not what its called)
Edit: I just checked Wiki and it is called normalization:
A unit vector is any vector with a length of one; normally unit vectors are used simply to indicate direction. A vector of arbitrary length can be divided by its length to create a unit vector. This is known as normalizing a vector. A unit vector is often indicated with a hat as in รข.
-
This definition is right, but it doesn't match your function ;)
Normalizing is dividing the vector's components by its length so that its new length is 1.
Vector2 normalize(const Vector2& source)
{
float length = sqrt((source.x * source.x) + (source.y * source.y));
if (length != 0)
return Vector2(source.x / length, source.y / length);
else
return source;
}
Your function is doing something totally different: it computes the delta between 2 points, and then scale it so that its Y component is 1.
-
We have already had a discussion about vector functions (-> Link (http://www.sfml-dev.org/forum/viewtopic.php?t=1232))...