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

Author Topic: Storing a vector2i into a vector2f (or vica versa)?  (Read 7532 times)

0 Members and 1 Guest are viewing this topic.

carton83

  • Newbie
  • *
  • Posts: 38
    • View Profile
Storing a vector2i into a vector2f (or vica versa)?
« on: April 29, 2014, 11:48:23 pm »
Is there a simple way to take the coordinates from vector2i and put it into a vector2f? 


Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Storing a vector2i into a vector2f (or vica versa)?
« Reply #1 on: April 29, 2014, 11:52:57 pm »
Please read the documentation, there is a conversion constructor.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

carton83

  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Storing a vector2i into a vector2f (or vica versa)?
« Reply #2 on: April 30, 2014, 12:11:30 am »
But what if I want to use an already existing vector?

Or, in a more specific instance, what if I want to take the vector2i coordinates given by sf::Mouse::getPosition() and store it in a vector2f?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Storing a vector2i into a vector2f (or vica versa)?
« Reply #3 on: April 30, 2014, 12:22:47 am »
There's always static_cast, but if you have to convert it often indicates a wrong/non-optimal way of doing things. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Storing a vector2i into a vector2f (or vica versa)?
« Reply #4 on: April 30, 2014, 12:32:23 am »
But what if I want to use an already existing vector?
What would that change? Conversion constructors are there to construct from another, existing object...

Or, in a more specific instance, what if I want to take the vector2i coordinates given by sf::Mouse::getPosition() and store it in a vector2f?
You don't.

Use sf::RenderWindow::mapPixelToCoords().
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

carton83

  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Storing a vector2i into a vector2f (or vica versa)?
« Reply #5 on: April 30, 2014, 12:54:08 am »
Well, I'm wanting to use the coordinates from getPosition() to "tell" a sprite where to move.  I'm writing a really simple program to test my vector math knowledge as I'm not entirely sure if I recall everything correctly.  I am pretty certain it's the only instance where I would convert a vector2i to vector2f.  Everything else deals with floats.

Two questions, why CAN'T I just do vector2i = vector2f or vice versa?  And why does getPosition() return a vector2i instead of a vector2f?

What would that change? Conversion constructors are there to construct from another, existing object...

I'm saying both objects already exist.  The constructor would only be useful if I was creating the vector2f right when I'm trying to store the coordinates from getPosition(), which I'm not.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Storing a vector2i into a vector2f (or vica versa)?
« Reply #6 on: April 30, 2014, 01:01:57 am »
Mouse position coordinate are always integer, a mouse is on a pixel, but can't be between 2 pixels.

If you want to "convert" the mouse position to a world position, you might want to use mapPixelToCoords anyway and it returns an sf::Vector2f.

carton83

  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Storing a vector2i into a vector2f (or vica versa)?
« Reply #7 on: April 30, 2014, 01:08:01 am »
Mouse position coordinate are always integer, a mouse is on a pixel, but can't be between 2 pixels.

If you want to "convert" the mouse position to a world position, you might want to use mapPixelToCoords anyway and it returns an sf::Vector2f.

I'm actually using the version of getPosition() that returns position of the mouse in window coordinates.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Storing a vector2i into a vector2f (or vica versa)?
« Reply #8 on: April 30, 2014, 01:10:57 am »
why CAN'T I just do vector2i = vector2f or vice versa?
Because implicit conversions decrease type safety.

And why does getPosition() return a vector2i instead of a vector2f?
The coordinates are in pixels, which are integral.

I'm saying both objects already exist.  The constructor would only be useful if I was creating the vector2f right when I'm trying to store the coordinates from getPosition(), which I'm not.
You can use the assignment operator to assign a newly constructed object... But that's not what you want here, as already stated.

But you have still not mentioned why you don't want to use the correct way (mapPixelToCoords).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

carton83

  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Storing a vector2i into a vector2f (or vica versa)?
« Reply #9 on: April 30, 2014, 05:48:15 am »
Because implicit conversions decrease type safety.

Even though it's not safe there should still be the option.  Just because it's bad doesn't mean it shouldn't be allowed.  One of the awesome things about C++ is that it lets the programmer decide what they want to do, regardless if it's a bad idea.

The coordinates are in pixels, which are integral.

Well, if we had the option to do implicit conversions with vector2 objects then it wouldn't be an issue.

But you have still not mentioned why you don't want to use the correct way (mapPixelToCoords).

I was confusing world coordinates with global coordinates.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Storing a vector2i into a vector2f (or vica versa)?
« Reply #10 on: April 30, 2014, 07:48:04 am »
You do have the option:

vf = static_cast<sf::Vector2f>(vi);
Laurent Gomila - SFML developer

carton83

  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Storing a vector2i into a vector2f (or vica versa)?
« Reply #11 on: May 01, 2014, 08:43:20 pm »
I thought of doing this as its own thread but since it's directly related to this decided against it.

Doing

mouseCord = static_cast<sf::Vector2f>(sf::Mouse::getPosition(window));

Will quickly cause my program to crash and return 0.  By commenting out that single line everything is fine. I tried debugging but I was never able to get any useful information. Edit: I should state that it will run for a short period of time.  It seems to vary.

Here's what I am working on.  As I have said before this is just a simple program to test my memory on vector math.  I realize there are a couple of things I'm not doing right but I'm not worried about that right now.  I just want to get this working then worry about everything else.
« Last Edit: May 01, 2014, 08:46:13 pm by carton83 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Storing a vector2i into a vector2f (or vica versa)?
« Reply #12 on: May 01, 2014, 10:45:20 pm »
Quote
I thought of doing this as its own thread but since it's directly related to this decided against it.
This is not related at all. You have a crash, and although it seems like it's caused by this conversion, I can tell you for sure it is not. Converting two integers to float has never caused any crash.

Since you cannot provide relevant debugging information, there's nothing much we can do to help you. You should really learn how to get useful information from the debugger, needless to say that it will help you a lot in all your future projects ;)

You should also provide a complete and minimal example that reproduces the problem. Nobody is interested in all your code, if the problem is only caused by a small part of it.
Laurent Gomila - SFML developer

carton83

  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Storing a vector2i into a vector2f (or vica versa)?
« Reply #13 on: May 01, 2014, 10:48:38 pm »
I'll start a new thread then.

 

anything