Getting the position of a node relative to another
Getting the position of a node relative to another
I'm trying to get the position of a node relative to the position of another node. How would I go about this?
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.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.
Psuedocode:
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:
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.
Code: Select all
node2.setPosition(positionyouwantittobe+node1->getPosition());
Code: Select all
node2.setPosition(vector3df(0, 0, 0)+vector3df(100, 10, 0)); // = vector3df(100, 10, 0)
Oh. In that case:
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.
Code: Select all
vector3df relPos = node2->getPosition() - node1->getPosition();
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.
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):
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;
}
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;
}