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

Author Topic: View zoom and mouse coordinate?  (Read 3687 times)

0 Members and 1 Guest are viewing this topic.

Birdron

  • Newbie
  • *
  • Posts: 17
    • View Profile
View zoom and mouse coordinate?
« on: June 12, 2014, 10:08:16 pm »
Hi,
   I have a custom entity class, like in the example, which has a simple bounding box for mouse click detection method -
bool Entity::isPointInside(sf::Vector2i input)
{
        if(input.x > getPosition().x && input.x < getPosition().x+Texture.getSize().x &&
       input.y > getPosition().y && input.y < getPosition().y+Texture.getSize().y)
        {
                return true;
        }

        return false;
}

This is working fine. I am using a View to zoom in and zoom out the viewport. The problem comes here, When the view is (0,0,1,1), everything works fine, but when I zoom out the view, the bounding box of the entity node still remains the same. So, changing the world coordinate to pixel does not work here.
Is there a way to get the position of the Enitity after zoom out, relative to the view rectangle area? So that I can re-update the bounding box which is a shape rectangle or there is any other way to detect the mouse click after zoom out?

Thank you.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: View zoom and mouse coordinate?
« Reply #1 on: June 12, 2014, 10:12:12 pm »
You can use mapPixelToCoords() to convert the mouse's screen position to its "world position".

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: View zoom and mouse coordinate?
« Reply #2 on: June 13, 2014, 12:10:04 am »
If you make your bounding box an sf::FloatRect, you can use its contains method to check if a point is inside instead of your homemade function.
(in addition to mapPixelToCoords like ixrec said, of course :p)

Birdron

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: View zoom and mouse coordinate?
« Reply #3 on: June 13, 2014, 09:09:37 am »
I tried both the method, were not working, that's why I came here to ask.

Thanks for the reply.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: View zoom and mouse coordinate?
« Reply #4 on: June 13, 2014, 07:55:27 pm »
Can you show us how you tried them (ie what code did you use) and exactly how they didn't work?  mapPixelToCoords() is definitely the way you're supposed to do this.

Birdron

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: View zoom and mouse coordinate?
« Reply #5 on: June 15, 2014, 10:07:39 am »
Here is what I tried -
Getting the mouse position -
Code: [Select]
sf::Vector2i PixelPos = sf::Mouse::getPosition(window);
MousePos = window.mapPixelToCoords(PixelPos, view);

Here is the checking -
Code: [Select]
bool Entity::isPointInside(sf::Vector2i input)
{
    if(boundRect.contains(input))
    {
        return true;
    }

    return false;
}

Then I just check the MousePos, it is inside or not.


Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: View zoom and mouse coordinate?
« Reply #6 on: June 15, 2014, 11:36:23 am »
The code you just showed looks correct, so could you provide a complete and minimal example? (ie, one we can actually run and see fail for ourselves)  Also you forgot to tell us how this current code is "not working," so we still can't even guess what the problem might be.

Quote
Then I just check the MousePos, it is inside or not.
What does this mean?  You shouldn't need to do another check after the two you just showed.


P.S. contains() already returns a bool, so isPointInside() should just be
returns boundRect.contains(input);
« Last Edit: June 15, 2014, 11:46:31 am by Ixrec »

Birdron

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: View zoom and mouse coordinate?
« Reply #7 on: June 15, 2014, 05:24:07 pm »
I mean calling the isPointInside() function.
I just print out all the values and found that everything is working fine except this -
Code: [Select]
MousePos = window.mapPixelToCoords(PixelPos, view);It always returns the Dimension of my window( also view), which is 1024x768. Even PixelPos is returning the correct mouse position, relative to my window.

EDIT: and when I set the zoom of view to 2 it returns - 1536x1050.
« Last Edit: June 15, 2014, 05:28:16 pm by Birdron »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: View zoom and mouse coordinate?
« Reply #8 on: June 15, 2014, 05:38:47 pm »
Is that "view" the same view that your window is actually using?  By default mapPixelToCoords will use the window's current view so you shouldn't even have to pass it one explicitly.

Birdron

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: View zoom and mouse coordinate?
« Reply #9 on: June 15, 2014, 05:56:51 pm »
damn me, there was a zoom variable I used to zoom the viewport, which I was using to subdivide the rect of the view. I have so many files that I totally forgot about that variable. Just found it, and everything is working correctly :).

Thank you for your reply.

 

anything