Getting the position of a node relative to another

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
Browndog
Posts: 74
Joined: Thu Aug 18, 2005 2:53 am

Getting the position of a node relative to another

Post by Browndog »

I'm trying to get the position of a node relative to the position of another node. How would I go about this?
Heizi
Posts: 30
Joined: Mon Oct 10, 2005 11:23 am
Location: Steinach/Baden

Post by Heizi »

maybe it works like this:
core::vector3df point=node1->getPosition()-node2->getPosition();

what do you mean with relative?
Guest

Post by Guest »

I mean I want to position of the second node as if the first node was at the origin. ie position (0,0,0) and rotation (0,0,0). So what you say will not get what I'm after I need to fator in the rotation of the first node compared with the posistion of the second.

Thanks
Guest

Post by Guest »

Anonymous wrote:I mean I want to position of the second node as if the first node was at the origin. ie position (0,0,0) and rotation (0,0,0). So what you say will not get what I'm after I need to fator in the rotation of the first node compared with the posistion of the second.
Sounds like you could put the second node as a child of the first one? Look at the API, at the get(something)SceneNode() theres a parameter for the parent scene node.
Guest

Post by Guest »

Psuedocode:

Code: Select all

node2.setPosition(positionyouwantittobe+node1->getPosition());
so if node 1 (parent) is at 100, 10, 0, and you want node2 to act as though node1 were the origin, simply position it to where you want plus the position of node1. eg:

Code: Select all

node2.setPosition(vector3df(0, 0, 0)+vector3df(100, 10, 0)); //  = vector3df(100, 10, 0)
in that case, you wanted to move it to the "origin", which you want node1 to act as, and it works perfectly. similar could be done for rotation.
Browndog
Posts: 74
Joined: Thu Aug 18, 2005 2:53 am

Post by Browndog »

ok right. I dont really want to set/change the position of the second node I just want to know what its position is with respect to the first node (as if the first node was at the origin).
Guest

Post by Guest »

Oh. In that case:

Code: Select all

vector3df relPos = node2->getPosition() - node1->getPosition();
result:

node 1 (origin) is at 100, 20, 0, as is node2

node2's position - node1's position: 0 (relative origin)

node1 (origin) is at 100, 20, 0, node2 is at 200, 20, 0

node2's position - node1's position: 200 - 100, 20 - 20, 0 - 0 = 100, 0, 0

should work perfectly.
Klasker
Posts: 230
Joined: Thu May 20, 2004 8:53 am
Contact:

Post by Klasker »

If I understand you correctly, you want to get its position seen from another node's point of view.

Try something like this (haven't tested it, wrote it off the top of my head):

Code: Select all

// Returns nodeB's position seen from nodeA's point of view
inline core::vector3df calcRelativePosition( scene::ISceneNode* nodeA,                                                                   scene::ISceneNode* nodeB )
{
    core::vector3df delta = nodeB->getAbsolutePosition() - nodeA->getAbsolutePosition();
    nodeA->getAbsoluteTransformation().rotateVect( delta );
    return delta;
}
Browndog
Posts: 74
Joined: Thu Aug 18, 2005 2:53 am

Post by Browndog »

Thanks Klasker thats the sort of thing I have been working on but it doesnt seem to work.

I would expect that when the first node is facing directly toward the second node X and Y of delta should = 0 or be very close to 0.
Klasker
Posts: 230
Joined: Thu May 20, 2004 8:53 am
Contact:

Post by Klasker »

Oops, try now -- uses inverseRotateVect instead of rotateVect.

Code: Select all

// Returns nodeB's position seen from nodeA's point of view
inline core::vector3df calcRelativePosition( scene::ISceneNode* nodeA,                                                                   scene::ISceneNode* nodeB )
{
    core::vector3df delta = nodeB->getAbsolutePosition() - nodeA->getAbsolutePosition();
    nodeA->getAbsoluteTransformation().inverseRotateVect( delta );
    return delta;
}
Browndog
Posts: 74
Joined: Thu Aug 18, 2005 2:53 am

Post by Browndog »

Ahh nice one! that seem to work :) thanks so much!!
Post Reply