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

相关推荐
cd_9492172140 分钟前
哪些AI工具适合生成游戏开发用的3D道具模型?从道具草稿到引擎测试的选择方法
人工智能·3d
3D小将5 小时前
3D格式转换之IGS 转 CATIA 标准化转换技术
3d·solidworks模型·ug模型·sketchup模型·igs模型
熊猫钓鱼>_>8 小时前
当3DGS遇上ArkTS:用HarmonyOS端侧重建守护文化遗产——从零构建「古韵新生」完整工程
3d·华为·harmonyos·arkts·鸿蒙·3dgs
dalong109 小时前
WPF:3D正四面体自动旋转
3d·wpf
饼饼学习空间智能13 小时前
Kimi K3发布后,为什么数字孪生与物理AI底座更重要?从3D智能到具身智能落地
人工智能·深度学习·3d
Joker可视化开发平台15 小时前
Joker AIX 3D 导演台全解析:一站式影视分镜预演工作流
3d
lpfasd12315 小时前
2025-2026前端3D展示技术发展全景
前端·3d
a1117761 天前
中国古建筑瑰宝 3D THreeJS 开源
3d
星云_byto1 天前
DCDA:双评判器扩散对齐,开放天气LiDAR-4D雷达融合3D检测泛化新范式
3d·扩散模型·3d目标检测·开放天气泛化·lidar-4d雷达融合·鲁棒感知·k-radar
2401_859506242 天前
珠宝玉石加工的智能化改造:AI视觉检测、3D建模与区块链溯源实践
人工智能·3d·视觉检测