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

相关推荐
cxoptics17 小时前
3D电影用的偏振眼镜原理是什么?
3d
我才是银古1 天前
OpenCSG.NET:一个 .NET CSG 几何建模库的技术剖析
3d·dwg·csg
迁移科技1 天前
周转箱拆垛码垛自动化实战:3D视觉实现 ±2mm 毫米级稳定作业
3d·自动化·视觉检测
a1117761 天前
汽车3D配置器 THreeJS 开源项目
前端·3d·html·汽车
dalong101 天前
WPF:3D正八面体自动旋转
3d·wpf
数字新视界1 天前
3D数字化孪生管理解决方案
物联网·3d·数字孪生·3d可视化
宇擎智脑科技2 天前
img2threejs 架构深度解析:AI 如何高效地从图片“雕刻“3D 模型
人工智能·3d·agent
柒木森2 天前
MMGS:基于多视角排序的最优传输聚合实现 10 倍压缩的 3DGS
3d