Class DynamicBVH<T>
A Dynamic Binary Volume Hierarchy implementation.
Namespace: Evergine.Common.Helpers
Assembly: Evergine.Common.dll
Syntax
public class DynamicBVH<T>
Type Parameters
Name | Description |
---|---|
T | The contained object. |
Remarks
Inspired by Box2D b2DynamicTree https://github.com/erincatto/Box2D/blob/master/Box2D/Collision/b2DynamicTree.h.
Constructors
DynamicBVH(int)
Initializes a new instance of the DynamicBVH<T> class.
Declaration
public DynamicBVH(int defaultNodeCapacity = 16)
Parameters
Type | Name | Description |
---|---|---|
int | defaultNodeCapacity | The default node capacity. |
Fields
NodeCount
The number of nodes.
Declaration
public int NodeCount
Field Value
Type | Description |
---|---|
int |
Nodes
The array of nodes.
Declaration
public DynamicBVHNode<T>[] Nodes
Field Value
Type | Description |
---|---|
DynamicBVHNode<T>[] |
Properties
AreaRatio
Gets the ratio of the sum of the node areas to the root area.
Declaration
public float AreaRatio { get; }
Property Value
Type | Description |
---|---|
float |
Height
Gets the height of the binary tree in O(N) time. It should not be called often.
Declaration
public int Height { get; }
Property Value
Type | Description |
---|---|
int |
MaxBalance
Gets the maximum balance of a node in the tree. The balance is the difference in height between the two children of a node.
Declaration
public int MaxBalance { get; }
Property Value
Type | Description |
---|---|
int |
Methods
CreateProxy(ref BoundingBox, T)
Creates a proxy. Provides a tightly fitting AABB and a userData pointer.
Declaration
public int CreateProxy(ref BoundingBox aabb, T userData)
Parameters
Type | Name | Description |
---|---|---|
BoundingBox | aabb | The AABB. |
T | userData | The userData. |
Returns
Type | Description |
---|---|
int | The proxy ID. |
DestroyProxy(int)
Destroys a proxy. This asserts if the ID is invalid.
Declaration
public void DestroyProxy(int proxyId)
Parameters
Type | Name | Description |
---|---|---|
int | proxyId | The proxy ID. |
GetFatAABB(int, out BoundingBox)
Gets the fat AABB for a proxy.
Declaration
public void GetFatAABB(int proxyId, out BoundingBox fatAABB)
Parameters
Type | Name | Description |
---|---|---|
int | proxyId | The proxy ID. |
BoundingBox | fatAABB | Output value for the fat AABB. |
GetUserData(int)
Gets proxy user data.
Declaration
public T GetUserData(int proxyId)
Parameters
Type | Name | Description |
---|---|---|
int | proxyId | The proxy ID. |
Returns
Type | Description |
---|---|
T | The proxy user data or null if the ID is invalid. |
MoveProxy(int, ref BoundingBox, ref Vector3)
Move a proxy with a swept AABB. If the proxy has moved outside of its fattened AABB, then the proxy is removed from the tree and reinserted. Otherwise, the function returns immediately.
Declaration
public bool MoveProxy(int proxyId, ref BoundingBox aabb, ref Vector3 displacement)
Parameters
Type | Name | Description |
---|---|---|
int | proxyId | The proxy Id. |
BoundingBox | aabb | The AABB. |
Vector3 | displacement | The displacement. |
Returns
Type | Description |
---|---|
bool | true if the proxy was reinserted. |
Query<TResult>(QueryDelegate<TResult>, ref BoundingBox)
Query an AABB for overlapping proxies. The callback function is called for each proxy that overlaps the supplied AABB.
Declaration
public TResult Query<TResult>(DynamicBVH<T>.QueryDelegate<TResult> callback, ref BoundingBox aabb)
Parameters
Type | Name | Description |
---|---|---|
DynamicBVH<T>.QueryDelegate<TResult> | callback | A callback function called on every hit. |
BoundingBox | aabb | The AABB to test. |
Returns
Type | Description |
---|---|
TResult | The query result. |
Type Parameters
Name | Description |
---|---|
TResult | The result type. |
RayCast(QueryDelegate, ref Ray)
Ray-cast against the proxies in the tree. This relies on the callback to perform an exact ray-cast in cases where the proxy contains a shape. The callback also performs any collision filtering. This has performance roughly equal to k * log(n), where k is the number of collisions and n is the number of proxies in the tree.
Declaration
public void RayCast(DynamicBVH<T>.QueryDelegate callback, ref Ray input)
Parameters
Type | Name | Description |
---|---|---|
DynamicBVH<T>.QueryDelegate | callback | A callback function that is called for each proxy that is hit by the ray. |
Ray | input | The ray-cast input data. The ray extends from p1 to p1 + maxFraction * (p2 - p1). |
RebuildBottomUp()
Builds an optimal tree. Very expensive. For testing.
Declaration
public void RebuildBottomUp()
ShiftOrigin(ref Vector3)
Shifts the world origin. This is useful for large worlds. The shift formula is: position -= newOrigin.
Declaration
public void ShiftOrigin(ref Vector3 newOrigin)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | newOrigin | The new origin with respect to the old origin. |
Validate()
Validates this tree. For testing.
Declaration
public void Validate()