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

相关推荐
迁移科技7 小时前
告别人工分拣!迁移科技 AI+3D 视觉让机器人 “看懂” 无序抓取
人工智能·科技·3d·机器人·自动化·视觉检测
玖釉-16 小时前
Vulkan 3D Gaussian Splatting 本地部署与运行:从 PLY 模型到实时渲染
3d
kobesdu16 小时前
当算法跑不通时:3D激光SLAM工程实践中的隐藏陷阱与全链路排查
算法·3d
AI视觉网奇17 小时前
b3dkit 生成连接器
3d
吃好睡好便好17 小时前
在Matlab中绘制饼状图
开发语言·学习·matlab·3d·信息可视化
koharu12318 小时前
PointRCNN 精解:从原始点云到三维框的两阶段检测
人工智能·深度学习·目标检测·3d·三维点云
似水流年wxk18 小时前
cocos3D物体线性溶解消失效果
3d
2401_863801461 天前
OBJ、FBX 与 GLTF 选择用于设计导入的最佳 3D 模型格式:实用的比较可帮助设计人员选择正确的 3D 格式,同时保持几何形状、纹理和性能不变。
3d
IT观测1 天前
从设备到课堂:3D打印教育迈入新阶段
3d
threelab2 天前
Three.js 3D 热力图效果 | 三维可视化 / AI 提示词
开发语言·前端·javascript·人工智能·3d·着色器