Up to date

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

Rect2

A 2D axis-aligned bounding box using floating-point coordinates.

Description

Rect2 consists of a position, a size, and several utility functions. It is typically used for fast overlap tests.

It uses floating-point coordinates. If you need integer coordinates, use Rect2i instead.

The 3D counterpart to Rect2 is AABB.

Negative values for size are not supported and will not work for most methods. Use abs to get a Rect2 with a positive size.

Tutorials

Properties

Vector2

end

Vector2(0, 0)

Vector2

position

Vector2(0, 0)

Vector2

size

Vector2(0, 0)

Constructors

Rect2

Rect2 ( )

Rect2

Rect2 ( Rect2 from )

Rect2

Rect2 ( Rect2i from )

Rect2

Rect2 ( Vector2 position, Vector2 size )

Rect2

Rect2 ( float x, float y, float width, float height )

Methods

Rect2

abs ( ) const

bool

encloses ( Rect2 b ) const

Rect2

expand ( Vector2 to ) const

float

get_area ( ) const

Vector2

get_center ( ) const

Rect2

grow ( float amount ) const

Rect2

grow_individual ( float left, float top, float right, float bottom ) const

Rect2

grow_side ( int side, float amount ) const

bool

has_area ( ) const

bool

has_point ( Vector2 point ) const

Rect2

intersection ( Rect2 b ) const

bool

intersects ( Rect2 b, bool include_borders=false ) const

bool

is_equal_approx ( Rect2 rect ) const

bool

is_finite ( ) const

Rect2

merge ( Rect2 b ) const

Operators

bool

operator != ( Rect2 right )

Rect2

operator * ( Transform2D right )

bool

operator == ( Rect2 right )


Property Descriptions

Vector2 end = Vector2(0, 0)

Ending corner. This is calculated as position + size. Setting this value will change the size.


Vector2 position = Vector2(0, 0)

Beginning corner. Typically has values lower than end.


Vector2 size = Vector2(0, 0)

Size from position to end. Typically, all components are positive.

If the size is negative, you can use abs to fix it.


Constructor Descriptions

Rect2 Rect2 ( )

Constructs a default-initialized Rect2 with default (zero) values of position and size.


Rect2 Rect2 ( Rect2 from )

Constructs a Rect2 as a copy of the given Rect2.


Rect2 Rect2 ( Rect2i from )

Constructs a Rect2 from a Rect2i.


Rect2 Rect2 ( Vector2 position, Vector2 size )

Constructs a Rect2 by position and size.


Rect2 Rect2 ( float x, float y, float width, float height )

Constructs a Rect2 by x, y, width, and height.


Method Descriptions

Rect2 abs ( ) const

Returns a Rect2 with equivalent position and area, modified so that the top-left corner is the origin and width and height are positive.


bool encloses ( Rect2 b ) const

Returns true if this Rect2 completely encloses another one.


Rect2 expand ( Vector2 to ) const

Returns a copy of this Rect2 expanded to include a given point.

Example:

# position (-3, 2), size (1, 1)
var rect = Rect2(Vector2(-3, 2), Vector2(1, 1))
# position (-3, -1), size (3, 4), so we fit both rect and Vector2(0, -1)
var rect2 = rect.expand(Vector2(0, -1))

float get_area ( ) const

Returns the area of the Rect2. See also has_area.


Vector2 get_center ( ) const

Returns the center of the Rect2, which is equal to position + (size / 2).


Rect2 grow ( float amount ) const

Returns a copy of the Rect2 grown by the specified amount on all sides.


Rect2 grow_individual ( float left, float top, float right, float bottom ) const

Returns a copy of the Rect2 grown by the specified amount on each side individually.


Rect2 grow_side ( int side, float amount ) const

Returns a copy of the Rect2 grown by the specified amount on the specified Side.


bool has_area ( ) const

Returns true if the Rect2 has area, and false if the Rect2 is linear, empty, or has a negative size. See also get_area.


bool has_point ( Vector2 point ) const

Returns true if the Rect2 contains a point. By convention, the right and bottom edges of the Rect2 are considered exclusive, so points on these edges are not included.

Note: This method is not reliable for Rect2 with a negative size. Use abs to get a positive sized equivalent rectangle to check for contained points.


Rect2 intersection ( Rect2 b ) const

Returns the intersection of this Rect2 and b.

If the rectangles do not intersect, an empty Rect2 is returned.


bool intersects ( Rect2 b, bool include_borders=false ) const

Returns true if the Rect2 overlaps with b (i.e. they have at least one point in common).

If include_borders is true, they will also be considered overlapping if their borders touch, even without intersection.


bool is_equal_approx ( Rect2 rect ) const

Returns true if this Rect2 and rect are approximately equal, by calling is_equal_approx on each component.


bool is_finite ( ) const

Returns true if this Rect2 is finite, by calling @GlobalScope.is_finite on each component.


Rect2 merge ( Rect2 b ) const

Returns a larger Rect2 that contains this Rect2 and b.


Operator Descriptions

bool operator != ( Rect2 right )

Returns true if the rectangles are not equal.

Note: Due to floating-point precision errors, consider using is_equal_approx instead, which is more reliable.


Rect2 operator * ( Transform2D right )

Inversely transforms (multiplies) the Rect2 by the given Transform2D transformation matrix.


bool operator == ( Rect2 right )

Returns true if the rectangles are exactly equal.

Note: Due to floating-point precision errors, consider using is_equal_approx instead, which is more reliable.