+1 vote

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.

Godot version 3.2.3
in Engine by (85 points)
edited by

1 Answer

+2 votes
Best answer

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

by (654 points)
selected by

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.

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 https://godotengine.org/qa/109572/how-to-keep-a-rigid-body-still-while-attaching-a-joint), but the principle remained the same; detach the joint, and then re-attach it at the next physics processing step.

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 Frequently asked questions and 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 [email protected] with your username.