add_Shape function

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By vonflyhighace2
:warning: Old Version Published before Godot 3 was released.

Does the add_shape function has any use? I tried using it with the collisionShape node but doesn’t seem to do anything. on the other hand the usual add_child works for both collisionShapes and regular node objects.

:bust_in_silhouette: Reply From: Zylann

This function doesn’t adds a child node, it adds a shape to the collision node, and this is the correct behaviour.
For this reason, the CollisionShape node exists as an editor helper, but in reality shapes are not nodes: http://docs.godotengine.org/en/latest/tutorials/2d/physics_introduction.html#collisionshape2d

I’m still having problems with my implementation. I’m not sure if you can spot my mistake based on the code below.

	

    #create a Area object
	var area = Area.new()
	#add the area as a child node
	self.add_child(area)
	#connect body_enter Signal to Area
	area.connect("body_enter", self, "_on_Area_body_enter")
	#create a collsionShape
	var collisionShape = CollisionShape.new()
	#add the coliision shape as child of area
	area.add_child(collisionShape)
	#configure the collsion shape
	collisionShape.set_shape(BoxShape.new())
	#adjust the shapes size
	collisionShape.get_shape().set_extents(Vector3(currentMoveSpaces * 10, 1, currentMoveSpaces * 10))
	#set the colliderShape to be a triiger
	collisionShape.set_trigger(true

)

vonflyhighace2 | 2016-09-14 23:36

I’ve also notice that while checking the remote Inspector, there seems to be a child node created under each and all CollisionShape nodes during runtime except for the object that I wish to add a CollisionShape to at runtime.

vonflyhighace2 | 2016-09-14 23:41

You don’t need a CollisionShape node at all, because that’s only used in the editor. When adding a collision shape with script you have to do it differently. This code should work:

# create an Area object
var area = Area.new()
# add the area as a child node
self.add_child(area)
# connect body_enter Signal to Area
area.connect("body_enter", self, "_on_Area_body_enter")
# create a BoxShape
var col = BoxShape.new()
# adjust the BoxShape size
col.set_extents(Vector3(currentMoveSpaces * 10, 1, currentMoveSpaces * 10))
# add BoxShape to Area object
area.add_shape(col, Transform()) # Change the Transform() if you need to change the position
# set the shape of Area to be a trigger
area.set_shape_as_trigger(0, true) # This requires the shape index, since there's only one shape it will be 0

you add a shape with area.add_shape(shape, transform), the transform can be used to change the rotation, scale, and position of the shape relative to the parent.

To set the shape as a trigger you have to call area.set_shape_as_trigger(shape_index, bool), since you’re adding only one shape the index will be 0.

Note this is the same for all CollisionObject’s, both 3D and 2D.

CowThing | 2016-09-15 00:10