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

相关推荐
一根数据线20 小时前
不用无人机也不用软件,AI也能直接从地图生成3D模型?
人工智能·3d·3d建模·无人机·倾斜摄影·造形家·场地建模
dalong101 天前
WPF:3D网格构建和渲染的三个核心
3d
dalong102 天前
WPF:3D顶点共享与法线设置对光照效果的影响
3d·wpf
摩尔线程2 天前
摩尔线程LiteGS入选ECCV 2026 高质量3DGS训练迎来效率突破
3d·摩尔线程
LateFrames2 天前
CT(DCM)外轮廓实时识别 → 逐层拼3D → 导出STL到Blender
图像处理·3d·可视化·医疗·ct重建
安徽尚格创意2 天前
安徽尚格创意签约深圳和力泰水乳灌装整线 3D仿真动画|助力日化设备投标演示
3d
云飞云共享云桌面3 天前
广东精密机械设备工厂8-10人共享一台SolidWorks设计服务器
运维·服务器·3d·自动化·汽车·制造
AI视觉网奇3 天前
bambu-studio-ai 踩坑实战笔记
人工智能·3d
MagicUrban3 天前
经纬管网MagicPipe3D V3.8.1发布,参数化自主可控三维管线建模工具
3d·gis·智慧城市·bim·地下管网
LONGZETECH3 天前
传统实训 vs 仿真实训:从技术架构拆解新能源汽车教学的范式之变
c语言·3d·unity·架构·汽车