How do I have line2D drawn on screen to collide with other objects?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ohpoloha

Hi community,
I’m completely new to godot. I wanted to build a game similar to Happy Glass (https://play.google.com/store/apps/details?id=com.game5mobile.lineandwater&hl=en_US&gl=US) which player can draw lines on the screen and existing objects would be able to collide/interact with the lines.

I’ve came to the position where I can draw lines on screen now with the script below:

func _input(event):
	if event is InputEventMouseButton:
		_pressed = event.pressed
		
		if _pressed:
			_current_line = Line2D.new()
			_lines.add_child(_current_line)
		
	if event is InputEventMouseMotion && _pressed:
		_current_line.add_point(event.position)
		linePoints.append(event.position)
	
	if event is InputEventMouseButton && !_pressed:
		addCollisionShape(linePoints)
		linePoints.clear()

However, my other objects are not interacting with the lines. I know I need to add CollisionBody or something but I can’t figure out how.

Thanks so much

:bust_in_silhouette: Reply From: kidscancode

You need to iterate through the points of the Line2D and add SegmentShape2D collisions to each pair of points.

Here’s an example:
https://kidscancode.org/godot_recipes/3.x/2d/line_collision/index.html

Thank you! This works but I need to figure out a way to make it apply the collision while I’m drawing. currently only works after I’m done drawing.

ohpoloha | 2023-06-28 18:46