当前位置: 首页 > news >正文

Unity3D 房间去重叠化算法详解

前言
在Unity3D游戏开发中,经常需要生成和处理多个房间的场景,特别是在地牢生成、房屋布局或迷宫设计等应用中。为了确保生成的房间不会重叠,我们需要一种有效的去重叠化算法。以下将详细介绍该算法的原理和代码实现。
对惹,这里有一个游戏开发交流小组,希望大家可以点击进来一起交流一下开发经验呀!
算法原理
房间表示:
每个房间可以表示为一个矩形,其位置和大小由其在世界坐标系中的位置(x, y, z)以及宽度和高度决定。在Unity3D中,通常使用Rect组件或自定义的DungeonCell类来表示房间。
重叠检测:
重叠检测是判断两个房间是否相交的过程。这可以通过比较两个矩形的边界来实现。如果两个矩形的任意一边相交,则它们重叠。
移动房间:
一旦检测到重叠,就需要移动其中一个房间以避免重叠。移动的方向和距离可以根据重叠的严重程度来计算。一种简单的方法是计算两个房间中心点的差值,然后移动重叠的房间,使其中心点沿这个差值方向移动一定距离。
迭代处理:
由于移动一个房间可能会导致它与另一个房间重叠,因此需要迭代处理,直到所有房间都不重叠为止。
代码实现
以下是一个Unity3D中实现房间去重叠化算法的示例代码。

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using System.Linq;

public class DungeonCell : MonoBehaviour

{

public Rect CellRect { get; private set; }

public Vector3 Position { get; private set; }

public int Width { get; private set; }

public int Height { get; private set; }

public CellType cellType { get; set; }

public enum CellType

{

Normal,

Hall

}

public void CreateCell(int width, int height)

{

Width = width;

Height = height;

Position = transform.position;

CellRect = new Rect(Position.x - Width / 2, Position.z - Height / 2, Width, Height);

}

public void MoveTo(Vector3 position)

{

transform.position = position;

Position = position;

CellRect = new Rect(Position.x - Width / 2, Position.z - Height / 2, Width, Height);

}

public bool Overlap(DungeonCell comparedCell)

{

return CellRect.Overlaps(comparedCell.CellRect);

}

}

public class DungeonMaker : MonoBehaviour

{

public float CellCreationRadius = 150;

public int NumberOfCells = 40;

public int NumberOfHalls = 12;

public List<DungeonCell> cellsList = new List<DungeonCell>();

public int MinWidth = 3;

public int MaxWidth = 8;

public int MinLength = 3;

public int MaxLength = 8;

public float movementForce = 4.0f;

public List<DungeonCell> importantCells = new List<DungeonCell>();

void Start()

{

CreateCells();

SeparateCells();

MarkImportantCell();

}

private void CreateCells()

{

for (int i = 0; i < NumberOfCells; i++)

{

Vector2 Position2D = Random.insideUnitCircle * CellCreationRadius;

GameObject gameObjectPointer = new GameObject("Cell" + i);

DungeonCell cellPointer = gameObjectPointer.AddComponent<DungeonCell>();

cellPointer.CreateCell(Random.Range(MinWidth, MaxWidth + 1), Random.Range(MinLength, MaxLength + 1));

cellPointer.MoveTo(new Vector3(Position2D.x, 0, Position2D.y));

cellsList.Add(cellPointer);

}

}

private void SeparateCells()

{

bool allCellsNotOverlap = false;

while (!allCellsNotOverlap)

{

allCellsNotOverlap = true;

foreach (DungeonCell currentCell in cellsList)

{

Vector3 movementVector = Vector3.zero;

int numberOfOverlaps = 0;

foreach (DungeonCell comparedCell in cellsList)

{

if (currentCell == comparedCell) continue;

if (currentCell.Overlap(comparedCell))

{

movementVector += currentCell.transform.position - comparedCell.transform.position;

numberOfOverlaps++;

}

}

if (numberOfOverlaps != 0)

{

allCellsNotOverlap = false;

if (movementVector.magnitude > 0)

{

movementVector = movementVector.normalized * movementForce;

}

else

{

movementVector = Random.insideUnitCircle.normalized * movementForce;

}

currentCell.MoveTo(currentCell.transform.position + movementVector);

}

}

}

}

private void MarkImportantCell()

{

importantCells = cellsList.OrderByDescending(n => n.CellRect.width * n.CellRect.height).ToList();

for (int i = 0; i < NumberOfHalls; i++)

{

importantCells[i].cellType = DungeonCell.CellType.Hall;

}

}

}

技术详解
DungeonCell 类:
CreateCell 方法用于初始化房间的大小和位置,并计算其Rect边界。
MoveTo 方法用于更新房间的位置,并重新计算其Rect边界。
Overlap 方法用于检测两个房间是否重叠。

DungeonMaker 类:
CreateCells 方法用于随机生成指定数量的房间,并将它们添加到cellsList中。
SeparateCells 方法用于迭代处理房间重叠问题。通过计算重叠房间的移动向量,并将房间移动到新位置来避免重叠。
MarkImportantCell 方法用于根据房间大小将房间标记为重要房间(如大厅),以便后续处理。

通过该算法和代码实现,可以有效地解决Unity3D中房间重叠的问题,并为后续的房间布局和场景生成提供基础。
更多教学视频
Unity3D

www.bycwedu.com/promotion_channels/2146264125

http://www.lryc.cn/news/450017.html

相关文章:

  • mybatis 配置文件完成增删改查(五) :单条件 动态sql查询,相当于switch
  • 全球IP归属地查询-IP地址查询-IP城市查询-IP地址归属地-IP地址解析-IP位置查询-IP地址查询API接口
  • Vue3+FastAPI中Token的刷新机制(含代码示例)
  • 【GAN 图像生成】
  • 【自然语言处理】词嵌入模型
  • 了解针对基座大语言模型(类似 ChatGPT 的架构,Decoder-only)的重头预训练和微调训练
  • cmake如何在编译时区分-std=c++17和-std=gnu++17?检查宏
  • 速通数据结构与算法第七站 排序
  • 灵当CRM index.php接口SQL注入漏洞复现 [附POC]
  • 修复: Flux女生脸不再油光满面, 屁股下巴 -- 超实用Comfyui小技巧
  • Actions Speak Louder than Words Meta史诗级的端到端推荐大模型落地
  • 金智维KRPA之Excel自动化
  • 哪款宠物空气净化器能有效去除浮毛?希喂、352实测分享
  • 2024.9.28更换启辰R30汽车火花塞
  • 2024上海网站建设公司哪家比较好TOP3
  • TDesign组件库+vue3+ts 如何视觉上合并相同内容的table列?(自定义合并table列)
  • BACnet协议-(基于ISO 8802-3 UDP)(2)
  • android 根据公历日期准确节气计算年月日时天干地支 四柱八字
  • VMware虚拟机连接公网,和WindTerm
  • 游戏盾SDK真的能无视攻击吗
  • 【QT】亲测有效:“生成的目标文件包含了过多的段,超出了编译器或链接器允许的最大数量”错误的解决方案
  • 什么是 Apache Ingress
  • SpringBoot助力墙绘艺术市场创新
  • Antlr的使用
  • HealChat心理大语言模型 丨OPENAIGC开发者大赛高校组AI创作力奖
  • PyQt5整合爬虫制作图片爬取器-幽络源
  • DC00023基于jsp+MySQL新生报到管理系统
  • AdaptIoT——制造业中使用因果关系的自我标签系统
  • 代码随想录算法训练营Day15
  • Thinkphp/Laravel旅游景区预约系统的设计与实现