How to close a CollisionPolygon2D?

Godot Version

4.3.dev4

Question

Is there a way to close a CollisionPolygon2D much like how you can close a Line2D? I need this functionality for a procedurally generated collision shape. I want to prevent the player from being able to leave the area enclosed by the CollisionShape2D but there’s a loop hole!

Godot_v4.3-dev4_win64_hre8pohdvV

This closes the line but it creates two polygons…

			var poly_line := Geometry2D.offset_polyline(
				polygon_points.keys(), 
				5,
				Geometry2D.JOIN_SQUARE, 
				Geometry2D.END_JOINED,
			)

Try drawing the polygon as usual and change its CollisionPolygon2D.build_mode to CollisionPolygon2D.BUILD_SEGMENTS

Hmmm that doesn’t appear to lead to changes…
vViQTolDYp

Do you mean like so?

var poly_line := Geometry2D.offset_polyline(
	polygon_points.keys(), 
	5,
)
	
	
for poly: Array in poly_line:	
	var collision_polygon := CollisionPolygon2D.new()
	collision_polygon.build_mode = CollisionPolygon2D.BUILD_SEGMENTS
	collision_polygon.polygon = poly

Don’t use a polyline, just draw your polygon normally as a closed one.

1 Like

Thank you very much for your help

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.