Attention | Topic was automatically imported from the old Question2Answer platform. | |
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.