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

相关推荐
2401_8638014611 小时前
OBJ、FBX 与 GLTF 选择用于设计导入的最佳 3D 模型格式:实用的比较可帮助设计人员选择正确的 3D 格式,同时保持几何形状、纹理和性能不变。
3d
IT观测12 小时前
从设备到课堂:3D打印教育迈入新阶段
3d
threelab15 小时前
Three.js 3D 热力图效果 | 三维可视化 / AI 提示词
开发语言·前端·javascript·人工智能·3d·着色器
yeflx16 小时前
Colmap增加轨迹过滤等参数
3d
VT LI16 小时前
Cocos2d-x 引擎架构全面深度解析:从底层渲染到上层交互的系统性技术全景
游戏引擎·cocos·引擎架构
千殇华来17 小时前
3D材料选择
百度·3d
yeflx18 小时前
COTR算法demo测试
3d
云飞云共享云桌面18 小时前
SolidWorks 服务器通过云飞云共享云桌面10人研发共享方案
运维·服务器·3d·设计模式·电脑
木斯佳19 小时前
HarmonyOS 6 ArkGraphics 3D精讲:坐标、向量与矩阵——初识3D数学的“空间建模”
线性代数·3d·矩阵
meilindehuzi_a19 小时前
Vibe Coding 实战:我用一条 Prompt 指挥 AI “盲盒式”生成 3D 积木物理世界
3d·prompt