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·自动化·制造
文阿花5 小时前
Echarts实现自定旋转3D饼状图
javascript·3d·echarts·饼状图
code_pgf7 小时前
3D点云目标检测(PointPillars)部署pipeline
人工智能·目标检测·3d
爱凤的小光8 小时前
Cog3DRangeImagePlaneEstimatorTool完全指南
3d·visionpro
文阿花8 小时前
Echarts实现柱状3D扇形图
android·3d·echarts
Zldaisy3d9 小时前
中南&南洋理工 l 3D打印含Al高熵合金高周疲劳机制与晶格摩擦工程研究
3d
CG_MAGIC9 小时前
Substance Painter:图层蒙版与智能材质
3d·材质·贴图·substance painter·效果图·建模教程·渲云渲染
小短腿的代码世界10 小时前
Qt Quick 3D场景导入与渲染架构深度解析:从USD到PBR材质的完整管线
qt·3d·架构
3DVisionary10 小时前
模具电极3D检测真实案例:手机后盖注塑模石墨电极全流程实录
人工智能·3d·智能手机·案例分析·蓝光三维扫描·模具检测·石墨电极
code_pgf10 小时前
CenterPoint 3D 目标检测详解
人工智能·目标检测·3d