How to add a collisionshape (shape: CircleShape2D) to a draw primitive?

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

Hi. I am new to Godot and trying to make the engine being able to detect collsion between circle and my polygon2D. I have this code which randomly draws a 2d circle using draw_arc function:

#Todo: How to attach collision shape to a draw primitive

extends Node2D

# Called when the node enters the scene tree for the first time.
func _ready():
	randomize()

func _draw():
	var smaller: int = -1
	if get_viewport().size.x <= get_viewport().size.y:
		smaller = get_viewport().size.x
	else:
		smaller = get_viewport().size.y
	var radius = randi() % (smaller / 2)
	var x = randi() % ((get_viewport().size.x as int)-radius)
	var y = randi() % ((get_viewport().size.y as int)-radius)
	var shape = CircleShape2D.new()
	shape.radius = radius
	var coll_shape = CollisionShape2D.new()
	coll_shape.set_shape(shape)
	draw_arc(Vector2(x,y), radius, 0, 360, 1000, Color(1.0,0,0,1.0), 1)

I have created the shape and associated it with a collision shape 2d. But how do I add it to the circle drawn in the last line? The draw_arc function does not return any object or body to attach to it.

The above script is for my root node (Node2D).

Any Help is highly appreciated. Thanks.

:bust_in_silhouette: Reply From: jgodfrey

I assume you’re trying to determine what circle a triggered CollisonShape is associated with?

I think you’ll have to “connect” them in your own data structure. For example, you could create a dictionary that associates each CollisionShape2D object with some data about the associated circle (center point, radius, …).

Then, when a specific CollisionShape is triggered, you could find the associated circle in your dictionary.

Hi. I see.

I thought there were a Godot way of doing it. You suggestion should definitely work.

BTW: I posted a similar post and thought i missed to confirm the posting so I wrote this thread. Please disregard the other threa: How to attach a collisionshape to a draw primitive? - Archive - Godot Forum.

I do not know how to remove a thread. Apologise for any inconvieniences.

CGAdventurer | 2020-02-15 19:17