0 votes

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.

in Engine by (14 points)

1 Answer

0 votes

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.

by (12,471 points)

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: https://godotengine.org/qa/61429/how-to-attach-a-collisionshape-to-a-draw-primitive.

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.