Page 1 of 1

Getting the position of a node relative to another

Posted: Mon Mar 20, 2006 6:44 am
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?

Posted: Mon Mar 20, 2006 8:40 am
by Heizi
maybe it works like this:
core::vector3df point=node1->getPosition()-node2->getPosition();

what do you mean with relative?

Posted: Mon Mar 20, 2006 8:49 am
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

Posted: Mon Mar 20, 2006 3:27 pm
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.

Posted: Mon Mar 20, 2006 3:44 pm
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.

Posted: Tue Mar 21, 2006 4:33 am
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).

Posted: Tue Mar 21, 2006 2:48 pm
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.

Posted: Tue Mar 21, 2006 3:57 pm
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;
}

Posted: Wed Mar 22, 2006 3:41 am
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.

Posted: Wed Mar 22, 2006 7:49 am
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;
}

Posted: Wed Mar 22, 2006 8:39 am
by Browndog
Ahh nice one! that seem to work :) thanks so much!!