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

相关推荐
海天鹰2 小时前
电机齿轮拉马
3d
鹧鸪云光伏2 小时前
3D光伏支架设计,让项目落地更直观
3d·光伏·光储
fie88892 小时前
基于MATLAB的3D心形图与玫瑰花图案实现
数学建模·matlab·3d
小宇的天下1 天前
Calibre 3Dstack --每日一个命令day3【stack】(3-3)
前端·数据库·3d
冥界摄政王1 天前
CesiumJS学习第四章 替换指定3D建筑模型
3d·vue·html·webgl·js·cesium
微凉的衣柜1 天前
【3D 打印避坑实录】如何用 Blender 彻底修复空壳 STL
c++·3d·blender
在下胡三汉1 天前
glTF/glb文件的深入指南下载获取:揭示3D可视化的核心
3d
军军君012 天前
Three.js基础功能学习四:摄像机与阴影
开发语言·前端·javascript·3d·typescript·three·三维
yesyesido2 天前
AI手办工坊:3D渲染级二次元写真生成、多风格角色定制与高清无损下载的一键创作平台
人工智能·3d