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

Author Topic: [SOLVED] SFML sf::Transform combined transforms  (Read 3064 times)

0 Members and 1 Guest are viewing this topic.

DasOhmoff

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
[SOLVED] SFML sf::Transform combined transforms
« on: June 21, 2015, 08:50:46 pm »
Hello,

i am trying to understand the meaning of

" Overload of binary operator * to combine two transforms. This call is equivalent to calling Transform(left).combine(right). "

but I can't understand this. I am reading the book "SFML Game Development" and I reached this part where I have to wirte this:

HPP FILE:
class SceneNode : public sf::Transformable, public sf::Drawable, private sf::NonCopyable
{
    ...

    private:
        sf::Transform getWorldTransform() const;

        SceneNode *mParent;
       
    ...
};


CPP FILE:
sf::Transform SceneNode::getWorldTransform() const
{
    sf::Transform transform = sf::Transform::Identity;

    for (const SceneNode* node = this; node != nullptr; node = node->mParent)
        transform = node->getTransform() * transform;

    return transform;
}

If a SceneNode is a child, it means it has a parent, we store it in mParent, we have a function which attaches children.
I understand what we are trying to do here, but I don't understand why this for loop should give us the complete transform of the world. We are multiplying our transform with the one our parent has, and if our parent has a parent too, we are multiplying transform with it either and so on. But why should this give us the complete world transform?

For example our position is (50, 50), position of our parent is (10, 10).
So transform * child-transform is (500, 500). Is this our world transform???

I don't get it, I'm sorry, I'm to stupid for that. What is the purpose of '*' operator?

« Last Edit: June 23, 2015, 08:41:45 am by DasOhmoff »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML sf::Transform combined transforms
« Reply #1 on: June 21, 2015, 09:14:19 pm »
Quote
For example our position is (50, 50), position of our parent is (10, 10).
So transform * child-transform is (500, 500).
No. A transform is a matrix, so multiplying two transforms is equivalent to multiplying two matrices, which has nothing to do with multiplying two numbers.

Anyway, you don't have to know the underlying math involved here, just take what the documentation says: multiplying two transforms combines them. In other words, applying transform A * B is equivalent to applying transform A followed by transform B.

To come back to your example, it would be equivalent to a translation of (50, 50) followed by a translation of (10, 10), which is a combined translation of (60, 60).
Laurent Gomila - SFML developer

DasOhmoff

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: SFML sf::Transform combined transforms
« Reply #2 on: June 22, 2015, 05:12:04 pm »
Quote
For example our position is (50, 50), position of our parent is (10, 10).
So transform * child-transform is (500, 500).
No. A transform is a matrix, so multiplying two transforms is equivalent to multiplying two matrices, which has nothing to do with multiplying two numbers.

Anyway, you don't have to know the underlying math involved here, just take what the documentation says: multiplying two transforms combines them. In other words, applying transform A * B is equivalent to applying transform A followed by transform B.

To come back to your example, it would be equivalent to a translation of (50, 50) followed by a translation of (10, 10), which is a combined translation of (60, 60).

Ok, I understand this now, thanks.
One question, perhaps you need more information of the code to know this, but why do we want to combine the transformation of our parents?

I mean if we have 10 parents and they have the position (20, 20) the result is (200, 200), so what's the point of that function?

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: SFML sf::Transform combined transforms
« Reply #3 on: June 22, 2015, 05:24:47 pm »
Unless you're counting parents of parents, each child should only really have a single parent. *rereads first post. *realises mParent can only reference a single parent *realises you probably are talking about parents of parents *facepalms

The point here I think is more clear if you think of it in one dimension.
If one object has an absolute position of 10. It's child has a relative position of 20 and it's child has a relative position of 5. The absolute position of the child's child is not 25 (the result of just combining the child's transform and it's parent's), but is 35, as each offset is combined with all of the previous. I may be completely wrong but you did say we may need more code.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML sf::Transform combined transforms
« Reply #4 on: June 22, 2015, 07:43:37 pm »
A node's transform is always relative to its parent. And the parent node is relative to its own parent. And so on until you reach the root node. So you have to combine the whole chain of transforms up to the root to get the final world transform of a node. Combining the parent's transform only would yield a transform relative to the node's grand-parent.
Laurent Gomila - SFML developer

DasOhmoff

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: SFML sf::Transform combined transforms
« Reply #5 on: June 23, 2015, 08:41:11 am »
Unless you're counting parents of parents, each child should only really have a single parent. *rereads first post. *realises mParent can only reference a single parent *realises you probably are talking about parents of parents *facepalms

The point here I think is more clear if you think of it in one dimension.
If one object has an absolute position of 10. It's child has a relative position of 20 and it's child has a relative position of 5. The absolute position of the child's child is not 25 (the result of just combining the child's transform and it's parent's), but is 35, as each offset is combined with all of the previous. I may be completely wrong but you did say we may need more code.

A node's transform is always relative to its parent. And the parent node is relative to its own parent. And so on until you reach the root node. So you have to combine the whole chain of transforms up to the root to get the final world transform of a node. Combining the parent's transform only would yield a transform relative to the node's grand-parent.

Ahhhhh of course >< !!

Sry was a stupid question, I realised this now.
Thank you very much! :)

 

anything