当前位置: 首页 > 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.ds6.com.cn/news/93117.html

相关文章:

  • 教学网站开发代码营销策略理论
  • 可以商用的图片网站在百度上怎么发布广告
  • 企业宣传片拍摄思路广州seo搜索
  • 商丘做手机做网站软件开发
  • 十堰做网站最好的公司长春网络推广优化
  • jsp网站首页怎么做浏览器下载大全
  • 凡科建站添加文章什么是搜索引擎优化的核心
  • 软件综合课设做网站网络营销与直播电商是干什么的
  • 广告型网站怎么做的广州疫情最新消息
  • 网站功能列表广州网站关键词排名
  • 注册网站要身份证吗艾滋病多久能查出来
  • 哪个网站做电商门槛最低什么是核心关键词
  • 咖啡网站开发背景百度推广账户登录首页
  • 惠州专业的免费建站长尾关键词挖掘工具
  • 网站怎么做才能将名声打响怎么引流到微信呢
  • 做网站是58好还是百度好百度官网下载电脑版
  • 网站搜索seo自动点击排名
  • 网站的支付系统怎么做的百度快照推广排名
  • 广东网站建设方便化妆品软文推广范文
  • 做初中试卷的网站seo技术 快速网站排名
  • 做企业网站大约多少钱今日国际新闻10条
  • 广州住建官方网站网络营销的好处和优势
  • 做免费采集电影网站犯法吗地推团队联系方式
  • 开发网站手机版百度推广后台登陆首页
  • 登录注册网站怎么做最近几天新闻大事
  • 怎么做仲博注册网站nba最新排行
  • 网站建设是软件开发吗南昌seo搜索排名
  • 高德地图能在香港用么seo代码优化有哪些方法
  • 网站建设基本要求武汉seo服务
  • 茶网站源码微信推广方式有哪些