Random2D

Inherits: Random < RandomNumberGenerator < Reference < Object

Randomization methods in 2D.

Description

A singleton which provides various randomization methods relating to 2D space.

Note: Random2D inherits all the methods from Random, but has a separate global instance and is treated separately from the base one. This means that the seed and the state are not shared between instances by default.

Properties

Vector2 direction Vector2( 1, 0 )
float rotation 6.28319

Methods

Vector2 point_in_circle ( float radius_min=0.0, float radius_max=1.0 )
Variant point_in_polygon ( Variant polygon, int point_count=1 )
Vector2 point_in_region ( Rect2 region )
Vector2 point_in_triangle ( PoolVector2Array triangle )

Property Descriptions

Default Vector2( 1, 0 )
Getter get_direction()

A random normalized direction. Equivalent to point_in_circle with minimum and maximum radiuses set to unit length of 1.0 and is slightly more efficient. The unit vector can be multiplied by a scalar value.

var radius = 64.0
var impulse = Random2D.direction * radius

Default 6.28319
Getter get_rotation()

A random rotation in radians. Ranges from 0.0 to @GDScript.TAU.

Method Descriptions

Generates a random point uniformly distributed on the circle’s boundary, within the circle’s area, or the area confined by inner and outer circle ranges specified with radius_min and radius_max parameters.

By default, generates points inside unit circle with radius 1.0. If you need random normalized vectors, use direction instead, which is more efficient to compute.

If radius_min == 0, generates points inside a unit circle, such that Geometry.is_point_in_circle shall return true given the same radius.

If radius_min != radius_max, generates points within the ring’s area, such that the inner area defined by radius_min remains unaffected.

If radius_min == radius_max, generates points exactly on the circle’s boundary, but do note that a point may slightly deviate from the actual circle’s boundary due to floating point error accumulation, so Geometry.is_point_in_circle may occasionally return false.

var point: Vector2
point = Random2D.point_in_circle(1.0, 1.0) # Unit vector.
point = Random2D.point_in_circle(0.0, 1.0) # Inside a circle.
point = Random2D.point_in_circle(0.5, 1.0) # Within a ring.

The parameters are not restricted to unit length of 1.0.


Generates a random point distributed within polygon’s area. The distribution may not be completely uniform, but should be good enough in most cases. If point_count == 1, returns a single Vector2 point. If point_count > 1, returns a PoolVector2Array of points, which can be cached for further usage. This improves performance significantly as the polygon must be triangulated first.

This works by first decomposing polygons into triangles with PolyDecomp2D.decompose_polygons. For approximately uniform distribution, each triangle is weighted by its area to fetch the needed number of points. Random points are then generated with point_in_triangle uniformly.

Polygons may consist of outer and inner polygons (holes), so the polygon parameter also accepts an Array of PoolVector2Arrays as input.

The quality of distribution works better for single polygons with arbitrary number of holes, so it’s recommended to use this method on distinct, non-overlapping objects.


Generates a random point in the area specified by top-left and bottom-right corners of a Rect2.

var rect := Rect2(-100, -100, 100, 100) # Position and size.
var point = Random2D.point_in_region(rect)

Generates a random point uniformly distributed within triangle’s area.