Up to date

This page is up to date for Godot `4.2`. If you still find outdated information, please open an issue.

# AABB¶

A 3D axis-aligned bounding box.

## Description¶

The AABB built-in Variant type represents an axis-aligned bounding box in a 3D space. It is defined by its position and size, which are Vector3. It is frequently used for fast overlap tests (see intersects). Although AABB itself is axis-aligned, it can be combined with Transform3D to represent a rotated or skewed bounding box.

It uses floating-point coordinates. The 2D counterpart to AABB is Rect2. There is no version of AABB that uses integer coordinates.

Note: Negative values for size are not supported. With negative size, most AABB methods do not work correctly. Use abs to get an equivalent AABB with a non-negative size.

Note: In a boolean context, a AABB evaluates to `false` if both position and size are zero (equal to Vector3.ZERO). Otherwise, it always evaluates to `true`.

There are notable differences when using this API with C#. See C# API 與 GDScript 的不同 for more information.

## Properties¶

 Vector3 end `Vector3(0, 0, 0)` Vector3 position `Vector3(0, 0, 0)` Vector3 size `Vector3(0, 0, 0)`

## Constructors¶

 AABB AABB ( ) AABB AABB ( AABB from ) AABB AABB ( Vector3 position, Vector3 size )

## Methods¶

 AABB abs ( ) const bool encloses ( AABB with ) const AABB expand ( Vector3 to_point ) const Vector3 get_center ( ) const Vector3 get_endpoint ( int idx ) const Vector3 get_longest_axis ( ) const int get_longest_axis_index ( ) const float get_longest_axis_size ( ) const Vector3 get_shortest_axis ( ) const int get_shortest_axis_index ( ) const float get_shortest_axis_size ( ) const Vector3 get_support ( Vector3 dir ) const float get_volume ( ) const AABB grow ( float by ) const bool has_point ( Vector3 point ) const bool has_surface ( ) const bool has_volume ( ) const AABB intersection ( AABB with ) const bool intersects ( AABB with ) const bool intersects_plane ( Plane plane ) const Variant intersects_ray ( Vector3 from, Vector3 dir ) const Variant intersects_segment ( Vector3 from, Vector3 to ) const bool is_equal_approx ( AABB aabb ) const bool is_finite ( ) const AABB merge ( AABB with ) const

## Operators¶

 bool operator != ( AABB right ) AABB operator * ( Transform3D right ) bool operator == ( AABB right )

## Property Descriptions¶

Vector3 end = `Vector3(0, 0, 0)`

The ending point. This is usually the corner on the top-right and forward of the bounding box, and is equivalent to `position + size`. Setting this point affects the size.

Vector3 position = `Vector3(0, 0, 0)`

The origin point. This is usually the corner on the bottom-left and back of the bounding box.

Vector3 size = `Vector3(0, 0, 0)`

The bounding box's width, height, and depth starting from position. Setting this value also affects the end point.

Note: It's recommended setting the width, height, and depth to non-negative values. This is because most methods in Godot assume that the position is the bottom-left-back corner, and the end is the top-right-forward corner. To get an equivalent bounding box with non-negative size, use abs.

## Constructor Descriptions¶

AABB AABB ( )

Constructs an AABB with its position and size set to Vector3.ZERO.

AABB AABB ( AABB from )

Constructs an AABB as a copy of the given AABB.

AABB AABB ( Vector3 position, Vector3 size )

Constructs an AABB by `position` and `size`.

## Method Descriptions¶

AABB abs ( ) const

Returns an AABB equivalent to this bounding box, with its width, height, and depth modified to be non-negative values.

```var box = AABB(Vector3(5, 0, 5), Vector3(-20, -10, -5))
var absolute = box.abs()
print(absolute.position) # Prints (-15, -10, 0)
print(absolute.size)     # Prints (20, 10, 5)
```

Note: It's recommended to use this method when size is negative, as most other methods in Godot assume that the size's components are greater than `0`.

bool encloses ( AABB with ) const

Returns `true` if this bounding box completely encloses the `with` box. The edges of both boxes are included.

```var a = AABB(Vector3(0, 0, 0), Vector3(4, 4, 4))
var b = AABB(Vector3(1, 1, 1), Vector3(3, 3, 3))
var c = AABB(Vector3(2, 2, 2), Vector3(8, 8, 8))

print(a.encloses(a)) # Prints true
print(a.encloses(b)) # Prints true
print(a.encloses(c)) # Prints false
```

AABB expand ( Vector3 to_point ) const

Returns a copy of this bounding box expanded to align the edges with the given `to_point`, if necessary.

`var box = AABB(Vector3(`