cocos 触摸2d屏幕拖动3d节点思路

根据屏幕点击的2d坐标,以及3d摄像机,生成一条带方向的3d射线检测,被射线碰撞的物体,就可以获取到射线碰撞到3d物体上的具体坐标点,然后根据这个坐标点设置被拖动3d节点的位置。

核心代码:

javascript 复制代码
//触摸移动
    onTouchMove(event: EventTouch) {

        //射线检测是否碰到其他方块,并修改显示颜色。
        let touchPos = event.getLocation();
        console.log('触摸移动:', touchPos,event.touch);
        if (!this.dragNode) { //有拖拽节点才进行移动。
            return;
        }
        //拖拽
        let point = new Vec3(1, 1, 0);
        // this.dragNode.setPosition(point);

        let ray = this.mainCamera.screenPointToRay(touchPos.x, touchPos.y);
        // console.log('ray',ray,PhysicsSystem.instance.raycastClosest(ray))
        if (PhysicsSystem.instance.raycastClosest(ray)) {
            const res = PhysicsSystem.instance.raycastClosestResult;
            console.log('移动位置:',res.hitPoint)
            const hitNode = res.collider.node;
            // this.dragNode.setPosition(res.hitPoint.x,res.hitPoint.y,res.hitPoint.z);
            this.dragNode.setPosition(res.hitPoint.x,1,res.hitPoint.z);
            if (hitNode.name.startsWith('tiled')) {
                this.cancelTiled();
                hitNode.getComponent(MeshRenderer).material = this.tiledImgActive;
                this.tiledNodeActive = hitNode;

            } else {
                this.cancelTiled();
            }
        } else {
            this.cancelTiled();
        }
    }

核心属性:res.hitPoint

相关推荐
CHOTEST中图仪器10 小时前
3d光学轮廓仪如何局部测量标准台阶?
3d·光学轮廓仪·三维形貌·微观尺寸
前端_Danny1 天前
使用 ECharts + ECharts-GL 生成 3D 环形图
3d·信息可视化·echarts
学無芷境1 天前
Large-Scale 3D Medical Image Pre-training with Geometric Context Priors
人工智能·3d
暴风鱼划水1 天前
三维重建【4-A】3D Gaussian Splatting:代码解读
python·深度学习·3d·3dgs
老黄编程1 天前
pcl 3DSC特征描述符、对应关系可视化以及ICP配准
3d·pcl·3dsc·icp
猿来是你_L2 天前
UGUI笔记——3D坐标转换成UGUI坐标
笔记·3d
渲吧云渲染3 天前
3D 技术赋能制造企业精准高效装配生产
3d·数字化装配·制造业转型
不会学习的小白O^O3 天前
基于色彩融合和3D功能的自动苹果识别
3d
上海蓝色星球3 天前
基于3DGIS+BIM的智慧园区运维平台价值分享
运维·人工智能·3d
alphacoder1233 天前
【2025最新】AutoCAD Plant3D下载与安装完整教程(附安装包)
其他·3d