Hello, I do not understand how to correctly use the
sf::Transformable and
sf::Drawable classes. My
Car class inherits from them, and it overrides the
draw() function. The car has a
sf::Sprite member data.
The problem is that the sprite's global bounding box is not axis-aligned; when the car is rotated, so too is the sprite's bounding box.
The questions I have are:
- What member data should a class that inherits from sf::Transformable and sf::Drawable contain? Sprites, rectangles, etc.?
- Is my Car class supposed to implement the getLocalBounds() and getGlobalBounds() member functions?
Following the tutorial steps I now have the equivalent of this simplified code:
//
// Car.hpp
//
class Car : public sf::Transformable, public sf::Drawable
{
private:
sf::Texture texture;
sf::Sprite sprite;
public:
Car()
{
// load texture, and
// load sprite using texture
}
private:
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
{
states.transform.combine(getTransform());
target.draw(sprite, states);
// and draw sprite.getGlobalBounds() rectangle
}
};
//
// main.cpp
//
int main()
{
// typical window code from tutorials
Car car;
car.rotate(45);
// typical event and looping code from tutorials
{
render_window.draw(car); // <-- BUG: the bounding box is at 45 degrees!
}
}