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

Author Topic: Vector converting  (Read 10364 times)

0 Members and 1 Guest are viewing this topic.

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Vector converting
« on: June 25, 2015, 01:57:32 pm »
When developing my SFML app I found one thing uncomfortable: sf::Vector2<type> is not converting. Example: I have information about mouse position in sf::Vector2i and than I want to use it in some king of function, but some of them require another vector(e.g. sf::vector2f) cause of I can't convert sf::vector2i to sf::vector2f I need to do this:
sf::vector2i mPos = window.mousePosition(window);
sprite.move(mPos.x, mPos.y);
//or
sprite.move(sf::vector2f(mPos.x, mPos.y));
 
Some of functions also don't take arguments like (float, float): only (sf::vector2f), that causes vector constructor usage.
And these were only too simple code examples in some situations the code, that I wrote was unreadable cause of big amount of brackets and words.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: Vector converting
« Reply #1 on: June 25, 2015, 02:12:36 pm »
If you had used the search function you'd have found many similar requests with the same repeating answer. This is by design. ;)

Most of the time if you need to convert between integer coords and float coords you actually want to use some form of transformation anyways, e.g. between window coords and world coords you should always use window.mapPixelToCoords or for the reverse direction window.mapCoordsToPixel.

If you ever really need to directly convert, you can still call the constructor explicitly:
vec = sf::Vector2f(vec2i);
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: Vector converting
« Reply #2 on: June 25, 2015, 02:14:34 pm »
Sorry. I just thought that overloading operator for converting variables will be good idea. :)
Class sf::Vector2f : public vector2<float>{
       Private:
...
       Public:
Sf::vector2i operatorsf::vector2i(){return sf::vector2i(x,y);}
};
 
« Last Edit: June 25, 2015, 02:27:26 pm by Mr_Blame »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: Vector converting
« Reply #3 on: June 25, 2015, 03:11:39 pm »
No idea what that piece of code should be, but anyways as I said, implicit conversion will lead in most cases to a wrong usage and hidden errors. Transformation functions are usually used and if you really need it, you can call the constructor explicit.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: Vector converting
« Reply #4 on: June 25, 2015, 05:55:02 pm »
Ok