SFML community forums
Help => System => Topic started by: OniLinkPlus on January 23, 2010, 10:31:41 pm
-
I see they haven't been implemented. Will they be added soon?
Some Vector functions you probably should do(and code for some of them) are:
Vector2
#include <cmath>
T Direction(){// May want to use double or float instead
return atan2(y, x);
}
T Dot(Vector2<T>& Other){//May want to use double or float instead
return x*Other.x + y*Other.y;
}
T Length(){
return sqrt(x*x + y*y);
}
void Normalize(){
T Length = Length();
x /= Length;
y /= Length;
}
Vector3
#include <cmath>
Vector3<T>& Cross(Vector3<T>& Other){
return Vector3<T>(y*Other.z - Other.y*z, z*Other.x - x*Other.z, x*Other.y - y*Other.x);
}
T Dot(Vector3<T>& Other){
return x*Other.x + y*Other.y + z*Other.z;
}
T Length(){
return sqrt(x*x + y*y + z*z);
}
void Normalize(){
T Length = Length();
x /= Length;
y /= Length;
z /= Length;
}
I'll add more functions and examples as I think of them.
-
Why are you doing a normalization in your dot-calculation? ;) That should be a user-side decision.
-
Why are you doing a normalization in your dot-calculation? ;) That should be a user-side decision.
Dot product requires that the lengths are equal, so I needed a way to make them equal. I'm probably going to rewrite it a little to normalize B to the length of A.
EDIT: Done, but I normalize A to the length of B instead.
EDIT: Ah, I misread the Wikipedia article, the lengths don't have to be equal. Fixing...
-
Added Cross-Product to Vector3. Laurent, you gonna add these soon?
-
Dunno :wink:
-
This discussion isn't new. ;)
http://www.sfml-dev.org/forum/viewtopic.php?t=1937
http://www.sfml-dev.org/forum/viewtopic.php?t=1488
http://www.sfml-dev.org/forum/viewtopic.php?t=1232
http://www.sfml-dev.org/forum/viewtopic.php?t=651
I think it's better to wait with the integration of vector algebra functions into SFML. Several discussions in the past showed that it's not easy to implement corresponding functionality, which is intuitive to use, powerful and yet generic. Especially the last point is an important one, in my opinion – most of the proposals didn't take genericity and extensibility into account. Yours is one of them.
By the way, your code isn't correct C++ (for example you shouldn't return references to local objects, templates don't work like this and variables must be declared).