Hey all, I think Vector math functions (normalize, magnitude, rotation, etc.) are important for any game developer, so I think they'd be a good addition to SFML. Right now, every time I get the latest SFML, I have to go back and add in those functions to the Vector2 class. Sure, it only takes a few seconds, but if everyone is using them (and I have sure used these in just about every game I have ever made), why not just include them? I searched the forum a bit, and found that Vector math had already been discussed, but the link provided is dead.
We have already had a discussion about vector functions (-> Link)...
I understand that Vector2 is an abstracted class, but, honestly, who uses Vector2 for anything other than math? I know both Ogre3D and Irrlicht provide extensive math helper functions for their Vector classes.
Vector2<T>& normalize()
{
float length = (float)(x*x + y*y);
if (length == 0.f)
return *this;
length = sqrt ( 1.f/length );
x = (T)(x * length);
y = (T)(y * length);
return *this;
}
Vector2<T>& rotateBy(float degrees, const Vector2<T>& center=Vector2<T>())
{
degrees *= 3.14159f/180.f;
const float cs = cos(degrees);
const float sn = sin(degrees);
x -= center.x;
y -= center.y;
T nx = (T)(x*cs - y*sn);
T ny = (T)(x*sn + y*cs);
x = nx;
y = ny;
x += center.x;
y += center.y;
return *this;
}
// returns trig angle
float getAngle() const
{
if (y == 0)
return x < 0 ? 180 : 0;
else if (x == 0)
return y < 0 ? 270 : 90;
if ( y > 0)
if (x > 0)
return atan(y/x) * 180.f/3.14159f;
else
return 180.0-atan(y/-x) * 180.f/3.14159f;
else
if (x > 0)
return 360.0-atan(-y/x) * 180.f/3.14159f;
else
return 180.0+atan(-y/-x) * 180.f/3.14159f;
}
float getLength()
{
return sqrt(x*x + y*y);
}