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

相关推荐
LONGZETECH41 分钟前
Unity 3D+C/S架构无人机数字孪生实训室:破解实训“三高”难题的底层技术实现
c语言·开发语言·3d·unity·架构·无人机
艺杯羹1 小时前
Vibe Coding实战:从零构建网页3D交互角色
3d·ai·交互·ai编程·ai agent·vibe coding
菩提树下的凡夫15 小时前
基于Halcon深度图的曲面圆柱凹坑缺陷处理方法
人工智能·计算机视觉·3d
weixin_446260851 天前
[特殊字符] PhotoFlow: Agentic 3D 虚拟摄影任务 (优化审阅报告)
人工智能·3d
Tech_D1 天前
XR系列交叉滚轴模组:XRZ高精度气平衡Z轴模组详解
单片机·3d·机器人·自动化·制造
扫地的小何尚1 天前
30 步生成 512×512×256 的 3D 脑部 MRI
3d
AI视觉网奇2 天前
3d交互拆件 通俗易懂的学习笔记
3d
爱娶媳妇的苗同学2 天前
MV2DFusion 3D-BEV检测学习教程
目标检测·3d·自动驾驶·transformer
云飞云共享云桌面2 天前
企业降本增效新思路:SolidWorks共享部署实战经验分享
运维·服务器·网络·人工智能·3d·自动化
元让_vincent2 天前
论文Review SLAM Super-LIO | RA-L 2026 | 面向嵌入式平台的高效 LiDAR-Inertial Odometry 系统
3d·性能提升·kdtree·激光slam