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

相关推荐
海伯森技术5 小时前
海伯森3D线光谱共焦技术在表面粗糙度检测的应用
3d
脾气有点小暴9 小时前
ECharts 伪 3D 柱状图完整注释 + 实现原理说明
前端·3d·信息可视化·vue·echarts
lialaka13 小时前
「声纹迷宫(Acoustic Labyrinth)」:具身交互智能驱动的实时声学解构与全双工语音数字人控制台
人工智能·3d·交互
蓝速科技16 小时前
蓝速科技 3D 全息数字人私模工艺与算力硬件深度评测
科技·3d
dalong1016 小时前
WPF:3D正四面体旋转改错
3d·wpf
lialaka17 小时前
「极客智库(The Architect‘s Nexus)」——全语音 3D AI 首席架构师与具身交互智能会话推演舱
人工智能·3d·交互
DTAS尺寸公差分析软件17 小时前
国产自研-DTAS 3D公差分析软件-功能简介
人工智能·3d·尺寸公差分析·三维公差分析·公差计算软件·尺寸链分析软件
cd_949217211 天前
哪些AI工具适合生成游戏开发用的3D道具模型?从道具草稿到引擎测试的选择方法
人工智能·3d
3D小将1 天前
3D格式转换之IGS 转 CATIA 标准化转换技术
3d·solidworks模型·ug模型·sketchup模型·igs模型
熊猫钓鱼>_>1 天前
当3DGS遇上ArkTS:用HarmonyOS端侧重建守护文化遗产——从零构建「古韵新生」完整工程
3d·华为·harmonyos·arkts·鸿蒙·3dgs