优化模式-空间分区
参考章节:https://gpp.tkchu.me/spatial-partition.html
脑内画面
空间分区把世界切成格子、树或区域,让"找附近对象"不再扫描全世界。它像城市邮编:要找附近的人,先看同一片区,而不是挨家挨户问完整座城市。
#mermaid-svg-WE9xeS13zJjyspZl{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-WE9xeS13zJjyspZl .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-WE9xeS13zJjyspZl .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-WE9xeS13zJjyspZl .error-icon{fill:#552222;}#mermaid-svg-WE9xeS13zJjyspZl .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-WE9xeS13zJjyspZl .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-WE9xeS13zJjyspZl .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-WE9xeS13zJjyspZl .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-WE9xeS13zJjyspZl .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-WE9xeS13zJjyspZl .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-WE9xeS13zJjyspZl .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-WE9xeS13zJjyspZl .marker{fill:#333333;stroke:#333333;}#mermaid-svg-WE9xeS13zJjyspZl .marker.cross{stroke:#333333;}#mermaid-svg-WE9xeS13zJjyspZl svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-WE9xeS13zJjyspZl p{margin:0;}#mermaid-svg-WE9xeS13zJjyspZl .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-WE9xeS13zJjyspZl .cluster-label text{fill:#333;}#mermaid-svg-WE9xeS13zJjyspZl .cluster-label span{color:#333;}#mermaid-svg-WE9xeS13zJjyspZl .cluster-label span p{background-color:transparent;}#mermaid-svg-WE9xeS13zJjyspZl .label text,#mermaid-svg-WE9xeS13zJjyspZl span{fill:#333;color:#333;}#mermaid-svg-WE9xeS13zJjyspZl .node rect,#mermaid-svg-WE9xeS13zJjyspZl .node circle,#mermaid-svg-WE9xeS13zJjyspZl .node ellipse,#mermaid-svg-WE9xeS13zJjyspZl .node polygon,#mermaid-svg-WE9xeS13zJjyspZl .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-WE9xeS13zJjyspZl .rough-node .label text,#mermaid-svg-WE9xeS13zJjyspZl .node .label text,#mermaid-svg-WE9xeS13zJjyspZl .image-shape .label,#mermaid-svg-WE9xeS13zJjyspZl .icon-shape .label{text-anchor:middle;}#mermaid-svg-WE9xeS13zJjyspZl .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-WE9xeS13zJjyspZl .rough-node .label,#mermaid-svg-WE9xeS13zJjyspZl .node .label,#mermaid-svg-WE9xeS13zJjyspZl .image-shape .label,#mermaid-svg-WE9xeS13zJjyspZl .icon-shape .label{text-align:center;}#mermaid-svg-WE9xeS13zJjyspZl .node.clickable{cursor:pointer;}#mermaid-svg-WE9xeS13zJjyspZl .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-WE9xeS13zJjyspZl .arrowheadPath{fill:#333333;}#mermaid-svg-WE9xeS13zJjyspZl .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-WE9xeS13zJjyspZl .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-WE9xeS13zJjyspZl .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-WE9xeS13zJjyspZl .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-WE9xeS13zJjyspZl .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-WE9xeS13zJjyspZl .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-WE9xeS13zJjyspZl .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-WE9xeS13zJjyspZl .cluster text{fill:#333;}#mermaid-svg-WE9xeS13zJjyspZl .cluster span{color:#333;}#mermaid-svg-WE9xeS13zJjyspZl div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-WE9xeS13zJjyspZl .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-WE9xeS13zJjyspZl rect.text{fill:none;stroke-width:0;}#mermaid-svg-WE9xeS13zJjyspZl .icon-shape,#mermaid-svg-WE9xeS13zJjyspZl .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-WE9xeS13zJjyspZl .icon-shape p,#mermaid-svg-WE9xeS13zJjyspZl .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-WE9xeS13zJjyspZl .icon-shape .label rect,#mermaid-svg-WE9xeS13zJjyspZl .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-WE9xeS13zJjyspZl .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-WE9xeS13zJjyspZl .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-WE9xeS13zJjyspZl :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 世界
Cell 0,0
Cell 1,0
Cell 0,1
Unit A
Unit B
Unit C
它解决的问题
碰撞检测、范围技能、AI 感知、拾取物查询都需要找附近对象。如果每个对象都和所有对象比较,复杂度会接近 O(n^2)。空间分区先缩小候选集,再做精确判断。
C# 示例
csharp
public sealed class SpatialGrid<T> where T : notnull
{
private readonly float _cellSize;
private readonly Dictionary<(int X, int Y), HashSet<T>> _cells = new();
private readonly Dictionary<T, (int X, int Y)> _positions = new();
public SpatialGrid(float cellSize)
{
_cellSize = cellSize;
}
public void Insert(T item, float x, float y)
{
var cell = ToCell(x, y);
if (!_cells.TryGetValue(cell, out var items))
{
items = new HashSet<T>();
_cells[cell] = items;
}
items.Add(item);
_positions[item] = cell;
}
public void Move(T item, float x, float y)
{
var next = ToCell(x, y);
if (_positions.TryGetValue(item, out var current) && current == next)
{
return;
}
Remove(item);
Insert(item, x, y);
}
public void Remove(T item)
{
if (!_positions.Remove(item, out var cell))
{
return;
}
if (_cells.TryGetValue(cell, out var items))
{
items.Remove(item);
}
}
public IEnumerable<T> QueryNearby(float x, float y)
{
var center = ToCell(x, y);
for (var dx = -1; dx <= 1; dx++)
for (var dy = -1; dy <= 1; dy++)
{
var cell = (center.X + dx, center.Y + dy);
if (!_cells.TryGetValue(cell, out var items))
{
continue;
}
foreach (var item in items)
{
yield return item;
}
}
}
private (int X, int Y) ToCell(float x, float y)
{
return ((int)MathF.Floor(x / _cellSize), (int)MathF.Floor(y / _cellSize));
}
}
什么时候用
- 大量对象需要按距离、区域、碰撞范围查询。
- 全量扫描已经成为性能瓶颈。
- 世界可以被合理划分成格子、树、区块或关卡房间。
使用时的锋利边
分区结构只负责减少候选集,不负责替代精确检测。格子里找到的对象仍然要做距离、碰撞体、阵营等最终判断。
分区粒度很关键。格子太大,候选集仍然很多;格子太小,对象跨格移动和查询邻居的成本会上升。