godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.12k stars 69 forks source link

Add a get_random_position&is_position_in_area method for Area2D/Area3D #10599

Open CsloudX opened 3 weeks ago

CsloudX commented 3 weeks ago

Describe the project you are working on

simulation game

Describe the problem or limitation you are having in your project

I want random generate some thing in a Area2D and check something like camera was in Area. when the shape was only rect and each collision shape not overlay each other, the code was simple. but when the Area2D's shape was complex, I don't know how the coding. so I found help to Godot developers (^V^). image

I'm serach for google ,found other people need this method too. image So, i think it was very useful method.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

if has this method, it deal the complex thing, for user, it was so simple the get a valid position in a area. for example, when user in a area, generate bomb random in the area.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

for Area2D/Area3D:

### Tasks
- [ ] add method `get_random_position()->Vector2/3`
- [ ] add method `is_position_in_area(pos:Vector2/3)->bool`

If this enhancement will not be used often, can it be worked around with a few lines of script?

no, i can't.

Is there a reason why this should be core and not an add-on in the asset library?

IMO, it was basic method, should be core.

KoBeWi commented 3 weeks ago

Sounds like something that should be in Shape2D, not Area2D.

CsloudX commented 3 weeks ago

@KoBeWi IMO, it should be Area, not Shape. because a Area can have more than one Shapes. image like this. if method only have on Shape. for method get_random_position, we need user to deal the overlay area.

but, i think it was a good idea Area&Shape both have this methods.

smix8 commented 3 weeks ago

I think this complelty underestimates how complex and performance problematic this random point picking on an Area node would be.

As soon as you have multiple shapes or more complex shapes than a rect or circle, you not only need to merge them to remove the surface overlap, but you also need to map them for their surface area in order to have uniformly random point picking.

None of the required information or functionality exists for physics shapes because the physics system does not need it.

Forget the naive cases with just 1-2 shapes that can be solved with 1-2 lines of script, such a system needs to work with x-shapes or not be added at all and that is where you will run into many problems.

timothyqiu commented 3 weeks ago

For is_position_in_area(), you might be interested in Shape2D.collide() which returns whether a shape collides with another. You can test that with a 1x1 RectangleShape2D I think.