+1 vote

Having a strange problem where RigidBody2D's are not colliding. I have CollisionPolygon2D scenes that are instanced as children of the bodies and work fine in the editor, but when playing the overall scene the polygons do not have any vertices in the remote inspector (despite "visible collision shapes" showing them as visible. The layers and masks of all the bodies are the same, the 0th bit is checked on them all. If I use a collisionShape2D instead of the instanced polygon collisions work fine.

enter image description here

enter image description here

in Engine by (670 points)

Possible issues here:

Using _process in CollisionObjects won't work right, they need to use _fixed_process to respond as you expect.

Polygon shape is a resource used by the CollisionPolygon helper (who creates the shape) and the CollisionObject, if you change the polygon on the editor helper, the object will have reference to the old shape outside the editor, you need to set it on the object and on the helper only if you want visual debug information.

I uploaded working and non working sample projects on the latest github comment. This issue arises even for bodies that move by gravity alone, but process/fp is good to know. I've tried changing and loading the CollisionPolygon multiple different ways and nothing seems to be reliably working here. I'm not sure I really understand what you mean by that last part?

I think that what's happening is that instancing a CollisionPolygon2D somehow makes it not work, but if I draw it directly on the body node's scene it works. is that what you mean?

Ok here's what I've learned (copied from GitHub). Is this what you meant? Because this seems like bad design if this is somehow intentional.

Ok I think I've figured out how it's working in one project and not
the other, but IDK why.

Create a new scene with a CollisionPolygon2D as root, draw something,
save it as polygon.tscn.

Create a new scene, make root RigidBody2D and save as body.tscn. Add a
Sprite child and load its texture. Attach a script to Sprite, and in
ready call getparent().calldeferred("addchild",load("res://polygon.tscn").instance()).

Create a main scene and load multiple bodies. Set "Visible Collision
Shapes" to ON. Run. Shapes are visible, but collisions don't work.

Go back to body.tscn. Clear script on the sprite. Add a
CollisionPolygon2D child to body, draw a shape, save+run (collisions work), stop the run, delete
polygon, save, re-attach sprite script. Run scene again, collisions
use deleted polygon but "Visible Collision Shapes" show the instanced
polygon.

Start with something simple, try to do it with CircleShape2D.

If your body does not have shape or need a new shape, look how changing the shape on the helper wont change the shape on the body (the body keeps reference to the original), you need to change the shape on the body and use a helper only if you need some visuals (you can always _draw on the body too).

More complex shapes need to be built, CollisionPolygon2D only stores a Vector2Array with points, not the Shape2D.


That is the case of changing shapes, if you modify the shape and body and helper have reference to the same object so you will notice changes on both (not the case of polygons).

ps: All this may change on Godot 3 but I don't know how.

Please log in or register to answer this question.

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 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.