1
System / Re: Nested transforms for sf::sprite and mouse event collision
« on: August 30, 2021, 04:42:57 am »
I eventually figured this out after a few days away from this part of the project.
In the original problem we didn't apply the transform like we did by with draw:
It would be somewhat awkward to pass "sf::RenderStates" as a extra parameter to a simple contains() function so we didn't want to necessarily copy how draw works but keep the same premise. First make a bounding box with the sprites global bounds like we originally expected. Then simply apply the transform of the wrapper class to the bounding box and check for this modified box.
The end result is I can now use the default setPosition function from sf::Transform on this Button class and it accurately handles collision and drawing without direct manipulation of the sprite the Button contains.
To my understanding the tutorial doesn't really help for this particular situation so it may be useful to add?
In the original problem we didn't apply the transform like we did by with draw:
mSprite.getGlobalBounds().contains(x,y); // only works if we change the sprites position directly
// defeats the point of using a wrapper class
// defeats the point of using a wrapper class
It would be somewhat awkward to pass "sf::RenderStates" as a extra parameter to a simple contains() function so we didn't want to necessarily copy how draw works but keep the same premise. First make a bounding box with the sprites global bounds like we originally expected. Then simply apply the transform of the wrapper class to the bounding box and check for this modified box.
bool Button::contains(int x, int y)
{
sf::FloatRect boundingBox = mSprite.getGlobalBounds();
boundingBox = getTransform().transformRect(boundingBox);
return boundingBox.contains(x,y);
}
{
sf::FloatRect boundingBox = mSprite.getGlobalBounds();
boundingBox = getTransform().transformRect(boundingBox);
return boundingBox.contains(x,y);
}
The end result is I can now use the default setPosition function from sf::Transform on this Button class and it accurately handles collision and drawing without direct manipulation of the sprite the Button contains.
To my understanding the tutorial doesn't really help for this particular situation so it may be useful to add?