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

相关推荐
LONGZETECH11 小时前
无人机仿真教学软件选型实战:5 个硬核技术维度,避开实训建设踩坑
3d·无人机·交互·cocos2d
装不满的克莱因瓶13 小时前
了解3D卷积原理——从空间感知到时空建模的深度学习核心算子
人工智能·pytorch·python·深度学习·机器学习·3d·ai
雪的季节13 小时前
Qt Graphs 2D+3D介绍
qt·3d
CG_MAGIC15 小时前
3ds Max材质编辑器:精简模式与Slate模式对比
3d·编辑器·材质·贴图·uv·建模教程
装不满的克莱因瓶16 小时前
掌握3D CNN模型结构——从时空特征建模到视频理解与医学影像核心架构
人工智能·pytorch·python·深度学习·神经网络·3d·cnn
AniShort16 小时前
AniShort携3D世界+3D导演台王炸组合AI短剧协作平台亮相2026横店AI短剧大会 近亿元融资赋能短剧工业化
人工智能·microsoft·3d
似水流年wxk17 小时前
cocos3.8,动态擦除3d效果,橡皮擦功能
3d
云飞云共享云桌面18 小时前
集中算力・统一数据・高效协同:SolidWorks 云桌面方案详解
运维·服务器·人工智能·安全·3d·电脑·制造
私人珍藏库2 天前
[Android] 三维山水全景地图-3D地形全景观测地图
android·3d·app·工具·软件·多功能
趋之3 天前
Mars3D 三维可视化开发入门实战教程
3d