Child Rigidbody Colliding with Parent Rigidbody

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By axii

My goal for having a Rigidbody parented to another Rigidbody is to create a ‘slime’ block, where once the slime has touched another rigidbody, it sticks to it, and to achieve this I have tried all of the joints (to little success) and have now tried reparenting the piece to stick to the slime rigidbody. However, this causes the rigidbody from the slime (parent) and the pieces it’s stuck to (children) to collide with eachother and causes unexpected behavior. I have tried messing with layers so that the children and the parents don’t collide with each other, but this causes the children to fall through the floor. I can’t increase the collision of the whole object because the parent and child relationship is dynamically created, and if somehow I managed to resize it to the whole object, it won’t be accurate to the whole shape of the object which is important.

Is there any way I can fix this parent child problem?

Here is my code for the slime block:

for area in base_piece.check_piece.get_overlapping_areas():
	if not area.is_in_group("Piece"):
		continue

	var piece: Piece = area.get_parent()

	piece.reparent(base_piece, true)

	piece.freeze = true
:bust_in_silhouette: Reply From: Zylann

Parenting a rigidbody under another is not going to work correctly if both rigidbodies are in a mode that requires them to be simulated by the laws of physics.
It’s equivalent to manually setting the position of the child everytime the parent moves, which is completely against what rigid bodies are for. So it will cause unexpected behavior…

For making a sticking mechanic, you would indeed have to use joints. It’s the most idiomatic solution to use in terms of physics engines. I don’t know how good joints are in Godot though (the physics engine is no longer the more battle-tested Bullet Physics, instead it’s back to GodotPhysics which is less good).

Another thing you could do is change the slime’s body to become kinematic/frozen, and turn off its collisions, so it can live under the new parent without bothering physics. But if the presence of that child needs to affect physics of the parent, you’d have to do something in addition to that, maybe add a copy of the child’s disabled collision shape as child of the parent, so that the engine still deals with a single rigidbody, but with multiple collision shapes?

It’s one of the cases where the node system has to be messed with a bit, or even broken down, as not every use case matches this kind of structure, especially when it comes to physics. And it’s indeed a pain to do so, but in some of my physics-based prototypes I had to give up the “scene-tree == game-structure” to get what I wanted… it involved keeping track of the game’s structure in a custom way in script variables instead of coupling it with how the tree looks like.