convertToNodeSpace convertToWorldSpace confusion

convertToNodeSpace convertToWorldSpace confusion
0.0 0

#1

I am having trouble understanding the how convertToNodeSpace and convertToWorldSpace work.

for example I have Object-C within Object-B within Object-A

Obj A

  |---- Obj B

            |---- Obj C

How do I get Object-C position in Object-A space?


#2

objectA.convertToNodeSpace(objectC.convertToWorldSpace(nodePoint))

   /**
     * !#en Converts a Point to node (local) space coordinates. The result is in Vec2.
     * !#zh 将一个点转换到节点 (局部) 坐标系。结果以 Vec2 为单位。
     * @method convertToNodeSpace
     * @param {Vec2} worldPoint
     * @return {Vec2}
     * @example
     * var newVec2 = node.convertToNodeSpace(cc.v2(100, 100));
     */
    convertToNodeSpace (worldPoint) {
        if (cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
            // ensure transform computed
            cc.director._visitScene();
        }
        var nodePositionIgnoreAnchorPoint = this._sgNode.convertToNodeSpace(worldPoint);
        return cc.pAdd(nodePositionIgnoreAnchorPoint, cc.p(this._anchorPoint.x * this._contentSize.width, this._anchorPoint.y * this._contentSize.height));
    },

    /**
     * !#en Converts a Point to world space coordinates. The result is in Points.
     * !#zh 将一个点转换到世界空间坐标系。结果以 Vec2 为单位。
     * @method convertToWorldSpace
     * @param {Vec2} nodePoint
     * @return {Vec2}
     * @example
     * var newVec2 = node.convertToWorldSpace(cc.v2(100, 100));
     */
    convertToWorldSpace (nodePoint) {
        if (cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
            // ensure transform computed
            cc.director._visitScene();
        }
        var x = nodePoint.x - this._anchorPoint.x * this._contentSize.width;
        var y = nodePoint.y - this._anchorPoint.y * this._contentSize.height;
        return cc.v2(this._sgNode.convertToWorldSpace(cc.v2(x, y)));
    },

#3

I use like this and it works well in my project.

function getPostionInOtherNode(spaceNode, targetNode) {
  if (targetNode.parent == null) {
    return null;
  }
  let pos = targetNode.parent.convertToWorldSpaceAR(targetNode.getPosition());
  return spaceNode.convertToNodeSpaceAR(pos);
}

You can use like this.
let objC_postion_in_objA = getPostionInOtherNode(Object_A, Object_C);