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

相关推荐
AI前沿资讯2 小时前
2026 AI 3D工具推荐:V2Fun如何重新定义“一站式角色创作”
人工智能·3d
ZPC82104 小时前
双目相机 + RGB-D + 点云:生成物体 3D 包围盒 + 计算抓取姿态
数码相机·3d
CG_MAGIC7 小时前
3D 建模比例把控技巧
3d·效果图·建模教程·渲云渲染
墨绿色的摆渡人9 小时前
论文笔记(一百一十五)Physically Embodied Gaussian Splatting: ... 3D Representation for Robotics (一)
论文阅读·3d
点PY10 小时前
Anywhere3D-Bench论文精读
3d
3D小将12 小时前
3D格式转换之IFC格式转换为GLTF格式技术文档
3d·solidworks模型·rhino模型·sketchup模型·igs模型
CG_MAGIC14 小时前
Enscape 新手从零到出图全流程
3d·贴图·uv·建模教程·渲云渲染
charlie11451419114 小时前
通用GUI编程技术——图形渲染实战(四十三)——D3D12设计哲学:显式控制与性能解锁
学习·3d·c·图形渲染·win32
AI前沿资讯15 小时前
一站式 AI 3D 创作首选:V2Fun—— 直连 Unity + 多人动捕双核心,重塑轻量化生产管线
人工智能·3d·unity
DTAS尺寸公差分析软件1 天前
DTAS 3D公差分析软件最新版本介绍
python·3d·尺寸公差分析·尺寸链计算·尺寸工程·尺寸链校核软件·公差仿真分析