godot: Physic Body Instanced scene doesn't use CollisionShapes

When instancing a PhysicBody2D node (don’t know if this is true for 3d as well) the new CollisionShape or CollisionPolygon nodes added as childs of the instanced scene are not used for the collisions.

This is the hierachy:

main_node
    - other nodes
    - instanced RigidBody2D
        - ... (other child nodes saved inside RigidBody2D)
        - CollisionShape2D (new node added to the instanced scene, does not work)

I think this is useful in a lot of ways, but it also follows the whole idea of “instanced scenes”. Besides if it works with other nodes, why not with collision shapes?

Here’s a test project: https://dl.dropboxusercontent.com/u/274954519/instance_scene_collision_test.zip

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 1
  • Comments: 17 (15 by maintainers)

Commits related to this issue

Most upvoted comments

Well, this is really heavy hack I’d say… I think it is better to patch engine than do this…

On Sat, Nov 19, 2016 at 6:12 PM, Bojidar Marinov notifications@github.com wrote:

Ahah, finally was able to workaround this in a game of mine:

extends CollisionObject2D func _ready(): for shape in get_children(): if shape.has_meta(“__registered”) and shape.get_meta(“__registered”): continue

    get_tree().set_editor_hint(true)

    remove_child(shape) # Make it pick up the editor hint
    add_child(shape)

    get_tree().set_editor_hint(false) # Unset quickly

    if shape extends CollisionShape2D: # Now update parent is working, so just change the shape
        shape.set_shape(shape.get_shape())
    elif shape extends CollisionPolygon2D:
        shape.set_polygon(shape.get_polygon())

    remove_child(shape) # Reset its editor hint cache, just in case it was needed.. (you might drop this part if it bottlenecks)
    add_child(shape)

    shape.set_meta("__registered", true)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/godotengine/godot/issues/2314#issuecomment-261719371, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAX0ysLkBxkFXhyphLFqA3ZL2BlueM6ks5q_xHfgaJpZM4Fhh8N .

This problem is due to how godot handles collision shapes. CollisionShape is just helper node which does add_shape your shapes (resources you create in CollisionShape) and CollisionShape doesn’t exist during runtime. The problem is that CollisionShape node will add shapes to their ascender on the current scene tree, and if none, they will be just removed. To make this work, just add your shapes by hand to the collision body. Or setup collision body and CollisionShapes children of it in the same scene.