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

相关推荐
3D小将14 小时前
3D格式转换之STP转GLTF格式技术文档
3d·solidworks模型·ug模型·sketchup模型·igs模型
Ulyanov14 小时前
《从质点到位姿:基于Python与PyVista的导弹制导控制全栈仿真》: 可视化革命——基于 PyVista 的 3D 战场构建与实时渲染
开发语言·python·算法·3d·系统仿真
GIS数据转换器21 小时前
农业物联网可视化管理系统
人工智能·物联网·3d·无人机·知识图谱·旅游
ZPC82101 天前
规划后的轨迹,如何发给 moveit_servo 执行
c++·人工智能·算法·3d
cskywit1 天前
【TMI2025】医学版 Stable Diffusion?3D MedDiffusion 如何生成高质量 3D 医学影像
人工智能·3d·stable diffusion
jiayong231 天前
数字孪生、虚拟仿真、3D 建模:详细分析与深刻总结
3d·数字孪生·虚拟仿真
kaikaile19951 天前
基于 MATLAB 的3D 蒙特卡洛光子传输模拟
开发语言·matlab·3d
AI视觉网奇1 天前
AI 3D建模生成STL文件教程 2026最新版
深度学习·3d
云飞云共享云桌面1 天前
机械工厂10个研发设计用SolidWorks,是否可以共享一台高配置服务器算力
大数据·运维·服务器·网络·3d·自动化·制造
小短腿的代码世界1 天前
Qt libQGLViewer 深度解析:高性能OpenGL 3D交互查看器的架构设计与性能优化
qt·3d·交互