MoonTools.Bonk/Bonk/BroadPhase/SpatialHash.cs

101 lines
3.6 KiB
C#
Raw Normal View History

2019-09-06 08:11:58 +00:00
using System;
using System.Collections.Generic;
using MoonTools.Core.Structs;
namespace MoonTools.Core.Bonk
{
2019-10-25 21:01:36 +00:00
/// <summary>
/// Used to quickly check if two shapes are potentially overlapping.
/// </summary>
/// <typeparam name="T">The type that will be used to uniquely identify shape-transform pairs.</typeparam>
2019-09-06 08:11:58 +00:00
public class SpatialHash<T> where T : IEquatable<T>
{
private readonly int cellSize;
private readonly Dictionary<int, Dictionary<int, HashSet<T>>> hashDictionary = new Dictionary<int, Dictionary<int, HashSet<T>>>();
private readonly Dictionary<T, (IShape2D, Transform2D)> IDLookup = new Dictionary<T, (IShape2D, Transform2D)>();
2019-09-06 08:11:58 +00:00
public SpatialHash(int cellSize)
{
this.cellSize = cellSize;
}
private (int, int) Hash(float x, float y)
2019-09-06 08:11:58 +00:00
{
return ((int)Math.Floor(x / cellSize), (int)Math.Floor(y / cellSize));
2019-09-06 08:11:58 +00:00
}
2019-10-25 21:01:36 +00:00
/// <summary>
/// Inserts an element into the SpatialHash.
/// </summary>
/// <param name="id">A unique ID for the shape-transform pair.</param>
/// <param name="shape"></param>
/// <param name="transform2D"></param>
public void Insert<TShape2D>(T id, TShape2D shape, Transform2D transform2D) where TShape2D : struct, IShape2D
2019-09-06 08:11:58 +00:00
{
var box = shape.AABB(transform2D);
2019-09-06 08:11:58 +00:00
var minHash = Hash(box.MinX, box.MinY);
var maxHash = Hash(box.MaxX, box.MaxY);
for (int i = minHash.Item1; i <= maxHash.Item1; i++)
{
for (int j = minHash.Item2; j <= maxHash.Item2; j++)
{
if (!hashDictionary.ContainsKey(i))
{
hashDictionary.Add(i, new Dictionary<int, HashSet<T>>());
2019-09-06 08:11:58 +00:00
}
if (!hashDictionary[i].ContainsKey(j))
{
hashDictionary[i].Add(j, new HashSet<T>());
2019-09-06 08:11:58 +00:00
}
hashDictionary[i][j].Add(id);
IDLookup[id] = (shape, transform2D);
2019-09-06 08:11:58 +00:00
}
}
}
2019-10-25 21:01:36 +00:00
/// <summary>
/// Retrieves all the potential collisions of a shape-transform pair. Excludes any shape-transforms with the given ID.
/// </summary>
public IEnumerable<(T, IShape2D, Transform2D)> Retrieve<TShape2D>(T id, TShape2D shape, Transform2D transform2D) where TShape2D : struct, IShape2D
2019-09-06 08:11:58 +00:00
{
var box = shape.AABB(transform2D);
2019-09-06 08:11:58 +00:00
var minHash = Hash(box.MinX, box.MinY);
var maxHash = Hash(box.MaxX, box.MaxY);
for (int i = minHash.Item1; i <= maxHash.Item1; i++)
{
for (int j = minHash.Item2; j <= maxHash.Item2; j++)
{
if (hashDictionary.ContainsKey(i) && hashDictionary[i].ContainsKey(j))
{
foreach (var t in hashDictionary[i][j])
2019-09-06 08:11:58 +00:00
{
var (otherShape, otherTransform) = IDLookup[t];
if (!id.Equals(t)) { yield return (t, otherShape, otherTransform); }
2019-09-06 08:11:58 +00:00
}
}
}
}
}
2019-10-25 21:01:36 +00:00
/// <summary>
/// Removes everything that has been inserted into the SpatialHash.
/// </summary>
2019-09-06 08:11:58 +00:00
public void Clear()
{
foreach (var innerDict in hashDictionary.Values)
{
foreach (var set in innerDict.Values)
{
set.Clear();
}
}
IDLookup.Clear();
}
}
}