how to debugdraw CollisionObjects/shapes created by code

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

In my learning process I tried to make a script that creates some RigidBody2Dby pure GDScript. But I could not figure out how to make the shapes visible (though the “debugDraw shapes” is enabled). The SaticBody I created via the editor is visible and the contact points too but not the shapes I created by code.
the code:

extends Node2D

var bod:RigidBody2D

func _ready():
pass # Replace with function body.

func _process(delta):
if bod!=null:
	print (bod.position) # the body moves
func _input(event):
if (event.is_pressed() and event.button_index == BUTTON_LEFT):
	var vpool=PoolVector2Array()
	var ownerID=bod.create_shape_owner(bod) #bod twice?
	print (bod.position)
	print (bod.visible)

a picture:
where is my triangle?


:bust_in_silhouette: Reply From: Alidro

as far as i know the RigidBody2D does not have any visible part on its own, it has to be used with a sprite, or similar, to be visible. For that you would just have to create a sprite, give it a texture, and append it to the RigidBody2D.

Ok but when I create a RigidBody2D in the editor (using CollisionShape2D) the shapes are visible when “Debug>Visible collision Shapes” is Set. But in the docs it’s said that I should not use CollisionShape2D to proceduraly generate shapes but instead use a shape_owner. SO the shapes can be drawn by the engine (without any texture) when created from the editor. My question is how to show this when generating a CollisionObject2D by code. If the editor can do it I suppose I can do it using GDScript…

abakobo | 2019-05-13 14:14

I understand what you mean, now unfortunately i dont have a solution for that since i never used this way of implementing a collision. What i noticed though, is that for some reason if you spam-click, you can see the points, and they are weirdly moving. Maybe you can interpret this in some useful way

Alidro | 2019-05-13 15:13

:bust_in_silhouette: Reply From: abakobo

Well it looks like using collisionshape2D actually works though it’s documented that it should be used in the editor only. And doing things this way shows the shapes when the the “view collision shapes” debug option is enabled.

if (event.is_pressed() and event.button_index == BUTTON_RIGHT):
	var collision =
	var shape =
	shape.extents = Vector2(50, 10)
	collision.shape = shape