Reputation: 30925
i have confusion here i have this sprites hierarchy
MasterSprite
| -------- SpriteContainer
| ---------------------SpriteNode1
i like to convert the SpriteNode1 position ot world position ( not sure about this ) that is
that the SpriteNode1 position will be as it was if it was MasterSprite child ( is it world position ? ).
i tried:
Vec2 newPos = MasterSprite->convertToNodeSpace(SpriteNode1->getPosition());
Vec2 newPos2 = MasterSprite->convertToWorldSpace(SpriteNode1->getPosition());
Vec2 newPos3 = MasterSprite->convertToNodeSpaceAR(SpriteNode1->getPosition());
Vec2 newPos4 = MasterSprite->convertToWorldSpaceAR(SpriteNode1->getPosition());
but none gave me the right position . what im doing wrong here ?
Upvotes: 0
Views: 2479
Reputation: 12777
The way I understand it right now is that Node-space is local to that node, and World-space is global space (but not necessarily screen-space, because of 3D cameras or whatever).
The so if you want to get a position of one node's position relative to another, you first convert that node's position to world-space, using it's parent, then using the other target parent's convertToNodeSpace.
So if you want the globalized position of a node,
Vec2 node_space_pos = my_node->getPosition(); //this is the node's node-space position
Vec2 world_space_pos = my_node->getParent()->convertToWorldSpace(node_space);
then in the root scene or whatever is relevant to you, you'd take the world-space/universal coords and convert it to the root node's local-space:
Vec2 root_local_pos = root_scene->convertToNodeSpace(world_space_pos);
my_node->removeFromParent();
root_scene->addChild(my_node);
my_node->setPosition(root_local_pos);
Now my_node
is still in the absolute same position on screen, but now it's a child of the root scene instead of wherever else in the graph it was before.
Upvotes: 1
Reputation: 403
In general, you can obtain the WorldSpace position of a node by using
auto pos = node->getParent()->convertToWorldSpace(node->getPosition());
Upvotes: 0