Converting points

I have

> ---Layer A (my this)
>    ---Layer B
>        --- Child B.A
>             --- Child B.A.a
>    ---Layer C
>        --- Child C.A

I can convert Child C.A position to Layer B by
LayerB->convertToNodeSpace(ChildC_A->getPosition());
But I don’t know how to convert Child C.A to Child B.A.a

I think want you want is

auto worldPos = node->getParent()->convertToWorldSpace(node->getPosition());
auto newPos = newParent->convertToNodeSpace(worldPos);

It’s good to convert the position to world space first in case the parent (Layer C) changes position.

So if you want to know what position what position a child of Child B.A.a needs to be to be at the same place on screen as Child C.A, you would use

auto worldPos = LayerC->convertToWorldSpace(ChildC_A->getPosition());
auto newPos = ChildB_A_a->convertToNodeSpace(worldPos);

Is that what you are after?

I’ll give it a try, give me a min. :smile:

Still not working, I think have to give more details. Actually I’m converting my cursor points

void HelloWorld::onMouseMove(Event *event)
{
	EventMouse* e = (EventMouse*)event;
	float final_Y = winSize.height - abs(e->getCursorY());
	cursor_xy = Vec2(e->getCursorX(), abs(final_Y));

	Vec2 target = spriteLayer->convertToNodeSpace(cursor_xy);
	cursor->setPosition(cursor_xy);

	auto worldPos = playerHUD->convertToWorldSpace(cursor->getPosition());
	auto newPos = camera->convertToNodeSpace(worldPos);
	camera->setPosition(newPos);
}

--this
  --spriteLayer
    --cameraLayer
      --camera
  --playerHUD
    --cursor

--this
  --spriteLayer (would be Layer B)
    --cameraLayer (Child B.A)
      --camera (Child B.A.a)
  --playerHUD (Layer C)
    --cursor (Child C.A)

If you are moving camera, then you need to use camera's parent (cameraLayer) to convert the position to node space. So try changing camera->convertToNodeSpace(worldPos); to cameraLayer->convertToNodeSpace(worldPos);

1 Like

got it! thank you so much @grimfate !