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

相关推荐
cd_9492172110 小时前
3D角色自动绑骨怎么做?用V2Fun完成建模、绑定、动作和导出
人工智能·3d
wuhanzhanhui11 小时前
从 3D 建模到高光谱2026 武汉机器视觉展会|工业相机展会洞见感知力量新高度
数码相机·3d
3DVisionary20 小时前
单目高速DIC、双目DIC与传统传感器,谁更适合精密件?
网络·数码相机·3d·性能优化·注塑件全尺寸检测·高频振动测量·悬臂梁面内振动
Microvision维视智造1 天前
3D视觉项目落地的“最后一公里“——PLP400高精密3D视觉实验台上新
3d·视觉检测·机器视觉
蓝速科技1 天前
蓝速科技 75 寸 3D 全息舱黄金尺寸实测与选型评测
科技·3d
youwen212 天前
一个3D集装箱可视化工具
3d·外贸·集装箱·外贸工具
碧口科技2 天前
AI3D模型生成会不会结构不完整?判断标准与修复流程
人工智能·3d
苏州邦恩精密2 天前
江苏蔡司3D扫描仪厂家助力企业提升检测效率
人工智能·机器学习·3d·自动化·制造
城中南小3 天前
3D-VLA 方法原理详解
人工智能·计算机视觉·3d
劉宇雁3 天前
C++ ASCII 3D无尽跑酷游戏
c++·游戏·3d