Add methods to generate random Vector2
/Vector3
points with uniform distribution in a circle/sphere
#1731
Labels
Vector2
/Vector3
points with uniform distribution in a circle/sphere
#1731
Describe the project you are working on:
Goost - Godot Engine extension
See original feature request at godotengine/godot#34301.
See other related issues: godotengine/godot#7199, godotengine/godot#28264.
Describe the problem or limitation you are having in your project:
The issue is mainly with
rand_range()
, if you want to generate a bunch of points within a circle (not a square) with a proper uniform distribution rather than normal distribution, without even having to normalize the vectors.Snippet from godotengine/godot#34301 (comment):
The same applies to
Vector3
.Describe the feature / enhancement and how it helps to overcome the problem or limitation:
This can be added to
Geometry
singleton (Geometry2D
andGeometry3D
in Godot 4.0 now) orRandomNumberGenerator
.For me, this is mainly quite useful for:
camera.offset
.Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:
For 2D, see godotengine/godot#43103, or less complex alternative godotengine/godot#43343
Also, I've recently implemented a bunch of methods for this in goostengine/goost#28, which mostly rely on
Math::random()
functionality:I've actually added those toInGoostGeometry2D
singleton which is equivalent toGeometry
singleton.Random2D
singleton now.If this enhancement will not be used often, can it be worked around with a few lines of script?:
Sure. Here's a minimal implementation in GDScript for
rand_point_in_circle()
:But if you need to generate thousands of points, you'll hit performance bottleneck with GDScript.
Geometry.is_point_in_circle()
could be used to help this on the script level by filtering out points which lie outside of a circle, but the distribution won't be uniform anymore.godotengine/godot#31667 can also be easily worked around via script, but nonetheless it's in the engine.
Is there a reason why this should be core and not an add-on in the asset library?:
There's no reason to bloat Godot, but perhaps we could come up with some minimal core functionality which could solve most use cases. Other engines like Unity or Unreal Engine provide this out of the box, though:
The text was updated successfully, but these errors were encountered: