Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Proposal for Vector2 and Vector3 operations  (Read 3078 times)

0 Members and 1 Guest are viewing this topic.

TomasRiker

  • Newbie
  • *
  • Posts: 5
    • View Profile
Proposal for Vector2 and Vector3 operations
« on: December 17, 2009, 10:32:02 am »
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:
Code: [Select]
////////////////////////////////////////////////////////////
/// \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:
Code: [Select]
////////////////////////////////////////////////////////////
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:
Code: [Select]
////////////////////////////////////////////////////////////
/// \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:
Code: [Select]
////////////////////////////////////////////////////////////
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);
}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Proposal for Vector2 and Vector3 operations
« Reply #1 on: December 17, 2009, 09:55:43 pm »
Hmm. I don't know whether it is a good idea to add some of the vector functions now and some later. As soon as Laurent implements them, loads of users are going to be confused why exactly these operations were added, while others – possibly more imporant ones like Length() – were not (consider the average user not thinking about implementation trouble). I'd rather suggest to leave the vector functionality for the moment, and add the new features all at once, as soon as a proper solution is found.

In my opinion, int-Vectors shouldn't get a function to compute the length. Which type to return? I also think, Integer-Vectors are rather used as 2D-indexes (for tiles, for example) than for real coordinates. In the other cases, one can still cast the vector to meet an explicitly stated type.

But anyway, I would choose other identifiers. ;)
Why the "Vector" prefix? There is no ambiguity in SFML. "Sq" expresses nothing, you'd rather write "SquaredLength" (or something like this) than "VectorLengthSq".
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything