SFML community forums

General => Feature requests => Topic started by: pighead10 on August 20, 2012, 03:07:22 pm

Title: Vector2- divide operator
Post by: pighead10 on August 20, 2012, 03:07:22 pm
I'm sure this has to have been mentioned before, but I have not been able to find anything mentioning it using the forum's search or google.

Why doesn't sf::Vector2 (I've only been using sf::Vector2f but I assume the problem is with the other types as well) have a divide operator? It seems like an obvious feature to have.
Title: Re: Vector2- divide operator
Post by: eXpl0it3r on August 20, 2012, 03:30:44 pm
I'm not quite sure what you're refering to, but sf::Vector<T> has two operators for division: operator/ (http://www.sfml-dev.org/documentation/2.0/classsf_1_1Vector2.php#a64659ab5db944895a1104e01e8309efa) and operator/= (http://www.sfml-dev.org/documentation/2.0/classsf_1_1Vector2.php#a0aebb326092754768f08d959053cd913).
So there you go with a scalar-vector divison.
If you want a Vector2-Vector2 division then you'll first have to teach us from the mathematical perspectiv how two vectors can be devided. In math vector divison is not well defined and the cases that define such a function can vary. So SFML doesn't force its idea of vector division up on the user, instead lets him do the job. ;)
Title: Re: Vector2- divide operator
Post by: pighead10 on August 20, 2012, 11:25:06 pm
Evidently I didn't read the documentation  :)

In that case I must be doing something wrong, as this does not work:

sf::Vector2f v(5,5);
sf::Vector2f z = v/5;

"no operator / matches these commands"
Title: Re: Vector2- divide operator
Post by: eXpl0it3r on August 20, 2012, 11:36:17 pm
I'm not sure if you're familiar with templates but here's the definition:
template<typename T >
Vector2< T > operator/ (const Vector2< T > & left, T right)
So as you can see if you call sf::Vector2f which is a typedef for sf::Vector2<float> you're setting the T from the template abvoe to float so if you'd rewrite that you'd get:
Vector2<float> operator/ (const Vector2<float> & left, float right)

Now you're code is using a sf::Vector2<float> and a precise number, thus the compiler actually sees that you would want to call the non existing function:
Vector2<float> operator/ (const Vector2<float> & left, int right)

This on the other hand will work:
sf::Vector2f v(5.f, 5.f);
sf::Vector2f z = v/5.f;