How to get world space position of child node?

Hi.

I’m trying to get the world space position of a child node, but I have no idea how to do that.
I was trying
node.position
node.getPosition()
but both return the local space position of the node relative to the parent node.

I did look in the node.convertToWorldSpace() method too, but I do not understand how is it works.
I tried
parent.node.convertToWorldSpace(child.node.getPosition())
child.node.convertToWorldSpace(parent.node.getPosition())
child.node.convertToWorldSpace(child.node.getPosition())
but I am not getting the value I need.

I was also searching the forum but did not find any solution.
Could anyone please tell me how to get world space position of child node?
I would appreciate any help.

Thanks.

Hi, I think this posting question can help you understand how world space it is, you can see it first.

1 Like

Thanks, but I don’t understand the solution posted in that topic.
Is it really have to be this complicated?

var outM4 = new cc.Mat4();
        this.target.parent.getWorldMatrix(outM4);

        var wpos = new cc.Vec3();
        this.target.position.transformMat4(outM4, wpos);

        const spos: cc.Vec3 = this.camera.getWorldToScreenPoint(wpos);
        const pos2d: cc.Vec3 = this.camera2d.getScreenToWorldPoint(spos)

        this.node.parent.getWorldMatrix(outM4);

        var invertM4 = new cc.Mat4();
        outM4.invert(invertM4);

        pos2d.transformMat4(invertM4, wpos);
        this.node.setPosition(wpos.x, wpos.y);

There is no a single class maybe the canvas or camera class that would have a single method to get this done by feeding the local position to it and then it would “simply” just return the world position?

Thanks.

Well, there is such an API, you try this!
parent.node.convertToWorldSpaceAR(child.node.getPosition())

1 Like

Thanks but for some reason it does not return the expected values. the values are off by 400 pixels.
Someone mentioned in a C++ topic that he was able to solve the problem by calling the convertToWorldSpace() method from the “SpriteContainer” class instead of the parent node, but could not find such class in Cocos Creator and TypeScript docs. Tried to get the Sprite component of parent and call the method from there but I’m getting the same result but at this point I really have no idea what am I doing just shooting blindly.

Thanks anyway, I’ll try to figure something out.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.