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

Author Topic: Move 2D-Camera with mouse  (Read 3304 times)

0 Members and 2 Guests are viewing this topic.

MOtega

  • Newbie
  • *
  • Posts: 2
    • View Profile
Move 2D-Camera with mouse
« on: April 12, 2015, 02:46:28 pm »
Hello!

I am new here in the forum and a SFML-beginner and wanted to ask how to move the view-camera with the mouse (for example if you press and hold the right mouse button and move it to also move the camera)?

I tried to get the current mouse position with this code:
sf::Vector2i Mouse = sf::Mouse::getPosition(window);
 

But it's an integer-vector and the view.move command for example requires two float-values.

So how is it possible?

Thank you very much!

MOtega

MasterDeveloper

  • Newbie
  • *
  • Posts: 14
  • Forever, is not forever!
    • View Profile
    • Email
Re: Move 2D-Camera with mouse
« Reply #1 on: April 12, 2015, 04:19:22 pm »
try doing something like this:

sf::Vector2f CurrentMousePosition = sf::Mouse::getPosition(YourWindow);
 

or try converting it in 'view.move' static_cast<float>(Variable)

Please, explain it better!!!!
And i will help you

MasterDeveloper
Born to be C++ Programmer!

MOtega

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Move 2D-Camera with mouse
« Reply #2 on: April 12, 2015, 04:24:13 pm »
Thanks for your reply!!

It seems that it works now if I use this code:
...
sf::Vector2i Mouse = sf::Mouse::getPosition(window);
...
//Game-Loop
view1.setCenter(Mouse.x, Mouse.y);
 

Here it seems to work although the setCenter-command requires a float-vector.

MasterDeveloper

  • Newbie
  • *
  • Posts: 14
  • Forever, is not forever!
    • View Profile
    • Email
Re: Move 2D-Camera with mouse
« Reply #3 on: April 12, 2015, 10:16:14 pm »
and also:

view1.setCenter(static_cast<float>(Mouse.x), static_cast<float>(Mouse.y));
but it is complicated....

You're welcome!
Born to be C++ Programmer!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Move 2D-Camera with mouse
« Reply #4 on: April 12, 2015, 10:37:47 pm »
If you want to convert pixel coordinates to world coordinates, don't just cast them, but use sf::RenderWindow::mapPixelToCoords().
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Move 2D-Camera with mouse
« Reply #5 on: April 13, 2015, 09:15:45 am »
SFML / OS X developer