Hello there!
I have been reworking my collision detection using sfml shapes but I've noticed an issue when getting global
bounding box from non rectangular shapes.
Here is a code sample that reproduces the problem. I commented out the rectangle code because it works, you can uncomment it and comment the triangle shape code to see for yourself the difference.
#include "sfml/graphics.hpp"
int main(void)
{
sf::RenderWindow m_window(sf::VideoMode(640, 480, 32), "Test Convex Shape", sf::Style::Close);
m_window.setVerticalSyncEnabled(true);
sf::Event m_events;
sf::ConvexShape m_shape;
/*m_shape.setPointCount(4);
m_shape.setPoint(0, sf::Vector2f(0, 0));
m_shape.setPoint(1, sf::Vector2f(0, 32));
m_shape.setPoint(2, sf::Vector2f(32, 32));
m_shape.setPoint(3, sf::Vector2f(32, 0));*/
m_shape.setPointCount(3);
m_shape.setPoint(0, sf::Vector2f(0, 0));
m_shape.setPoint(1, sf::Vector2f(0, 32));
m_shape.setPoint(2, sf::Vector2f(32, 32));
m_shape.setFillColor(sf::Color::Transparent);
m_shape.setOutlineColor(sf::Color::Red);
m_shape.setOutlineThickness(1);
sf::RectangleShape m_rectangle;
m_rectangle.setFillColor(sf::Color::Transparent);
m_rectangle.setOutlineColor(sf::Color::Yellow);
m_rectangle.setOutlineThickness(1);
while(m_window.isOpen())
{
while(m_window.pollEvent(m_events))
{
switch(m_events.type)
{
case sf::Event::Closed:
{
m_window.close();
break;
}
default:
break;
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
m_shape.move(0, -10);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
m_shape.move(0, 10);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
m_shape.move(-10, 0);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
m_shape.move(10, 0);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
m_shape.rotate(10);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
m_shape.rotate(-10);
m_rectangle.setPosition(m_shape.getGlobalBounds().left, m_shape.getGlobalBounds().top);
m_rectangle.setSize(sf::Vector2f(m_shape.getGlobalBounds().width, m_shape.getGlobalBounds().height));
m_window.clear();
m_window.draw(m_rectangle);
m_window.draw(m_shape);
m_window.display();
}
return false;
}
The getGlobalBounds() function should return the bounding space in global coords right? So why the extra space that is not part of the shape included?
Help greatly appreciated!