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

Author Topic: Vector2/Vector3 Zero member  (Read 5766 times)

0 Members and 1 Guest are viewing this topic.

Aesthete

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Matt Millwood
Vector2/Vector3 Zero member
« on: May 02, 2015, 11:06:46 pm »
Wondering if there is a reason SFML doesn't have a Zero Vector member (0,0). I understand that the Vector constructors will zero them by default, but there are (and always will be) situations where I don't want to instantiate a new vector.

Same goes for members such as: UnitX, UnitY, UnitX, One. You can imagine what these do.. Unit vectors are used alot in various trig calculations, and One is particularly useful when you want to multiply by a scalar.

template <typename T>
class Vector3
{
...
public:
    static const Vector3<T> Zero;
};
template <typename T> Vector3<T> Vector3<T>::Zero = Vector3<T>(0.0f, 0.0f, 0.0f);

// Avoiding an extra copy
void SetPosition(const Vector2f& v);

SetPosition(Vector2f::Zero);
// vs.
SetPosition(Vector2f());

// Constructing a vector of all 42
Vector2i answerVec = Vector2i::One * 42;

Happy to submit a patch, if people think it's useful.
« Last Edit: May 02, 2015, 11:20:48 pm by Aesthete »
A casual stroll through the lunatic asylum shows that faith does not prove anything.
 - Friedrich Nietzsche

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: Vector2/Vector3 Zero member
« Reply #1 on: May 02, 2015, 11:14:41 pm »
Even if the vector is set to zero at initialization and a zero vector feature wouldn't be an addition as such, I have to admit I faced the same situation which can be unclear about wether or not the vector would be (0,0).

Okay, the documentation probably says it does. But still when you read the code:

mSprite.setPosition(sf::Vector2f::Zero);

it's far clearer about what it does than

mSprite.setPosition(sf::Vector2f()); // have to jump to the doc to know what that does


Edit: it would have been the same about sf::Time if it hadn't sf::Time::Zero. sf::Time::Zero is useful.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Vector2/Vector3 Zero member
« Reply #2 on: May 03, 2015, 08:58:06 am »
sf::Vector has never been a full vector class in the mathematical sense. Otherwise it would have functions such as normalize(), length(), etc. It only defines what's needed to manipulate 2D and 3D coordinates for position etc. So this request falls on the same category as all the other "add math functions to sf::Vector", and is unlikely to be accepted -- at least until we change our mind on this subject ;)

As for your examples: when I set a position, I specify a X coordinate and a Y coordinate. So to me, it is much clearer to write Vector2f(0, 0) and Vector2f(42, 42) than Vector2f::Zero and Vector2f::One * 42. And don't mention performances... at this level I'm not even sure that passing 2 floats by copy is more expensive than a sf::Vector by reference. Really.

sf::Time::Zero exists for another reason. sf::Time has no constructor, you have to use one of the sf::seconds / sf::milliseconds / sf::microseconds functions to create an instance. And 0 shouldn't have to be tied to a time unit (sf::seconds(0), sf::milliseconds(0), ...), it is neutral, so sf::Time::Zero was added.
Laurent Gomila - SFML developer

Aesthete

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Matt Millwood
Re: Vector2/Vector3 Zero member
« Reply #3 on: May 03, 2015, 11:34:45 am »
The use of it definitely sides on convenience, over performance. But yes, I agree this belongs in the same category as why the Vector classes don't have math functionality. I would vote yay on adding mathematical functionality to the Vector classes anyway (Normalize, Normalized, Magnitude, MagnitudeSqr, Dot, Cross), It's not often I am using a Vector class without needing such functionality.

And not that performance is really an issue as you mentioned, but adding a handful of const static vectors available is trivial.

I'm sure many people have gone out and written their own utility functions as I did to deal with the math operations. I'll check the other threads to follow the discussions on such operations in Vector classes, unless you want to explain why it's unlikely to be accepted in future versions?
A casual stroll through the lunatic asylum shows that faith does not prove anything.
 - Friedrich Nietzsche

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Vector2/Vector3 Zero member
« Reply #4 on: May 03, 2015, 11:44:26 am »
I'm sure many people have gone out and written their own utility functions as I did to deal with the math operations.
I've written Thor.Vectors which is used by quite a few people. It's also non-intrusive, everything exists as a global function. In my opinion, it doesn't make sense to add such functionality as member functions.

I'll check the other threads to follow the discussions on such operations in Vector classes
That would be nice, it has been discussed many times already :)
« Last Edit: May 03, 2015, 11:46:50 am by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: