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

Author Topic: Vector2- divide operator  (Read 4988 times)

0 Members and 1 Guest are viewing this topic.

pighead10

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Hog Pog
Vector2- divide operator
« 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.
Immortui - Zombie puzzle game: http://immortui.hogpog.co.uk

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Vector2- divide operator
« Reply #1 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/ and operator/=.
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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

pighead10

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Hog Pog
Re: Vector2- divide operator
« Reply #2 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"
Immortui - Zombie puzzle game: http://immortui.hogpog.co.uk

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Vector2- divide operator
« Reply #3 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;
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything