AABB¶
Overview¶
An AABB (Axis-Aligned Bounding Box) is used to represent a 3D region in the world, which is aligned to the X, Y and Z axes.
They are constructed from two 3D positions in the world, which represent the minimum value corner and the maximum value corner.
Examples¶
-- An AABB which is
-- X: 0 -> 10
-- Y: -20 -> 100
-- Z: 40 -> 50
-- Minimum corner = (0, -20, 40)
-- Maximum corner = (10, 100, 50)
local aabb = AABB.new(Vector3.new(0, -20, 40), Vector3.new(10, 100, 50))
-- Another AABB
local otherAABB = AABB.new(Vector3.new(5, 50, 10), Vector3.new(45, 60, 45))
-- Test intersections
local intersects = aabb:Intersects(otherAABB)
local containsPosition = aabb:IsInside(Vector3.new(4, 70, 42))
-- Modify
aabb:ScaleFromCentre(Vector3.new(0.5, 0.5, 0.5))
aabb:AddPoint(Vector3.new(40, 200, 90))
Enumerations¶
BoxCornerType¶
Used with AABB:GetCorner() to select the position of a specific corner of an AABB.
Axis Terminology
Axis directions use the following terms:
| Negative | Positive |
|---|---|
X- = Left |
X+ = Right |
Y- = Bottom |
Y+ = Top |
Z- = Near |
Z+ = Far |
| Name | Value |
|---|---|
| LEFT_TOP_NEAR | 0 |
| RIGHT_TOP_NEAR | 1 |
| LEFT_BOTTOM_NEAR | 2 |
| RIGHT_BOTTOM_NEAR | 3 |
| LEFT_TOP_FAR | 4 |
| RIGHT_TOP_FAR | 5 |
| LEFT_BOTTOM_FAR | 6 |
| RIGHT_BOTTOM_FAR | 7 |
Constructors¶
Creates and returns an AABB with the provided values.
| Parameter | Type | Description |
|---|---|---|
| minimum | Vector3 | The minimum value on each axis. |
| maximum | Vector3 | The maximum value on each axis. |
| Returns | Description |
|---|---|
AABB |
The newly created AABB |
Methods¶
GetCorner¶
Returns the position of the given corner type in this AABB.
| Parameter | Type | Description |
|---|---|---|
| corner | BoxCornerType | The corner type. |
| Returns | Description |
|---|---|
| Vector3 | The position of the corner. |
Example
IsInside(Vector3)¶
Returns whether the position is within the AABB
| Parameter | Type | Description |
|---|---|---|
| position | Vector3 | The position to test. |
| Returns | Description |
|---|---|
| Boolean | Whether the given position is within this AABB. |
Example
IsInside(AABB)¶
Returns whether the given other AABB entirely encompasses this AABB.
| Parameter | Type | Description |
|---|---|---|
| otherAABB | AABB | The AABB to test. |
| Returns | Description |
|---|---|
| Boolean | Whether this AABB is entirely with the given AABB. |
Example
Intersects(Vector3, Vector3)¶
Returns whether the given line segment intersects with this AABB at any point.
| Parameter | Type | Description |
|---|---|---|
| fromPosition | Vector3 | The line segment start position to test. |
| toPosition | Vector3 | The line segment end position to test. |
| Returns | Description |
|---|---|
| Boolean | Whether an intersection occurs. |
Example
Intersects(AABB)¶
Returns whether the given AABB intersects with this AABB at any point.
| Parameter | Type | Description |
|---|---|---|
| otherAABB | AABB | The AABB to test. |
| Returns | Description |
|---|---|
| Boolean | Whether an intersection occurs. |
Example
local aabb = AABB.new(Vector3.new(5, 50, 10), Vector3.new(45, 60, 45))
aabb:Intersects(AABB.new(Vector3.new(0, 0, 0), Vector3.new(100, 100, 100)))
-- true
aabb:Intersects(Vector3.new(10, 20, 30), Vector3.new(30, 70, 35))
-- true
aabb:Intersects(Vector3.new(10, 20, 30), Vector3.new(30, 40, 35))
-- false
GetClosestPosition¶
Returns the position on the AABB surface which is closest to the given position. If the position is inside the AABB then the given position will be returned.
| Parameter | Type | Description |
|---|---|---|
| position | Vector3 | The position to test. |
| Returns | Description |
|---|---|
| Vector3 | The closest AABB surface position, or the given position if it is inside the AABB. |
Example
GetFurthestPosition¶
Returns the position on the AABB surface which is furthest to the given position.
| Parameter | Type | Description |
|---|---|---|
| position | Vector3 | The position to test. |
| Returns | Description |
|---|---|
| Vector3 | The furthest AABB surface position. |
Example
GetDistanceToPosition¶
Returns the shortest distance from the given position to this AABB.
Will return 0.0 if the position is inside the AABB.
| Parameter | Type | Description |
|---|---|---|
| position | Vector3 | The position to test. |
| Returns | Description |
|---|---|
| Float | The closest distance from the position to the AABB, or 0 if the position is inside the AABB. |
Example
Translate¶
Moves the AABB's minimum and maximum elements by the given offset value.
| Parameter | Type | Description |
|---|---|---|
| offset | Vector3 | The offset applied to the AABB. |
Example
Scale¶
Scales the AABB about 0,0,0 by the given amount.
| Parameter | Type | Description |
|---|---|---|
| scale | Vector3 | The scaling value to apply to the AABB. |
Example
ScaleFromCentre¶
Scales the AABB about the centre of the AABB by the given amount.
Info
The calculation for this method is:
| Parameter | Type | Description |
|---|---|---|
| scale | Vector3 | The scaling value to apply to the AABB. |
Example
AddPoint¶
Expands this AABB to encompass the given position.
| Parameter | Type | Description |
|---|---|---|
| position | Vector3 | The position to encompass. |
Example
AddAABB¶
Expands this AABB to encompass the given AABB.
| Parameter | Type | Description |
|---|---|---|
| otherAABB | AABB | The AABB to encompass. |
Example
Properties¶
| Name | Type | Writeable | Description |
|---|---|---|---|
| minimum | Vector3 | The minimum values of each axis. | |
| maximum | Vector3 | The maximum values of each axis. | |
| dimensions | Vector3 | The width, height, and length values of the AABB. | |
| centre | Vector3 | The centre position of the AABB. | |
| minX | Float | The minimum value of the X axis. | |
| minY | Float | The minimum value of the Y axis. | |
| minZ | Float | The minimum value of the Z axis. | |
| maxX | Float | The maximum value of the X axis. | |
| maxY | Float | The maximum value of the Y axis. | |
| maxZ | Float | The maximum value of the Z axis. | |
| width | Float | The width of the AABB. | |
| height | Float | The height of the AABB. | |
| length | Float | The length of the AABB. | |
| radiusMinimum | Float | The radius of a sphere centered inside the AABB that wholly contains the AABB at any orientation | |
| radiusMinimumSq | Float | The squared radius of a sphere centered inside the AABB that wholly contains the AABB at any orientation | |
| radiusMaximum | Float | The radius of a sphere centered inside the AABB that is the largest sphere that can fit inside this AABB | |
| radiusMaximumSq | Float | The squared radius of a sphere centered inside the AABB that is the largest sphere that can fit inside this AABB |