Table of Contents

Class Rect2

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

Rect2

Remarks

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 Rect2.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 ZERO). Otherwise, it always evaluates to true.

See Also

Constructors

Rect2

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

Rect2 Rect2

Rect2(Rect2)

Constructs a Rect2 as a copy of the given Rect2.

Rect2 Rect2(Rect2 from)

Parameters

from Rect2

Rect2(Rect2i)

Constructs a Rect2 from a Rect2i.

Rect2 Rect2(Rect2i from)

Parameters

from Rect2i

Rect2(Vector2, Vector2)

Constructs a Rect2 by position and size.

Rect2 Rect2(Vector2 position, Vector2 size)

Parameters

position Vector2
size Vector2

Rect2(float, float, float, float)

Constructs a Rect2 by setting its position to (x, y), and its size to (width, height).

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

Parameters

x float
y float
width float
height float

Properties

end

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.

var end : Vector2 = Vector2(0, 0)

Property Value

Vector2

position

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

var position : Vector2 = Vector2(0, 0)

Property Value

Vector2

size

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.

var size : Vector2 = Vector2(0, 0)

Property Value

Vector2

Methods

abs

Qualifiers: 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.

Rect2 abs

encloses(Rect2)

Qualifiers: const

Returns true if this rectangle completely encloses the b rectangle.

bool encloses(Rect2 b)

Parameters

b Rect2

expand(Vector2)

Qualifiers: 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, 15, 5)

Rect2 expand(Vector2 to)

Parameters

to Vector2

get_area

Qualifiers: const

Returns the rectangle's area. This is equivalent to size.x * size.y. See also has_area.

float get_area

get_center

Qualifiers: const

Returns the center point of the rectangle. This is the same as position + (size / 2.0).

Vector2 get_center

get_support(Vector2)

Qualifiers: const

Returns the vertex's position of this rect that's the farthest in the given direction. This point is commonly known as the support point in collision detection algorithms.

Vector2 get_support(Vector2 direction)

Parameters

direction Vector2

grow(float)

Qualifiers: const

Returns a copy of this rectangle extended on all sides by the given amount. A negative amount shrinks the rectangle instead. See also Rect2.grow_individual and Rect2.grow_side.

var a = Rect2(4, 4, 8, 8).grow(4) # a is Rect2(0, 0, 16, 16)
var b = Rect2(0, 0, 8, 4).grow(2) # b is Rect2(-2, -2, 12, 8)

Rect2 grow(float amount)

Parameters

amount float

grow_individual(float, float, float, float)

Qualifiers: const

Returns a copy of this rectangle with its left, top, right, and bottom sides extended by the given amounts. Negative values shrink the sides, instead. See also Rect2.grow and Rect2.grow_side.

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

Parameters

left float
top float
right float
bottom float

grow_side(int, float)

Qualifiers: const

Returns a copy of this rectangle with its side extended by the given amount (see Side constants). A negative amount shrinks the rectangle, instead. See also Rect2.grow and Rect2.grow_individual.

Rect2 grow_side(int side, float amount)

Parameters

side int
amount float

has_area

Qualifiers: const

Returns true if this rectangle has positive width and height. See also get_area.

bool has_area

has_point(Vector2)

Qualifiers: const

Returns true if the rectangle contains the given point. By convention, points on the right and bottom edges are not included.

Note: This method is not reliable for Rect2 with a negative size. Use abs first to get a valid rectangle.

bool has_point(Vector2 point)

Parameters

point Vector2

intersection(Rect2)

Qualifiers: const

Returns the intersection between this rectangle and b. If the rectangles do not intersect, returns an empty Rect2.

var rect1 = Rect2(0, 0, 5, 10)
var rect2 = Rect2(2, 0, 8, 4)

var a = rect1.intersection(rect2) # a is Rect2(2, 0, 3, 4)

Note: If you only need to know whether two rectangles are overlapping, use Rect2.intersects, instead.

Rect2 intersection(Rect2 b)

Parameters

b Rect2

intersects(Rect2, bool)

Qualifiers: const

Returns true if this rectangle overlaps with the b rectangle. The edges of both rectangles are excluded, unless include_borders is true.

bool intersects(Rect2 b, bool include_borders)

Parameters

b Rect2
include_borders bool

is_equal_approx(Rect2)

Qualifiers: const

Returns true if this rectangle and rect are approximately equal, by calling Vector2.is_equal_approx on the position and the size.

bool is_equal_approx(Rect2 rect)

Parameters

rect Rect2

is_finite

Qualifiers: const

Returns true if this rectangle's values are finite, by calling is_finite on the position and the size.

bool is_finite

merge(Rect2)

Qualifiers: const

Returns a Rect2 that encloses both this rectangle and b around the edges. See also Rect2.encloses.

Rect2 merge(Rect2 b)

Parameters

b Rect2

Operators

!= (Rect2)

Returns true if the position or size of both rectangles are not equal.

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

bool != (Rect2 right)

Parameters

right Rect2

* (Transform2D)

Inversely transforms (multiplies) the Rect2 by the given Transform2D transformation matrix, under the assumption that the transformation basis is orthonormal (i.e. rotation/reflection is fine, scaling/skew is not).

rect * transform is equivalent to transform.inverse() * rect. See inverse.

For transforming by inverse of an affine transformation (e.g. with scaling) transform.affine_inverse() * rect can be used instead. See affine_inverse.

Rect2 * (Transform2D right)

Parameters

right Transform2D

== (Rect2)

Returns true if both position and size of the rectangles are exactly equal, respectively.

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

bool == (Rect2 right)

Parameters

right Rect2