Unity空间划分 CellSpacePartition(单元空间划分)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值