Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Positioning a sprite after another sprites rotation?  (Read 2144 times)

0 Members and 1 Guest are viewing this topic.

Haikarainen

  • Guest
Positioning a sprite after another sprites rotation?
« on: June 15, 2012, 04:03:17 am »
In another project of mine, utilizing only opengl and my own code, I have "sprites" in a sense of nodes and entities(kindof like ogre3ds nodesystem, except it's 2d). In this system every node can have both childrennodes and entities(entities being drawn stuff, like a texture, nodes being positions etc).

As an example of what I want to achieve with SFML; in this system I can have 1 rotating node, with a box drawn to it, and then a subnode with an offset(below the parentnode for example) with another box drawn to it. When I draw the nodes, I draw them from a top-to-bottom-hierarchical way, wich includes all the nodes transforms. So when the parentnode is rotating, the childnode is rotating with it(as well as positioning itself with its offset correctly). Kind of looks like this:

NodeSystem->Draw() // <- draws all the nodes

        ParentNode->ApplyRotation() // <- calls glRotatef(0,0,1,rotation)
        ParentNode->Draw()
        ParentNode->DrawChildren()
                ChildNode->ApplyRotation() // This isnt rotated,  but it parentnodes rotationtransofrmation is still in effect so it will be
                ChildNode->Draw();
                ChildNode->DrawChildren();
                        //  No children to be drawn
                ChildNode->DeleteRotation()
        ParentNode->DeleteRotation() // calls glRotatef(0,0,-1,rotation);

This way it has made it really easy for me to attach stuff logically and just have them work with eachother automatically, and not care so much about the math behind it. Now I want to do this in SFML, I have a rocket-tower with attached rockets, but cant rotate the tower with the rockets properly using only sprites, I guess it is doable with some cos/sin/tan-magic but I'm not the brightest nut in the toolbox regarding this. Is there any easier and perhaps more automated way?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Positioning a sprite after another sprites rotation?
« Reply #1 on: June 15, 2012, 08:08:20 am »
You should use sf::Transform (SFML 2).
Laurent Gomila - SFML developer

Haikarainen

  • Guest
Re: Positioning a sprite after another sprites rotation?
« Reply #2 on: June 15, 2012, 03:43:45 pm »
You should use sf::Transform (SFML 2).

Could  you give me an example to how I could use this? Does it transform the whole "view"?  Sorry my math skills are undubitabely dumb.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Positioning a sprite after another sprites rotation?
« Reply #3 on: June 15, 2012, 05:00:47 pm »
For a parent-child hierarchy, you can do this:

class Node : public sf::Drawable
{
public:

    // functions to transform the node

private:

    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
    {
        states.transform *= m_transform;

        // draw self using states...

        // draw children
        foreach (Node* child: m_children)
            target.draw(child, states);
    }

    std::vector<Node*> m_children;
    sf::Transform m_transform;
}

If you want to have a SFML-like interface for transformations, you can inherit from sf::Transformable.
Laurent Gomila - SFML developer