Attention: Here be dragons
This is the
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
A 2D axis-aligned bounding box using floating-point coordinates.
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.
There are notable differences when using this API with C#. See C# API 与 GDScript 的差异 for more information.
Rect2 ( )
abs ( ) const
get_area ( ) const
get_center ( ) const
has_area ( ) const
is_finite ( ) const
Vector2 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.
Vector2 position =
The origin point. This is usually the top-left corner of the rectangle.
Vector2 size =
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.
Rect2 Rect2 ( )
Constructs a Rect2 as a copy of the given Rect2.
Constructs a Rect2 from a Rect2i.
Constructs a Rect2 by
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)
var rect = new Rect2(25, 25, -100, -50); var absolute = rect.Abs(); // absolute is Rect2(-75, -25, 100, 50)
true if this rectangle completely encloses the
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(