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

相关推荐
ykjhr_3d15 小时前
东莞车间教学vr虚拟仿真实训制作公司
3d·vr·华锐视点·虚拟仿真实训·虚拟车间
3D小将1 天前
3D格式转换之3DXML 转换为 GLTF 技术文档
3d·maya·solidworks模型·ug模型·catia模型·stl模型
CG_MAGIC1 天前
3ds Max 模型导入导出常见报错解决方法
3d·贴图·效果图·建模教程·渲云渲染
郑寿昌1 天前
NaniteLumen打造高精3D互动游戏
游戏·3d
小短腿的代码世界1 天前
Qt 3D 深度解析:QtQuick 与 Scene Graph 驱动的工业级 3D 渲染架构
qt·3d·架构
hey202005282 天前
D5渲染器电脑配置不够怎么办
3d
JoyCong19982 天前
玩3D游戏、做三维设计时视角乱跳?ToDesk“3D鼠标”功能了解一下!
游戏·3d·计算机外设·远程工作·远程操作
Bnews2 天前
3D精准室内定位设备在机器人科研中的推荐
3d·机器人
3D小将2 天前
3D格式转换之X_T格式转换为GLTF格式技术文档
3d·solidworks模型·rhino模型·sketchup模型·igs模型
迁移科技2 天前
AI+3D视觉赋能铝制静盘自动化上下料,破解反光堆叠难题
人工智能·3d·自动化