Struct ScyllaAABB2
Blittable 2D axis-aligned bounding box used for broad-phase spatial queries in 2D and projected 3D spaces.
Implements
Inherited Members
Namespace: Scylla.Core.Structures
Assembly: ScyllaCore.dll
Syntax
[BurstCompile]
public struct ScyllaAABB2 : IEquatable<ScyllaAABB2>
Remarks
An AABB is defined by two corner points (Min and Max) that represent the smallest box aligned with the coordinate axes that encloses a given object or set of points.
Key Characteristics:
- Axis-aligned: box edges are parallel to the X and Y axes (no rotation).
- Efficient: overlap and containment tests use simple min/max comparisons.
- Blittable: contains only primitive types; safe for Burst compilation and native memory.
- Immutable operations: methods return new values rather than modifying in place.
Coordinate System:
The coordinate system is caller-defined. Common projections include XY (screen or world 2D) and XZ (Unity's typical ground plane). The ScyllaLooseQuadtree uses ScyllaAABB2 with either projection depending on the configured ScyllaQuadtreePlane.
Validity:
A valid AABB has Max >= Min on both axes. Invalid AABBs (where Max < Min
on any axis) represent empty or degenerate regions and will fail IsValid checks.
Performance Notes:
- All operations are designed for SIMD optimization via
Unity.Mathematics. - Compatible with the Burst compiler for maximum performance in jobs.
- Zero managed allocations (struct with value semantics).
For 3D broad-phase work, use ScyllaAABB3 instead. For BVH construction, see ScyllaBVH.
Constructors
ScyllaAABB2(float2, float2)
Creates a new ScyllaAABB2 from explicit minimum and maximum corner points.
Declaration
public ScyllaAABB2(float2 min, float2 max)
Parameters
| Type | Name | Description |
|---|---|---|
| float2 | min | Minimum corner (smallest X and Y coordinates). For a valid AABB this must be
component-wise less than or equal to |
| float2 | max | Maximum corner (largest X and Y coordinates). For a valid AABB this must be
component-wise greater than or equal to |
Remarks
This constructor assigns the corners directly without validation. The caller is
responsible for ensuring max >= min on both axes
for a valid AABB. Use IsValid to verify the result if the inputs are not
known to be well-ordered.
Fields
Max
Maximum corner of the AABB (largest X and Y coordinates of the enclosed region).
Declaration
public float2 Max
Field Value
| Type | Description |
|---|---|
| float2 |
Remarks
For a valid AABB, Max must be greater than or equal to Min on both
axes. This point represents the corner with the largest coordinate values in the
caller-defined 2D coordinate system.
Min
Minimum corner of the AABB (smallest X and Y coordinates of the enclosed region).
Declaration
public float2 Min
Field Value
| Type | Description |
|---|---|
| float2 |
Remarks
For a valid AABB, Min must be less than or equal to Max on both
axes. This point represents the corner with the smallest coordinate values in the
caller-defined 2D coordinate system.
Properties
Center
Declaration
public float2 Center { get; }
Property Value
| Type | Description |
|---|---|
| float2 | The center point, computed as |
HalfSize
Gets the half-size (extents) of the AABB along each axis.
Declaration
public float2 HalfSize { get; }
Property Value
| Type | Description |
|---|---|
| float2 | Half the full size, computed as |
IsValid
Gets a value indicating whether this AABB is well-formed.
Declaration
public bool IsValid { get; }
Property Value
| Type | Description |
|---|---|
| bool |
|
Remarks
Degenerate AABBs (where Min == Max on one or both axes) are considered valid: they
represent zero-area lines or points, which are geometrically meaningful. Only AABBs where
Max < Min on any axis are considered invalid.
Size
Gets the full size of the AABB along each axis (width and height).
Declaration
public float2 Size { get; }
Property Value
| Type | Description |
|---|---|
| float2 | The full dimensions, computed as |
Methods
Contains(ScyllaAABB2)
Determines whether this AABB fully contains another AABB.
Declaration
public bool Contains(ScyllaAABB2 other)
Parameters
| Type | Name | Description |
|---|---|---|
| ScyllaAABB2 | other | The AABB to test for containment within this AABB. |
Returns
| Type | Description |
|---|---|
| bool |
|
Remarks
Containment is satisfied when every point in other lies inside or on
the boundary of this AABB. This requires:
this.Min <= other.Minon both axes (the other's minimum corner is inside this).this.Max >= other.Maxon both axes (the other's maximum corner is inside this).
Edge cases:
- AABBs that share a boundary are considered contained (inclusive test).
- An AABB always contains itself.
- Degenerate (zero-area) AABBs can still contain other degenerate AABBs at the same position.
DistanceSqToPoint(float2)
Computes the squared distance from a point to the nearest point on or inside this AABB.
Declaration
public float DistanceSqToPoint(float2 point)
Parameters
| Type | Name | Description |
|---|---|---|
| float2 | point | The 2D point to measure from. |
Returns
| Type | Description |
|---|---|
| float | The squared distance from |
Remarks
For each axis the per-axis distance component is computed as:
- If the point lies below Min: component =
Min - point. - If the point lies above Max: component =
point - Max. - If the point lies between Min and Max: component = 0.
The squared distance is the sum of the squared per-axis components.
Common Use Cases:
- Circle-vs-AABB test: a circle overlaps this AABB if
DistanceSqToPoint(center) <= radius * radius. - Nearest-neighbor priority ordering in tree traversal (see ScyllaLooseQuadtree).
- Range queries: find all AABBs within distance
Dof a point by testingdistanceSq <= D * D.
Working with squared distance avoids the expensive square root operation; only take the root when an actual distance value is required.
Equals(ScyllaAABB2)
Tests exact equality between this ScyllaAABB2 and another.
Declaration
public bool Equals(ScyllaAABB2 other)
Parameters
| Type | Name | Description |
|---|---|---|
| ScyllaAABB2 | other | The ScyllaAABB2 to compare with this instance. |
Returns
| Type | Description |
|---|---|
| bool |
|
Remarks
Two AABBs are equal if and only if both their Min and Max
corners are exactly equal (component-wise floating-point equality). This is a strict
equality test with no epsilon tolerance. For approximate equality with floating-point
tolerance, implement a custom comparison using NumberUtil.Approximately.
Equals(object)
Tests exact equality between this ScyllaAABB2 and a boxed object.
Declaration
public override bool Equals(object obj)
Parameters
| Type | Name | Description |
|---|---|---|
| object | obj | The object to compare with this instance. |
Returns
| Type | Description |
|---|---|
| bool |
|
Overrides
Remarks
Returns true only if obj is a ScyllaAABB2 with
exactly equal Min and Max corners. Returns false if
obj is null or not a ScyllaAABB2.
FromCenterHalfSize(float2, float2)
Creates a ScyllaAABB2 from a center point and a half-size (extents) vector.
Declaration
public static ScyllaAABB2 FromCenterHalfSize(float2 center, float2 halfSize)
Parameters
| Type | Name | Description |
|---|---|---|
| float2 | center | Center point of the new AABB in the caller-defined 2D coordinate system. |
| float2 | halfSize | Half the size of the AABB along each axis (the extents). Both components should be non-negative for a valid result. |
Returns
| Type | Description |
|---|---|
| ScyllaAABB2 | A new ScyllaAABB2 centered at |
Remarks
This is a convenient alternative to the min/max constructor when the center and extents of the bounding box are known. The resulting AABB is computed as:
Min = center - halfSizeMax = center + halfSize
halfSize should have non-negative components. Negative components
will produce an invalid AABB where Max < Min on the corresponding axis.
See also FromCenterHalfSize(float3, float3) for the 3D equivalent.
GetHashCode()
Computes a hash code for this ScyllaAABB2.
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| int | A 32-bit signed integer hash code suitable for use in hash tables and dictionaries. |
Overrides
Remarks
The hash code is derived by combining the hash codes of Min and
Max using a prime-number multiplication chain (initial value 17, multiplier
31). The unchecked block allows integer overflow, which is intentional and safe
for hash code computation. AABBs with equal Min and Max
corners are guaranteed to produce the same hash code.
Overlaps(ScyllaAABB2)
Determines whether this AABB overlaps or touches another AABB.
Declaration
public bool Overlaps(ScyllaAABB2 other)
Parameters
| Type | Name | Description |
|---|---|---|
| ScyllaAABB2 | other | The AABB to test for overlap with this AABB. |
Returns
| Type | Description |
|---|---|
| bool |
|
Remarks
The overlap test uses the separating axis theorem: two AABBs overlap when they are NOT separated along any axis. The test checks that:
this.Min <= other.Maxon both axes (this starts before or where other ends).this.Max >= other.Minon both axes (this ends after or where other starts).
This is an inclusive test:
- AABBs that share only a boundary (face, edge, or corner) are considered overlapping.
- Degenerate (zero-area) AABBs can overlap at a single point or line.
- The test is symmetric:
A.Overlaps(B) == B.Overlaps(A).
Performance: this is an extremely fast test (4 comparisons and a logical AND). It is the primary broad-phase test used by ScyllaLooseQuadtree.
RayIntersects(float2, float2, float, float)
Tests whether a 2D ray intersects this AABB using the efficient slab method.
Declaration
public bool RayIntersects(float2 origin, float2 invDir, float tMin, float tMax)
Parameters
| Type | Name | Description |
|---|---|---|
| float2 | origin | The 2D origin point of the ray. |
| float2 | invDir | The component-wise reciprocal of the ray direction ( |
| float | tMin | The minimum ray parameter defining the start of the segment to test. Use |
| float | tMax | The maximum ray parameter defining the end of the segment to test. Use
|
Returns
| Type | Description |
|---|---|
| bool |
|
Remarks
The slab method treats the AABB as the intersection of two axis-aligned slabs (one per axis). For each axis it computes where the ray enters and exits that slab, then finds the overall intersection interval across both axes.
Algorithm:
- For each axis, compute entry distance
t1and exit distancet2using(corner - origin) * invDir. - Find the maximum entry distance across all axes (the last slab entered).
- Find the minimum exit distance across all axes (the first slab exited).
- The ray intersects if
exit >= enter.
Inverse Direction:
invDir must be the component-wise reciprocal (1 / direction)
of the ray direction. Precomputing this value outside the test avoids division per axis
during traversal. Near-zero direction components should be replaced with
float.PositiveInfinity to correctly handle rays parallel to an axis. See
ScyllaRay3 for the analogous 3D ray type that handles this automatically.
Ray Parameter Range:
tMin: start of the ray segment to test (typically0for a ray from its origin).tMax: end of the ray segment to test (usefloat.PositiveInfinityfor an infinite ray).- A point at parameter
tlies atorigin + t * direction.
Operators
operator ==(ScyllaAABB2, ScyllaAABB2)
Returns true if two ScyllaAABB2 instances have exactly equal
Min and Max corners.
Declaration
public static bool operator ==(ScyllaAABB2 a, ScyllaAABB2 b)
Parameters
| Type | Name | Description |
|---|---|---|
| ScyllaAABB2 | a | The first AABB to compare. |
| ScyllaAABB2 | b | The second AABB to compare. |
Returns
| Type | Description |
|---|---|
| bool |
|
operator !=(ScyllaAABB2, ScyllaAABB2)
Returns true if two ScyllaAABB2 instances have different
Min or Max corners.
Declaration
public static bool operator !=(ScyllaAABB2 a, ScyllaAABB2 b)
Parameters
| Type | Name | Description |
|---|---|---|
| ScyllaAABB2 | a | The first AABB to compare. |
| ScyllaAABB2 | b | The second AABB to compare. |
Returns
| Type | Description |
|---|---|
| bool |
|