Topic was automatically imported from the old Question2Answer platform.
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):
bod=RigidBody2D.new()
bod.position=event.position
var shap=ConvexPolygonShape2D.new()
var vpool=PoolVector2Array()
vpool.append(Vector2(20,70))
vpool.append(Vector2(80,70))
vpool.append(Vector2(50,20))
shap.set_point_cloud(vpool)
var ownerID=bod.create_shape_owner(bod) #bod twice?
bod.shape_owner_add_shape(ownerID,shap)
add_child(bod)
print (bod.position)
print (bod.visible)
Hello,
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
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):
bod=RigidBody2D.new()
bod.position=event.position
var collision = CollisionShape2D.new()
bod.add_child(collision)
var shape = RectangleShape2D.new()
shape.extents = Vector2(50, 10)
collision.shape = shape
add_child(bod)