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

相关推荐
2501_907136826 小时前
ChātGPT赋能的“SolidWorks工具箱”:重塑3D设计效率新标杆
3d
MediaTea12 小时前
Ae:导入 3D 模型
3d
渲染101专业云渲染12 小时前
渲染 101 支持 3ds Max 的渲染器及其优势
3d·云计算·动画·blender·maya·houdini
Hali_Botebie1 天前
【蒸馏(1)】UniDistill:用于BEV 3D检测的通用跨模态蒸馏框架!
3d
MediaTea1 天前
Ae 效果详解:3D 通道提取
3d
3DVisionary1 天前
XTOP3D的DIC技术在极端条件下的应用解决方案
数码相机·3d·航空工业·全场应变测量·航空机匣内部四测头同步测量·反射镜辅助dic观测·四测头方案
吃个糖糖2 天前
Halcon 3D加快表面匹配速度
3d
mirrornan2 天前
3D全景沉浸式看车:虚拟现实重构汽车消费新体验
科技·3d·汽车·vr·3d数字化·3d看车
pixle03 天前
Three.js 快速入门教程【一】开启你的 3D Web 开发之旅
前端·javascript·3d
视觉人机器视觉3 天前
机器视觉中的3D高反光工件检测
人工智能·3d·c#·视觉检测