Struct Box3
Axis-aligned bounding box. Box3 consists of a position, a size, and several utility functions. It is typically used for fast overlap tests.
[Serializable]
public struct Box3 : IEquatable<Box3>
- Implements
- Inherited Members
Constructors
Box3(Vector3, Vector3)
Constructs an Box3 from a position and size.
public Box3(Vector3 position, Vector3 size)
Parameters
Box3(Vector3, float, float, float)
Constructs an Box3 from a position
,
width
, height
, and depth
.
public Box3(Vector3 position, float width, float height, float depth)
Parameters
position
Vector3The position.
width
floatThe width, typically positive.
height
floatThe height, typically positive.
depth
floatThe depth, typically positive.
Box3(float, float, float, Vector3)
Constructs an Box3 from x
,
y
, z
, and size
.
public Box3(float x, float y, float z, Vector3 size)
Parameters
x
floatThe position's X coordinate.
y
floatThe position's Y coordinate.
z
floatThe position's Z coordinate.
size
Vector3The size, typically positive.
Box3(float, float, float, float, float, float)
Constructs an Box3 from x
,
y
, z
, width
,
height
, and depth
.
public Box3(float x, float y, float z, float width, float height, float depth)
Parameters
x
floatThe position's X coordinate.
y
floatThe position's Y coordinate.
z
floatThe position's Z coordinate.
width
floatThe width, typically positive.
height
floatThe height, typically positive.
depth
floatThe depth, typically positive.
Properties
End
public Vector3 End { readonly get; set; }
Property Value
- Vector3
Getting is equivalent to
value
= Position + Size, setting is equivalent to Size =value
- Position
Position
Beginning corner. Typically has values lower than End.
public Vector3 Position { readonly get; set; }
Property Value
- Vector3
Directly uses a private field.
Size
Size from Position to End. Typically all components are positive. If the size is negative, you can use Abs() to fix it.
public Vector3 Size { readonly get; set; }
Property Value
- Vector3
Directly uses a private field.
Volume
The volume of this Box3. See also HasVolume().
public readonly float Volume { get; }
Property Value
Methods
Abs()
Returns an Box3 with equivalent position and size, modified so that the most-negative corner is the origin and the size is positive.
public readonly Box3 Abs()
Returns
Encloses(Box3)
public readonly bool Encloses(Box3 with)
Parameters
Returns
Equals(Box3)
Returns true if the AABBs are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Box3) instead, which is more reliable.
public readonly bool Equals(Box3 other)
Parameters
other
Box3The other AABB.
Returns
- bool
Whether or not the AABBs are exactly equal.
Equals(object?)
Returns true if the AABB is exactly equal
to the given object (obj
).
Note: Due to floating-point precision errors, consider using
IsEqualApprox(Box3) instead, which is more reliable.
public override readonly bool Equals(object? obj)
Parameters
obj
objectThe object to compare with.
Returns
- bool
Whether or not the AABB and the object are equal.
Expand(Vector3)
Returns this Box3 expanded to include a given point.
public readonly Box3 Expand(Vector3 point)
Parameters
point
Vector3The point to include.
Returns
FromCorners(Vector3, Vector3)
Constructs a Box3 from two corners.
public static Box3 FromCorners(Vector3 a, Vector3 b)
Parameters
Returns
GetCenter()
public readonly Vector3 GetCenter()
Returns
- Vector3
The center.
GetEndpoint(int)
Gets the position of one of the 8 endpoints of the Box3.
public readonly Vector3 GetEndpoint(int idx)
Parameters
idx
intWhich endpoint to get.
Returns
Exceptions
- ArgumentOutOfRangeException
idx
is less than 0 or greater than 7.
GetHashCode()
Serves as the hash function for Box3.
public override readonly int GetHashCode()
Returns
- int
A hash code for this AABB.
GetLongestAxis()
Returns the normalized longest axis of the Box3.
public readonly Vector3 GetLongestAxis()
Returns
GetLongestAxisIndex()
public readonly Axis GetLongestAxisIndex()
Returns
GetLongestAxisSize()
Returns the scalar length of the longest axis of the Box3.
public readonly float GetLongestAxisSize()
Returns
GetShortestAxis()
Returns the normalized shortest axis of the Box3.
public readonly Vector3 GetShortestAxis()
Returns
GetShortestAxisIndex()
public readonly Axis GetShortestAxisIndex()
Returns
GetShortestAxisSize()
Returns the scalar length of the shortest axis of the Box3.
public readonly float GetShortestAxisSize()
Returns
GetSupport(Vector3)
Returns the support point in a given direction. This is useful for collision detection algorithms.
public readonly Vector3 GetSupport(Vector3 dir)
Parameters
dir
Vector3The direction to find support for.
Returns
- Vector3
A vector representing the support.
Grow(float)
Returns a copy of the Box3 grown a given amount of units towards all the sides.
public readonly Box3 Grow(float by)
Parameters
by
floatThe amount to grow by.
Returns
HasPoint(Vector3)
public readonly bool HasPoint(Vector3 point)
Parameters
point
Vector3The point to check.
Returns
HasSurface()
Returns true if the Box3 has a surface or a length, and false if the Box3 is empty (all components of Size are zero or negative).
public readonly bool HasSurface()
Returns
HasVolume()
Returns true if the Box3 has area, and false if the Box3 is linear, empty, or has a negative Size. See also Volume.
public readonly bool HasVolume()
Returns
Intersection(Box3)
Returns the intersection of this Box3 and with
.
public readonly Box3 Intersection(Box3 with)
Parameters
Returns
Intersects(Box3)
public readonly bool Intersects(Box3 with)
Parameters
Returns
IntersectsPlane(Plane)
public readonly bool IntersectsPlane(Plane plane)
Parameters
Returns
IntersectsSegment(Vector3, Vector3)
public readonly bool IntersectsSegment(Vector3 from, Vector3 to)
Parameters
Returns
IsEqualApprox(Box3)
Returns true if this AABB and other
are approximately equal,
by running IsEqualApprox(Vector3, Vector3) on each component.
public readonly bool IsEqualApprox(Box3 other)
Parameters
other
Box3The other AABB to compare.
Returns
- bool
Whether or not the AABBs structures are approximately equal.
IsFinite()
Returns true if this Box3 is finite, by calling IsFinite(float) on each component.
public readonly bool IsFinite()
Returns
- bool
Whether this vector is finite or not.
Merge(Box3)
public readonly Box3 Merge(Box3 with)
Parameters
Returns
ToString()
Converts this Box3 to a string.
public override readonly string ToString()
Returns
- string
A string representation of this AABB.
ToString(string?)
Converts this Box3 to a string with the given format
.
public readonly string ToString(string? format)
Parameters
format
string
Returns
- string
A string representation of this AABB.
Operators
operator ==(Box3, Box3)
Returns true if the AABBs are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Box3) instead, which is more reliable.
public static bool operator ==(Box3 left, Box3 right)
Parameters
Returns
- bool
Whether or not the AABBs are exactly equal.
operator !=(Box3, Box3)
Returns true if the AABBs are not equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Box3) instead, which is more reliable.
public static bool operator !=(Box3 left, Box3 right)
Parameters
Returns
- bool
Whether or not the AABBs are not equal.