SFML community forums

General => Feature requests => Topic started by: Mr_Blame on June 25, 2015, 01:57:32 pm

Title: Vector converting
Post by: Mr_Blame 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.
Title: Re: Vector converting
Post by: eXpl0it3r 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);
Title: Re: Vector converting
Post by: Mr_Blame 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);}
};
 
Title: Re: Vector converting
Post by: eXpl0it3r 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.
Title: Re: Vector converting
Post by: Mr_Blame on June 25, 2015, 05:55:02 pm
Ok