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

相关推荐
zttsm2 小时前
UBUNTU22.04安装ORB_SLAM3
3d
LONGZETECH3 小时前
Unity 3D工业级教育软件实战:200+无人机装调任务的碰撞检测与交互落地
3d·unity·架构·游戏引擎·无人机·交互·cocos2d
菩提树下的凡夫3 小时前
Halcon例程detect_3d_defects_on_battery_surface.hdev解析
3d
AI视觉网奇19 小时前
3d 打印模型修复
人工智能·3d
元让_vincent21 小时前
Spark 2.0:面向 Web 的 3DGS 可视化与大场景渲染平台详解
前端·3d·spark·渲染·轻量化·3dgs·lod
迁移科技1 天前
AI+3D视觉赋能汽车箱体智能上下料
人工智能·3d·自动化·视觉检测
3DVisionary1 天前
蓝光三维扫描:模具电极3D检测新方案
3d·智能制造·3d检测·非接触测量·蓝光三维扫描·xtom·模具电极
蓝速科技1 天前
蓝速科技 3D 全息数字人舱实景效能与选型指南
大数据·人工智能·科技·3d·交互
AI视觉网奇1 天前
3d 编辑算法总结
3d
蓝速科技1 天前
3D 全息数字人舱在多场景迎宾接待中的落地实战
3d