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

相关推荐
GISer_Jing13 小时前
前端图片、动图与动画全解析(含PNG/APNG/Lottie/GIF/Canvas/WebGL/WebGPU)
前端·3d·动画·webgl
沙振宇16 小时前
【Web】使用Vue3+PlayCanvas开发3D游戏(十)让人物动起来
前端·游戏·3d·人物·
_李小白17 小时前
【OSG学习笔记】Day 53: Text3D( 三维文字)
笔记·学习·3d
云飞云共享云桌面18 小时前
制造研发降本新思路:云飞云共享云桌面集群如何将软硬件利用率提升至200%?
运维·服务器·网络·人工智能·3d·制造
Yao.Li19 小时前
PVN3D Full ONNX 导出与自定义算子说明
人工智能·3d·具身智能
Yao.Li19 小时前
PVN3D 中 SA 模块与 FP 模块详解
人工智能·3d·具身智能
一直会游泳的小猫19 小时前
Pascal Editor:基于 WebGPU 的开源 3D 建筑编辑器技术解析
3d·开源·编辑器
大模型实验室Lab4AI20 小时前
MAG-3D: Multi-Agent Grounded Reasoning for 3D Understanding
人工智能·计算机视觉·3d
霍志杰1 天前
3ds max 安装VRay渲染器
3d