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

相关推荐
a1117761 小时前
中国古建筑瑰宝 3D THreeJS 开源
3d
星云_byto6 小时前
DCDA:双评判器扩散对齐,开放天气LiDAR-4D雷达融合3D检测泛化新范式
3d·扩散模型·3d目标检测·开放天气泛化·lidar-4d雷达融合·鲁棒感知·k-radar
2401_8595062410 小时前
珠宝玉石加工的智能化改造:AI视觉检测、3D建模与区块链溯源实践
人工智能·3d·视觉检测
a11177610 小时前
3D 建筑编辑器 Pascal Editor THreeJS 开源
前端·3d·html
a1117761 天前
坦克大战3D Three.js 3D (开源项目)
开发语言·javascript·3d
a1117761 天前
Web 3D 肌肉车展示项目 开源
前端·3d
dalong102 天前
WPF:3D立方体
3d·wpf
寒水馨2 天前
macOS下载、安装godot-4.7.1-stable(附安装包Godot_v4.7.1-stable_macos.universal.zip)
macos·3d·游戏引擎·godot·跨平台·游戏开发·2d
人工智能培训3 天前
AI如何学习知识?与人类学习的本质差异
大数据·网络·数据库·人工智能·学习·3d
3D小将3 天前
3D格式转换之Rhino(Rhinoceros)转 CATIA 标准化转换技术
3d·solidworks模型·ug模型·rhino模型·catia模型