0 votes

Does the addshape 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 addchild works for both collisionShapes and regular node objects.

in Engine by (271 points)

1 Answer

0 votes
Best answer

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

by (28,833 points)
selected by

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

)

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.

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.

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 Frequently asked questions and 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.