一个空间中个体数量大的时候就需要做一些结构进行分区以减少计算量。空间划分有很多种,本片介绍一个简单的划分方式:单元空间划分。
简单来说就是将一个空间划分成很多格子,格子中有存着个体的集合。个体运动过程中,更新所有格子的集合。
下面图片演示运动过程中格子的变化,绿色格子标识其个体集合中有>0数量的个体。

核心代码如下:
SpacePartition.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace CellSpacePartition
{
public class Cell {
public int id;
public Bounds bounds;
public List<Agent> agentList;
public Cell(int id, Vector3 center, Vector3 size) {
this.id = id;
this.agentList = new List<Agent>();
this.bounds = new Bounds(center, size);
}
public void RemoveAgent(Agent agent) {
if (agentList.Contains(agent)) {
agentList.Remove(agent);
}
}
public void AddAgent(Agent agent) {
if (!agentList.Contains(agent))
{
agentList.Add(agent);
}
}
}
public static class SpacePartition
{
static float spaceX;
static float spaceZ;
static int cellCountX;
static int cellCountZ;
static List<Cell> cellList;
static Vector3 cellSize;
static Vector3 cellStartPos;
public static void Init(float spaceX, float spaceZ, Vector3 cellSize) {
cellList = new List<Cell

最低0.47元/天 解锁文章
1730

被折叠的 条评论
为什么被折叠?



