set an absolute position for a child

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Xaron
Posts: 310
Joined: Sun Oct 16, 2005 7:39 am
Location: Germany
Contact:

set an absolute position for a child

Post by Xaron »

Hi all,

I have some kind of stupid problem. I'd like to set an absolute position of a child where the parent has been rotated.

Example:
My parent is at 0,0,10.
My child shall be at 5,0,10 (absolute)

So for setting an absolute position I do the following (where absPosition is the wanted absolute position vector):

Code: Select all

ISceneNode* parent = node->getParent();
if( parent )
{
  parent->updateAbsolutePosition();

  matrix4 m = parent->getAbsoluteTransformation();
  matrix4 n = node->getAbsoluteTransformation();

  vector3df t = m.getTranslation();
  vector3df p = absPosition - t;
  node->setPosition( p );
  node->setRotation( n.getRotationDegrees() );
  node->setScale( n.getScale() );
}
This works as long the parent has no rotation. If I rotate the parent and AFTER that I want to set an absolute position for the child the rotation from the parent is taken into account that way it's moved along the new parent's rotated axis.

Here come two screenshots to show what I mean:
1)
Cube 1 is created.
Cube 2 is created with cube 1 as parent
Cube 1 is rotated.
set absolute position for cube 1 at 0,0,10 (via setPosition)
set absolute position for cube 2 at 5,0,10 (function above)

Now I expect to see it like this (both cubes are rotated as cube 2 is the child of cube 1):
Image

But actually it looks like this (it looks like the setPosition function from above just uses the new rotation values from cube 1 for positioning instead of setting it to the wanted absolute world coordinates of 5,0,10):
Image

THANKS!
cobra
Posts: 371
Joined: Fri Jan 23, 2009 2:56 am
Location: United States
Contact:

Post by cobra »

You need to get the transformation you wish to position your child at, then get the inverse transformation of the parent and multiply them in the order (parentTrans*desiredTrans).

I recommend reading some things about matrix transformations.

Below is functional code.

Code: Select all

if(node->getParent() != 0)
    {
        if(node->getParent() != node->getSceneManager()->getRootSceneNode())
        {
            irr::core::matrix4 matr = desiredTransformation;

            const core::matrix4 w2n(node->getParent()->getAbsoluteTransformation(), core::matrix4::EM4CONST_INVERSE);

            matr = (w2n*matr);

            node->setPosition(matr.getTranslation());
            node->setRotation(matr.getRotationDegrees());
            node->updateAbsolutePosition();
        }
    }
Josiah Hartzell
Image
Xaron
Posts: 310
Joined: Sun Oct 16, 2005 7:39 am
Location: Germany
Contact:

Post by Xaron »

Thank you! That did work. :)

I have another problem with getAbsoluteTransformation. Should this return the absolute transformation including all parents?

So if I have 2 cubes:
Cube 1 (no parent)
Cube 2 (parent is Cube 1).

If I know rotate Cube 1 and call getAbsoluteTransformation for Cube 2, shouldn't the rotation values of the matrix show the rotation for Cube 1 as well?
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

Yes, like absolute coordinates are relative to 0,0,0, an absolute transformation is relative to the identity matrix; it describes a position, rotation and scale in world space.
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
Post Reply