Removing and re-adding a joint child detaches the joint

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

My question builds off of one of the answers to this question:
I wanted to update the rest position (i.e. the position where the two objects are attached) of a PinJoint2D on moving one of the objects it’s attached to. To do so, I tried out Thomas’s answer to the linked question.

Flipping disable_collision twice didn’t affect the rest position, presumably because the value is still the same at the end of the frame being processed. Instead, I tried out calling node.remove_child(joint) and then node.add_child(joint) to try and get it to update. This caused the joint to detach entirely. On viewing the node in the debugger at a future frame, I can see that the joint’s still a child of the node, and it still has the same node_a and node_b paths.

Why would resetting the joint’s parental hierarchy cause it to detach, and how can I prevent it? It would make sense if the node no longer had the same relative path to either node_a or node_b, but it’s been re-parented to the same parent in this case.

:bust_in_silhouette: Reply From: andersmmg

I think I see what you mean, I’ve done something like this. The simplest way I’ve found that works is to save the position, move it to the position you want, connect it to the joint, and set the position back to where it was if you want. The position of the object at the time it’s connected determines where it’s connected, so you can just move it prior to connecting it

That was the workaround I had ended up going with, but I’m attaching a rigid body with another joint stuck to it. The actual joint pin placement happens after the bodies’ engine-side physics are applied, so the joint misaligns, instead placing at where the rigid body finished moving as a result of other forces. I had gotten around that for the initial setup by setting both objects to the same velocity, but I can’t do that if there’s another joint attached to the object.
My first thought was to somehow disable the physics, but setting the rigid body to sleeping or static mode caused the joint to not set up the position until the mode changed back or the sleeping ended. I’m still working through that one.

GreasyMcBeef | 2021-07-03 06:57

I got a workable solution out of this, and wanted to leave an addendum here for later reader reference before I accept the answer. I ended up having to move all of the handling for this specific joint to the physics server because in my specific configuration, other joints were pulling on one of the objects I was trying to attach (see How to keep a Rigid Body still while attaching a joint - Archive - Godot Forum), but the principle remained the same; detach the joint, and then re-attach it at the next physics processing step.

GreasyMcBeef | 2021-07-14 23:41