Up to date

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

Rect2

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

Description

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

For integer coordinates, use Rect2i. The 3D equivalent to Rect2 is AABB.

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

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

Note

There are notable differences when using this API with C#. See C# API differences to GDScript for more information.

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)

The ending point. This is usually the bottom-right corner of the rectangle, and is equivalent to position + size. Setting this point affects the size.


Vector2 position = Vector2(0, 0)

The origin point. This is usually the top-left corner of the rectangle.


Vector2 size = Vector2(0, 0)

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

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


Constructor Descriptions

Rect2 Rect2 ( )

Constructs a Rect2 with its position and size set to Vector2.ZERO.


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 setting its position to (x, y), and its size to (width, height).


Method Descriptions

Rect2 abs ( ) const

Returns a Rect2 equivalent to this rectangle, with its width and height modified to be non-negative values, and with its position being the top-left corner of the rectangle.

var rect = Rect2(25, 25, -100, -50)
var absolute = rect.abs() # absolute is Rect2(-75, -25, 100, 50)

Note: It's recommended to use this method when size is negative, as most other methods in Godot assume that the position is the top-left corner, and the end is the bottom-right corner.


bool encloses ( Rect2 b ) const

Returns true if this rectangle completely encloses the b rectangle.


Rect2 expand ( Vector2 to ) const

Returns a copy of this rectangle expanded to align the edges with the given to point, if necessary.

var rect = Rect2(0, 0, 5, 2)

rect = rect.expand(Vector2(10, 0)) # rect is Rect2(0, 0, 10, 2)
rect = rect.expand(Vector2(-5, 5)) # rect is Rect2(-5, 0, 10, 5)

float get_area ( ) const

Returns the rectangle's area. This is e