(Solved)Dumb SetParent() + setPosition() question (+1 Arras)

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
Bear_130278
Posts: 237
Joined: Mon Jan 16, 2006 1:18 pm
Location: Odessa,Russian Federation

(Solved)Dumb SetParent() + setPosition() question (+1 Arras)

Post by Bear_130278 »

Haalo *)
I know that now i'm asking a very lazy question....
Here what i want...
We have an arrow.. it flyes.... and when it hits the animated stuff i want it to become a child of those.
So upon hit i call setParent() for the arrow...
But at that moment, the position of the arrow node changes from world(there was no parent) to parent-relative position...

The question is... what is the fast conversion between the absolute position, and relative position??
It should be something like
Arrow new pos=Arrow abs pos - Parent abs pos... yes???
Last edited by Bear_130278 on Tue Mar 30, 2010 9:13 am, edited 1 time in total.
Do you like VODKA???
Image
Image
Bear_130278
Posts: 237
Joined: Mon Jan 16, 2006 1:18 pm
Location: Odessa,Russian Federation

Post by Bear_130278 »

Guys 8))
Do you like VODKA???
Image
Image
Lil Margin
Posts: 212
Joined: Sun Jul 19, 2009 4:24 am
Location: Netherlands Antilles, Curacao

Post by Lil Margin »

i dont know dude but i think when it the arrow hit the animated stuff you can change the absolute position and then make it a parent of the animated stuff...
Lil Margin
Posts: 212
Joined: Sun Jul 19, 2009 4:24 am
Location: Netherlands Antilles, Curacao

Post by Lil Margin »

sorry for double post...never happend to me before :?
Bear_130278
Posts: 237
Joined: Mon Jan 16, 2006 1:18 pm
Location: Odessa,Russian Federation

Post by Bear_130278 »

Up 8)))
Still not good with this 8)))
Do you like VODKA???
Image
Image
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

I think it would be:

Code: Select all

oldPos = arrow->getAbsolutePosition();
arrow->setParent(animatedStuff);
arrow->setPosition( oldPos - animatedStuff->getAbsolutePosition() );
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Bear_130278
Posts: 237
Joined: Mon Jan 16, 2006 1:18 pm
Location: Odessa,Russian Federation

Post by Bear_130278 »

And if the Shield(animated stuff) is a child itself, and has a rotation??
Do you like VODKA???
Image
Image
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

For the position it should not matter. For the rotation - what do you want it to be afterwards. The rotation of the new parent, the rotation of the old child or the added rotation of both?
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
anchor
Posts: 9
Joined: Thu Jun 25, 2009 2:57 pm
Location: Hungary

Dumb SetParent() + setPosition() question

Post by anchor »

you should compute the new relative matrix for the child.
the formula is:

arrow rel_mtx = arrow abs_mtx * inverse(parent abs_mtx)
Bear_130278
Posts: 237
Joined: Mon Jan 16, 2006 1:18 pm
Location: Odessa,Russian Federation

Post by Bear_130278 »

10x anchor.... i was thinking in this way... ok i'll try it out and post results...
Do you like VODKA???
Image
Image
Bear_130278
Posts: 237
Joined: Mon Jan 16, 2006 1:18 pm
Location: Odessa,Russian Federation

Post by Bear_130278 »

Ok, i'm done computing.... but...
How can i set this new matrix to the arrow??
With setPosition+setRotation??

Look guys.... what am i doing wrong?

Code: Select all

						matrix4 AbsParentMat=Units[u_Host->targetInd].shield->getAbsoluteTransformation();
						matrix4 AbsArrowMat=projNode->getAbsoluteTransformation();
						matrix4 ParrentMatInv;AbsParentMat.getInverse(ParrentMatInv);
						matrix4 RelativeArrowMat=AbsArrowMat*ParrentMatInv;
						vector3df npos=RelativeArrowMat.getTranslation();
						vector3df nrot=RelativeArrowMat.getRotationDegrees();
						projNode->setParent(Units[u_Host->targetInd].shield);
						projNode->setRotation(nrot);
						projNode->setPosition(npos);
Do you like VODKA???
Image
Image
Bear_130278
Posts: 237
Joined: Mon Jan 16, 2006 1:18 pm
Location: Odessa,Russian Federation

Post by Bear_130278 »

CuteAlien, it matters 8)))
Look....
i can get the new_pos = Arrow_abs_Pos-Parent_abs_pos
BUT!!! if the new parent got a rotation around a global axis? His local axises will not be the same as global ones....
This subtracted position, will not be correct for the new parents space... you see??
The closer to the center of the shield arrow hits, the less this effect is visible...
Do you like VODKA???
Image
Image
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

OK, here is complete solution, works with scale and everything:

attach arrow (or anything else):

Code: Select all

            core::matrix4 m,n;

            shield->updateAbsolutePosition();
            shield->getAbsoluteTransformation().getInverse(m);

            arrow->updateAbsolutePosition();
            n = arrow->getAbsoluteTransformation();

            m *= n;

            arrow->setPosition(m.getTranslation());
            arrow->setRotation(m.getRotationDegrees());
            arrow->setScale(m.getScale());

            arrow->setParent(shield);
release arrow:

Code: Select all

            arrow->updateAbsolutePosition();
            core::matrix4 m = arrow->getAbsoluteTransformation();

            arrow->setPosition(m.getTranslation());
            arrow->setRotation(m.getRotationDegrees());
            arrow->setScale(m.getScale());

            arrow->setParent(smgr->getRootSceneNode());
And here is small example project I set up (instructions inside main.cpp): arrow2.zip

EDIT: short description of what it does:
Basic idea is that you need to find relative position, rotation and scale of arrow against shield. Or in other words, position ,rotation and scale of arrow in local coordinates of shield. To get it you need to substract absolute values of position, rotation and scale of shield from arrow. Position is easy, rotation is tricky as we are in wonder world of professor Euler there :)

This is in principle opposite process to one which will be applied to arrow once its child of shield (or better sad the same proces but with opposite values applied). This process is made by multiplication of parent (shield) absolute transformation matrix with relative transformation matrix of child(arrow). You can look at source code of ISceneNode if you are interested.

To reverse process we simply take inverse absolute transformation matrix of shield (to be parent) and multiply it by absolute transformation matrix of arrow (to be child soon). Absolute because arrow might be already attached to some other object (in fact it always is attached at last to root scene node). If it is not attached, we can use relative transformation but in that case relative and absolute are the same so better to always use absolute one.

It is basically what anchor have already suggested as i see.
Bear_130278
Posts: 237
Joined: Mon Jan 16, 2006 1:18 pm
Location: Odessa,Russian Federation

Post by Bear_130278 »

10x a lot Arras once again.
It works perfectly ^_^
Do you like VODKA???
Image
Image
Post Reply