Hello,
I know this has been discussed before, and Laurent's last comment on it was (as far as I know) that it is on the roadmap, but he hasn't found a solution yet that he likes.
The proposal is to add VectorDotProduct (for 2D and 3D), VectorLengthSq (for 2D and 3D) and VectorCrossProduct (for 3D only). These functions are "safe" because they can use the same type for their result as used in the vector. VectorLength would be a bit tricky because a the length of a Vector3i should not be an integer, and what would happen when the user instances the Vector template with a custom type that SFML can't possibly know in advance?
The proposed solution still allows the user to call VectorLengthSq, cast accordingly and then call sqrt or whatever function. Also, it makes you think if you really need the length or if the squared length (faster to compute!) would suffice, like when comparing distanes.
For some built-in types SFML could provide a VectorLength function (float, double, ints).
However, these three functions mentioned are safe in this aspect.
This is how it could look like.
I know it's trivial, but it should be done at some point!
Vector2.hpp:
////////////////////////////////////////////////////////////
/// \brief Vector dot product
///
/// This function computes the dot product of two vectors.
/// It is defined as the sum of the memberwise products.
/// When the result is zero, it means that the vectors point
/// into perpendicular directions.
///
/// \param left Left operand (a vector)
/// \param right Right operand (a vector)
///
/// \return The dot product of \a left and \a right
///
////////////////////////////////////////////////////////////
template <typename T>
T VectorDotProduct(const Vector2<T>& left, const Vector2<T>& right);
////////////////////////////////////////////////////////////
/// \brief Vector squared length
///
/// This function computes the squared length of a vector.
/// If the true length is desired, take the square root.
///
/// \param vec The vector
///
/// \return The squared length of \a vec
///
////////////////////////////////////////////////////////////
template <typename T>
T VectorLengthSq(const Vector2<T>& vec);
Vector2.inl:
////////////////////////////////////////////////////////////
template <typename T>
inline T VectorDotProduct(const Vector2<T>& left, const Vector2<T>& right)
{
return (left.x * right.x) + (left.y * right.y);
}
////////////////////////////////////////////////////////////
template <typename T>
inline T VectorLengthSq(const Vector2<T>& vec)
{
return (vec.x * vec.x) + (vec.y * vec.y);
}
---
Vector3.hpp:
////////////////////////////////////////////////////////////
/// \brief Vector dot product
///
/// This function computes the dot product of two vectors.
/// It is defined as the sum of the memberwise products.
/// When the result is zero, it means that the vectors point
/// into perpendicular directions.
///
/// \param left Left operand (a vector)
/// \param right Right operand (a vector)
///
/// \return The dot product of \a left and \a right
///
////////////////////////////////////////////////////////////
template <typename T>
T VectorDotProduct(const Vector3<T>& left, const Vector3<T>& right);
////////////////////////////////////////////////////////////
/// \brief Vector cross product
///
/// This function computes the cross product of two vectors.
/// The resulting vector is perpendicular to both vectors.
///
/// \param left Left operand (a vector)
/// \param right Right operand (a vector)
///
/// \return The cross product of \a left and \a right
///
////////////////////////////////////////////////////////////
template <typename T>
Vector3<T> VectorCrossProduct(const Vector3<T>& left, const Vector3<T>& right);
////////////////////////////////////////////////////////////
/// \brief Vector squared length
///
/// This function computes the squared length of a vector.
/// If the true length is desired, take the square root.
///
/// \param vec The vector
///
/// \return The squared length of \a vec
///
////////////////////////////////////////////////////////////
template <typename T>
T VectorLengthSq(const Vector3<T>& vec);
Vector3.inl:
////////////////////////////////////////////////////////////
template <typename T>
inline T VectorDotProduct(const Vector3<T>& left, const Vector3<T>& right)
{
return (left.x * right.x) + (left.y * right.y) + (left.z * right.z);
}
////////////////////////////////////////////////////////////
template <typename T>
inline Vector3<T> VectorCrossProduct(const Vector3<T>& left, const Vector3<T>& right)
{
return Vector3<T>(left.y * right.z - left.z * right.y,
left.z * right.x - left.x * right.z,
left.x * right.y - left.y * right.x);
}
////////////////////////////////////////////////////////////
template <typename T>
inline T VectorLengthSq(const Vector3<T>& vec)
{
return (vec.x * vec.x) + (vec.y * vec.y) + (vec.z * vec.z);
}