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

Author Topic: New coordinates based on view  (Read 3180 times)

0 Members and 1 Guest are viewing this topic.

AdventWolf

  • Jr. Member
  • **
  • Posts: 54
    • View Profile
New coordinates based on view
« on: September 02, 2013, 04:18:39 am »
So the problem is that I am trying to click on the window to place an object on the screen. It works perfectly until I add views.

When I move the view to the right 100 pixels and click on the position of (300, 300), then the object is moved to the new view coordinates of (200, 300). I need the object to be drawn to the new view coordinates of (300, 300). I have experimented with the mapCoordsToPixel function but I can't get it working.

Here is the sample code:
Code: [Select]
#include <SFML/Graphics.hpp>

int main()
{
sf::RenderWindow window(sf::VideoMode(600, 400), "View");
window.setFramerateLimit(60);

sf::CircleShape circle;
circle.setFillColor(sf::Color::Black);
circle.setRadius(15);

sf::View view = window.getView();

while(window.isOpen())
{
sf::Event event;

while(window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
window.close();
}

if( event.type == sf::Event::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Left )
{
circle.setPosition(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y);
}

if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::A))
view.move(-5.f, 0.f);

if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::D))
view.move(5.f, 0.f);

if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::W))
view.move(0.f, -5.f);

if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::S))
view.move(0.f, 5.f);

window.clear(sf::Color::White);
window.setView(view);
window.draw(circle);
window.display();
}

return 0;
}


Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: New coordinates based on view
« Reply #1 on: September 02, 2013, 04:25:33 am »
I think you might want mapPixelToCoords instead of mapCoordsToPixel.  The documentation says
Quote
For render-windows, this function is typically used to find which point (or object) is located below the mouse cursor.
in the entry for mapPixelToCoords but not the entry for mapCoordsToPixel.

By the way,
circle.setPosition(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y);
is needlessly verbose because like many other SFML functions you can just give it a Vector2X directly:
circle.setPosition(sf::Mouse::getPosition(window));

So the correct line is probably:
circle.setPosition(window.mapPixelToCoords(Mouse::getPosition(window)));
« Last Edit: September 02, 2013, 04:30:15 am by Ixrec »

AdventWolf

  • Jr. Member
  • **
  • Posts: 54
    • View Profile
Re: New coordinates based on view
« Reply #2 on: September 02, 2013, 04:48:11 am »
Awesome that worked perfectly for the sample but for my actual program I am still getting the same results. Bleh I hate programming lol.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: New coordinates based on view
« Reply #3 on: September 02, 2013, 05:19:56 am »
Just take a look at the doc. ;)
http://www.sfml-dev.org/documentation/2.1/classsf_1_1RenderTarget.php#a46eb08f775dd1420d6207ea87dde6e54

mapPixelToCoords converts the window coordinates of your point to its world coordinates in the current view. If the view where you want to draw your circle isn't the current one at the moment, pass it as an argument to the function.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: New coordinates based on view
« Reply #4 on: September 02, 2013, 05:31:58 am »
If you can't figure it out by yourself you can always paste your actual code.

AdventWolf

  • Jr. Member
  • **
  • Posts: 54
    • View Profile
Re: New coordinates based on view
« Reply #5 on: September 02, 2013, 09:21:59 am »
After tweaking a few things, the issue finally worked itself out so thanks for the help!