Hello, SFML gurus. :)
I've been kicking around with the ConvexShape class today, and came across something curious. When I make a call to the ConvexShape::getGlobalBounds() method, it returns a FloatRect whose position and/or size seems to be flawed as the shape rotates.
Here's a simple program to demonstrate...
#include <SFML/Graphics.hpp>
using namespace sf;
int main() {
RenderWindow window(VideoMode(800, 600), "Bounding Box Demo");
ConvexShape arrow(4);
arrow.setPosition(400.0f, 300.0f);
arrow.setFillColor(Color::Blue);
arrow.setOutlineColor(Color::Blue);
arrow.setOutlineThickness(1.0f);
arrow.setPoint(0, Vector2f(0, -20));
arrow.setPoint(1, Vector2f(30, 60));
arrow.setPoint(2, Vector2f(0, 40));
arrow.setPoint(3, Vector2f(-30, 60));
Vertex pivotPoint[1];
pivotPoint[0].position.x = 400.0f;
pivotPoint[0].position.y = 300.0f;
pivotPoint[0].color = Color::Yellow;
FloatRect bounds;
RectangleShape boundingBox;
Event event;
while (window.isOpen()) {
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) window.close();
}
window.clear(Color::Black);
arrow.rotate(0.01f);
bounds = arrow.getGlobalBounds();
boundingBox.setPosition(Vector2f(bounds.left, bounds.top));
boundingBox.setSize(Vector2f(bounds.width, bounds.height));
window.draw(boundingBox, RenderStates::Default);
window.draw(arrow, RenderStates::Default);
window.draw(pivotPoint, 1, sf::PrimitiveType::Points, RenderStates::Default);
window.display();
}
return 0;
}
If the arrow is rotated in any of the cardinal compass directions (0, 90, 180, 270 degrees) , the bounding box matches the vertices flawlessly. However, as the arrow rotates, the bounding box grows increasingly inaccurate as it approaches the diagonal directions (45, 135, 225, 315 degrees), leaving a sizable gap around the arrow's tip, while remaining true to the two ends of the arrow's tail...
(https://dl.dropboxusercontent.com/u/63625202/temp/nogaps.gif)
(https://dl.dropboxusercontent.com/u/63625202/temp/gaps.gif)
I've tried running this using SFML 2.1, plus the latest source snapshot downloaded from the downloads page, and the same behavior persists. I'm wondering if there's a problem with the ConvexShape::getGlobalBounds() method, or if perhaps I'm just misunderstanding what it's intended to provide.
Any insight would be greatly appreciated!
- Syn