Search code examples
hierarchygame-physicssfml

Object hierarchy in SFML 2.3


I'm trying to program a car with 4 wheels and I want them to inherit the transform of the car when drawing. I'm aware of the very small tutorial at SFML transform but I don't exactly get it. I decided to come here early into the project so I don't have to rewrite most of it (saw it happen to other)

At the moment I only have this :

class Car : public Drawable, public Transformable
{
public:
    Car(Vector2f dimensions):
        m_dimensions(dimensions),
        m_body(m_dimensions),
        m_wheels(4)
    {}

private:

    virtual void draw(RenderTarget& target, RenderStates states) const
    {
        states.transform *= getTransform();
        states.texture = NULL;
        target.draw(m_body, states);
    }
    struct Wheel :
    {

    };
    vector<Wheel> m_wheels;
    Vector2f m_dimensions;
    RectangleShape m_body;

};


int main()
{
    RenderWindow window(VideoMode(800, 600), "SFML window");
    window.setVerticalSyncEnabled(true);
    Car car(Vector2f(300.f, 180.f));
    car.setOrigin(20,20);
    car.setPosition(400, 300);
    car.rotate(45);

    while(window.isOpen())
    {
        Event event;
        while(window.pollEvent(event))
        {
            if(event.type == Event::Closed)
                window.close();
        }
        car.rotate(1);
        window.clear();
        window.draw(car);
        window.display();
    }
}

I just need the wheels to be printed next to the car ( I'll figure out the position and what now) but right now I just want to know if I'm heading the right way or you know of any better implementation.


Solution

  • You are doing things correctly. I hope the following will help clear up what is going on in the code you have posted above:

    The sf::Transformable class is used as a wrapper for data (a matrix) that can represent transformations that are commonly used in graphics programming, such as rotation, scaling, and translation. By inheriting the sf::Transformable class you add all the transformation functions to the class, and are also supplied with a function that gives you the data - sf::Transformable::getTransform(). You want to move your car around, and this supplies you with all the tools you need to keep track of its movement.

    Moving your car would be pointless if it didn't change position on the screen, so you must inform the graphics card of the movement as the car is drawn. When the line states.transform *= getTransform(); executes you are taking the transform passed in on the states argument and adding the car's transform (through matrix multiplication) to ensure that it is drawn with all the changes you've made using sf::Transformable's functions.

    Since you are making a compound object (car), you will have to consider the placement of the individual objects (body, wheels). Note what happens if the individual objects also inherit sf::Transformable and sf::Drawable and implement them as you have above: they add their transform to the car's transform (passed in through the states argument) before drawing to the screen, so you only have to maintain their position relative to the car itself!