godotengine / godot-proposals

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

Add concave hull algorithm and support generating concave polygon from Sprite2D #10267

Open beicause opened 1 month ago

beicause commented 1 month ago

Describe the project you are working on

A 2D game

Describe the problem or limitation you are having in your project

For discontinuous (with gaps) images, currently in editor Sprite2D Convert to Polygon2D or CollisionPolygon2D can only generate multiple polygons.But sometimes we want to treat the image as a single collision shape, Although it can generate one polygon in some cases by adjusty epsilon, the result maybe different from concave hull. Geometry2D has convex_hull method, which is a subset of concave hull, not as useful as it.

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

After adding concave hull algorithm to BitMap and Geometry2D and supporting it in Sprite2d editor plugin, we can generate a single concave polygon from Sprite2D which is easier to use, instead of multiple CollisionPolygon2D.

These are multiple CollisionPolygon2D generated by BitMap::clip_opaque_to_polygons:

The CollisionPolygon2D generated by convex hull:

The CollisionPolygon2D created by concave hull should be like these( generated using concaveman-cpp without simplifying point number ): very low concavity threshold:

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

The concave hull algorithm can refer to https://github.com/mapbox/concaveman and its cpp implementation, or other better algorithms. A example implementation of this proposal: Add method "opaque_to_concave_polygon" in BitMap. Add method "concave_hull" in Geometry2D. Add a option in sprite2d editor to enable concave polygon when converting Sprite2D to polygon.

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

This can be a addon and written in script, but it's better if provided by Godot

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

Sprite2D editor plugin, BitMap and Geometry2D are core

fire commented 1 month ago

@lawnjelly Do you remember if clipper or such already does this?

lawnjelly commented 1 month ago

No idea on clipper.

Bear in mind afaik concave collisions are slow and can be prone to other physics problems, often it can be better to decompose into several convex hulls (or collide against the bitmap directly).

beicause commented 1 month ago

ConvexPolygonShape2D can also be concave without using a concave collision, but I don't know if it's possible to generate something like these: