Welcome to the forums @xtreme!
I haven't tried to tackle a problem like this myself in 2D, but in 3D for Voxels I found the easiest way to handle something like this was to make a ConvexPolygonShape and set the shape using the verticies of the voxel mesh. I think you may be able to do something similar with the ConvexPolygonShape2D class, by writing an algorithm that takes two points and makes a rectangle between the points.
Something like this might work:
var collision_body
var clone_points = PoolVector2Array()
func _ready():
collision_body = get_node("StaticBody2D")
# call this when the user is done drawing a line
func generate_shapes_for_line(line:Line2D):
var point_count = line.get_point_count()
var clone_shape = CollisionShape2D.new()
var clone_polygon = ConvexPolygonShape2D.new()
clone_shape.shape = clone_polygon
var clone_points = PoolVector2Array()
for i in range(0, point_count):
# can we get the next point?
if (i+1 < point_count):
make_collision_rectangle(get_point_position(i), get_point_position(i+1))
clone_polygon.points = clone_points
collision_body.add_node(clone_shape)
func make_collision_rectangle(point_1, point_2):
var point_dir = (point_2 - point_1).normalized()
var point_dir_rot = point_dir.rotated(PI * 0.5)
# to make the rectangle different sizes, change the value multiplied
point_dir_rot *= 8 # will be 8 pixels deep
# first triangle
clone_points.append(point_1)
clone_points.append(point_2)
clone_points.append(point_1 + point_dir_rot)
# second triangle
clone_points.append(point_1 + point_dir_rot)
clone_points.append(point_2)
clone_points.append(point_2 + point_dir_rot)
But I'm not sure and it would probably need quite a bit of testing. I'm also not positive I have the triangle winding order correct (I think they are both clockwise though)
Another solution might be using several SegmentShape2D shapes, one for each pair of points in the Line2D. This might not be as performant, since it would require several nodes, but it might be the easiest way to get collision working quickly.
The code I think would look something like this:
var collision_body
func _ready():
collision_body = get_node("StaticBody2D")
# call this when the user is done drawing a line
func generate_shapes_for_line(line:Line2D):
var point_count = line.get_point_count()
for i in range(0, point_count):
# can we get the next point?
if (i+1 < point_count):
var clone_shape = CollisionShape2D.new()
var clone_segment = SegmentShape2D.new()
clone_segment.a = line.get_point_position(i)
clone_segment.b = line.get_point_position(i+1)
clone_shape.shape = clone_segment
collision_body.add_node(clone_shape)