(Solved)Dumb SetParent() + setPosition() question (+1 Arras)
-
- Posts: 237
- Joined: Mon Jan 16, 2006 1:18 pm
- Location: Odessa,Russian Federation
(Solved)Dumb SetParent() + setPosition() question (+1 Arras)
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???
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.
-
- Posts: 237
- Joined: Mon Jan 16, 2006 1:18 pm
- Location: Odessa,Russian Federation
-
- Posts: 212
- Joined: Sun Jul 19, 2009 4:24 am
- Location: Netherlands Antilles, Curacao
-
- Posts: 212
- Joined: Sun Jul 19, 2009 4:24 am
- Location: Netherlands Antilles, Curacao
-
- Posts: 237
- Joined: Mon Jan 16, 2006 1:18 pm
- Location: Odessa,Russian Federation
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
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
-
- Posts: 237
- Joined: Mon Jan 16, 2006 1:18 pm
- Location: Odessa,Russian Federation
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
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Dumb SetParent() + setPosition() question
you should compute the new relative matrix for the child.
the formula is:
arrow rel_mtx = arrow abs_mtx * inverse(parent abs_mtx)
the formula is:
arrow rel_mtx = arrow abs_mtx * inverse(parent abs_mtx)
-
- Posts: 237
- Joined: Mon Jan 16, 2006 1:18 pm
- Location: Odessa,Russian Federation
-
- Posts: 237
- Joined: Mon Jan 16, 2006 1:18 pm
- Location: Odessa,Russian Federation
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?
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);
-
- Posts: 237
- Joined: Mon Jan 16, 2006 1:18 pm
- Location: Odessa,Russian Federation
CuteAlien, it matters ))
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...
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...
OK, here is complete solution, works with scale and everything:
attach arrow (or anything else):
release arrow:
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.
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);
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());
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.
-
- Posts: 237
- Joined: Mon Jan 16, 2006 1:18 pm
- Location: Odessa,Russian Federation