godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.16k stars 97 forks source link

Add a PolylineShape2D resource for 2D collision detection #10581

Open vbettaque opened 2 months ago

vbettaque commented 2 months ago

Describe the project you are working on

A Pokemon Ranger-like game where the player leaves a trail behind to encircle enemies. The trail is destroyed/damaged if enemies collide with it.

Describe the problem or limitation you are having in your project

It is not straightforward to detect collision with a polyline composed of multiple (possibly intersecting) line segments. There are multiple options, which all fall short in some regard:

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

I propose to add a new subresource of Shape2D which represents a single polyline and would therefore be able to resolve the aforementioned problems in an elegant way. Such a resource is also present in other engines, e.g. Rapier.

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

PolylineShape2D would be very similar to SegmentShape2D, but would accept a PackedVector2Array instead of just two points. The required physics/collision detection is also straightforward to implement due to the the polyline being composed of individual segments, for which these routines already exist in the engine.

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

As mentioned before, it is technically possible to work around these limitations, but not in a few lines of script and not efficiently.

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

It is not possible to extend physics engine behavior through an external add-on.

AThousandShips commented 2 months ago

This would just be ConcavePolygonShape2D but not closed, as that shape has no interior but only checks against the segments

vbettaque commented 2 months ago

Precisely. I was considering proposing the addition of a flag to have ConcavePolygonShape2D be non-closed as an alternative, but it would not be a polygon anymore so I didn't find it fitting.

vbettaque commented 2 months ago

Okay after reading more about it, I guess ConcavePolygonShape2d does look like what I want (except that its individual segments do not necessarily have to be connected it seems?). But I'd argue that the name is a misnomer then, which is why I didn't find it earlier. Also Godot give you a warning if you try to use ConcavePolygonShape2d outside CollisionPolygon2D (this is not mentioned in the documentation), so clearly the direct usage is not encouraged.