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

相关推荐
GIS数据转换器21 小时前
基于3D GIS的监控视频精准标定平台
人工智能·物联网·3d·音视频·无人机·知识图谱
小宋加油啊1 天前
机械臂抓取物体 PVN3D算法调研学习
学习·算法·3d
文阿花1 天前
Echarts实现自动旋转柱状3D扇形图
前端·3d·echarts
小短腿的代码世界1 天前
QtitanRibbon 深度解析:工业级Ribbon界面框架的架构设计与自定义扩展
qt·3d·架构
苏州邦恩精密1 天前
江苏三维扫描仪厂家如何选择合适的工业测量方案?
人工智能·科技·机器学习·3d·自动化·制造
文阿花1 天前
Echarts实现自定旋转3D饼状图
javascript·3d·echarts·饼状图
code_pgf1 天前
3D点云目标检测(PointPillars)部署pipeline
人工智能·目标检测·3d
爱凤的小光1 天前
Cog3DRangeImagePlaneEstimatorTool完全指南
3d·visionpro
文阿花1 天前
Echarts实现柱状3D扇形图
android·3d·echarts
Zldaisy3d1 天前
中南&南洋理工 l 3D打印含Al高熵合金高周疲劳机制与晶格摩擦工程研究
3d