Cocos Creator world coordinates to screen coordinates

Cocos creator converts a certain coordinate to the screen coordinate (screen position), which used to be converted from the screen coordinate to the world coordinate.
Please upload the code first (just provide a way of thinking, if you have better implementation and suggestions, please leave a message)

    //Scene的设计分辨率是640*1136,适配方案是Fit width,所以下面的缩放按sW来计算的。
    //其他情况没有测试,情况应该差不多。先讲明适用条件,其他条件测试成功的欢迎留言。
    let size = this.panel.node.getContentSize();//panel的坐标转为屏幕坐标
    let width = size.width;
    let height = size.height;
    //屏幕分辨率
    let canvasSize = cc.view.getCanvasSize();
    //scene的设计分辨率
    let desiginSize = cc.view.getDesignResolutionSize();
    //Fit width适配方案,以width作为缩放基准
    let sW = (canvasSize.width / desiginSize.width);
    //计算panel在屏幕中的尺寸
    width = sW * width;
    height = sW * height;
    //转为世界坐标    
    let worldPos =  Wb.Pos.localConvertWorldPointAR(this.panel.node);
    let outPos = cc.v2();
    //设置scene的camera
    this.camera.getWorldToScreenPoint(worldPos, outPos);
    //x,y即屏幕坐标,creator的坐标原点在坐下,Android的在左上,需要做个转换。
    //x轴不变,y轴画布高减去y对应比例的值即为屏幕y坐标。(宽高都以sW为比例计算)
    let x = (outPos.x * sW) - (width / 2);
    let y = canvasSize.height - (outPos.y * sW) - (height / 2);

The above is the code to obtain the screen coordinates. The code is simple and only provides a way of thinking. The content displayed in the test of different models will be correctly overlaid on the panel.

Guess you like

Origin blog.csdn.net/guo0625/article/details/128202715