RigidBody2D's not colliding

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

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

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.

eons | 2017-05-24 00:06

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())

jarlowrey | 2017-05-24 14:25

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.

jarlowrey | 2017-05-24 14:31

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.

jarlowrey | 2017-05-24 14:35

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.

jarlowrey | 2017-05-24 14:57

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

jarlowrey | 2017-05-24 15:02

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

jarlowrey | 2017-05-24 15:16

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.

eons | 2017-05-24 17:54

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?

jarlowrey | 2017-05-24 17:58

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?

jarlowrey | 2017-05-24 18:02

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 get_parent().call_deferred(“add_child”,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.

jarlowrey | 2017-05-24 18:17

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.

eons | 2017-05-24 20:04