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

相关推荐
kailp2 小时前
语言模型玩转3D生成:LLaMA-Mesh开源项目
人工智能·3d·ai·语言模型·llama·gpu算力
莱茶荼菜2 天前
虚拟项目[3D物体测量]
数码相机·计算机视觉·3d
云空2 天前
《PyQt6-3D:开启Python 3D开发新世界》
python·3d·pyqt
咔咔一顿操作2 天前
5、Vue中使用Cesium实现交互式折线绘制详解
前端·javascript·vue.js·3d
元让_vincent2 天前
论文Review 3DGSSLAM GauS-SLAM: Dense RGB-D SLAM with Gaussian Surfels
图像处理·人工智能·平面·3d·图形渲染
笨笨马甲2 天前
Qt 3D模块加载复杂模型
开发语言·qt·3d
Coovally AI模型快速验证2 天前
从FCOS3D到PGD:看深度估计如何快速搭建你的3D检测项目
人工智能·深度学习·神经网络·yolo·3d·cnn
不收藏找不到我2 天前
将手工建模模型(fbx、obj)转换为3dtiles的免费工具!
3d
草莓奶忻3 天前
Eigen 几何模块深拆:Isometry3d vs Affine3d + 变换矩阵本质详解
3d
Casia_Dominic3 天前
【三维重建工具】NeRFStudio、3D GaussianSplatting、Colmap安装与使用指南
git·3d·github·点云