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

相关推荐
咔咔一顿操作1 天前
【CSS 3D 交互】实现精美翻牌效果:从原理到实战
前端·css·3d·交互·css3
Magnum Lehar1 天前
wpf 3d游戏引擎的PrimitiveMesh.h和ToolsCommon.h
3d·游戏引擎
新启航光学频率梳2 天前
激光频率梳 3D 轮廓测量 -- 平晶干涉法观察高精度平面度
科技·3d·制造
AndrewHZ2 天前
【3D算法技术】blender中,在曲面上如何进行贴图?
算法·3d·blender·贴图·三维建模·三维重建·pcg
新启航光学频率梳2 天前
激光频率梳 3D 轮廓测量 - 油路板的凹槽深度和平面度测量
科技·3d·制造
灵犀物润2 天前
Blender来设计一个机器宠物-完整的3D建模流程
3d·blender·宠物
杀生丸学AI2 天前
【三维重建】3R-GS:优化相机位姿的3DGS最佳实践
人工智能·3d·aigc·三维重建·视觉大模型·高斯泼溅
研梦非凡2 天前
ICCV 2025|基于曲线感知高斯溅射的3D参数曲线重建
人工智能·算法·3d
伊织code2 天前
Matplotlib 2 -绘图、统计、网格、3D
3d·matplotlib·绘图
咔咔一顿操作2 天前
【CSS 3D 交互】打造沉浸式 3D 照片墙:结合 JS 实现拖拽交互
前端·javascript·css·3d·交互·css3