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

Author Topic: Cursor collision on rectangle after View transform  (Read 1390 times)

0 Members and 1 Guest are viewing this topic.

AkidSe

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Cursor collision on rectangle after View transform
« on: March 18, 2017, 12:39:01 pm »
Hello, I got problem with cursor collission with rectangle(for example) after transform sf::View.
Here is code: http://take.ms/oHate
How it works: http://take.ms/A5efL (when mouse collide with rectangle, rectangle's fillColor changes)
And what happens after setRotation(20) to sf::View: http://take.ms/5Wphh
As you see it doesn't work correct.
How to solve this problem?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Cursor collision on rectangle after View transform
« Reply #1 on: March 18, 2017, 01:03:11 pm »
Don't just case the mouse pixel position vector2i to a vector2f. You should map the pixel position to the view co-ordinates system:
window.mapPixelToCoords(sf::Mouse::getPosition(window));
See: http://www.sfml-dev.org/tutorials/2.4/graphics-view.php#coordinates-conversions

Bounding rectangles are always axis-aligned. That is, they cannot be rotated.
Of course, rectangle shapes can. You can "untransform" a rectangle using its inverse transform (or just use its local bounds instead of its global bounds). You can also apply the same inverse transform to the mouse position to see if it is inside the rectangle when converted to the rectangle shape's local co-ordinates.
Something like:
const sf::Vector2f mappedMousePosition = window.mapPixelToCoords(sf::Mouse::getPosition(window));
const sf::Vector2f transformedMousePosition = rectangle.getInverseTransform.transformPoint(mappedMousePosition);
if (rectangle.getLocalBounds.contains(transformedMousePosition))
    mouseIsInsideRectange = true;
Notice the use of local bounds, not global bounds.

I may have missed off the use of the view's inverse transform...
« Last Edit: March 18, 2017, 01:07:27 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*