+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 (673 points)

How do you move the rigid bodies?

Remember that games do not need perfect collision shapes, the more complex are, the more resources they need, you can make those ships with a couple of simple shapes.

And from a design point of view, hitboxes should favor the player in most cases, so, the simpler and inaccurate, the better for the player experience too.


Also, RigidBodies do not work too well with some concave shapes, maybe you are facing that issue too.

Inside of _process I check for input and apply an impulse if needed. What do you mean by inaccurate hitboxes being better? I don't think it's related to performance issues as I only have 2 or 3 hitboxes, however it seems really weird that the polygon shows 0 vertices in the remote inspector but is visually appearing in-game.

Just tested: if I draw a polygon in the editor collisions work fine, but trying to instance breaks the collisions. This is my polygon instancing code, which is succeeding in finding the right scene file, instancing a node, and creating a Polygon2D, but that polygon appears to have 0 vertices upon inspection. If I print the vertices from the code print(get_polygon()) a bunch of vertices are shown. Is this a Godot bug?

func set_texture(tex):
    .set_texture(tex)
    var tex = get_texture().get_name().split(".")[0] #remove ext from name
    var collider = get_polygon_scene(tex)
    get_parent().call_deferred("add_child",collider.instance())

Just checked and, strangely enough, the polygon that I drew in the editor and works fine also has a "Polygon" property with an "Array2" of size 0 vertices. So that seems to be a red herring.

OK, so if I create a polygon2d in the editor, and dynamically change the "Polygon" property's verticies thru code get_node("../CollisionPolygon2D").set_polygon(collider.instance().get_polygon()), the original polygon is still used in game (seemingly unmodified) but collisions act as if nothing was changed. This seems to be a godot bug, so I'm going to submit an issue once I can replicate.

It appears to be an issues with my polygon's xml file. Despite appearing correctly with the visible shapes something is broken with the instanced scene to make collisions not work. I created a new polygon via the editor overlayed on top of the broken polygon and it appears to be working. Is there any way to save this polygon's xml to compare (save icon is grayed out)? It seems like Godot should probably be able to find the issue in my xml and alert me when instancing.

Figured that out (saved as scene instead of resource and posted the two polygons to github

The issue was with my polygon scene. I had created it using physics editor - https://www.codeandweb.com/physicseditor - combined with a godot exporter but it seems to be causing trouble. Instead I re-created the polygon using the godot editor (by overlaying it on top of the sprite node and then deleting the sprite) and saved it as its own scene, which now works perfectly. Thanks @eons!

Edit: This is still not working when I try to replicate it, turns out that the reason i thought it was working is not right. here's a copy of a simple project that replicates the problem - https://ufile.io/h9lm2

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.