Nicely done, trying to convert the code! However, there are a few mistakes that you’ve made when converting the code. Well… maybe not mistakes. It looks like you tried to convert the code to constrain the body with the data from the rope, but I don’t think that will work as you intend.
Moreover, I want to clarify that when I say anchor, I am referring to the base of the rope which follows the mouse around – not the attachment point on the ball.
How the constraint works
The constraint isn’t, and shouldn’t, be concerned with the rope you’re using to move the body around. It’s only trying to constrain the body’s distance to a specific point.
Mistake: using the rope’s attachment point instead
var rope_to_anchor = anchor_position - rope_origin_position
Your new definition of the vector that originally went from the anchor to the ball now goes from the anchor to the end of the rope. This might be fine if the end of the rope is attached to the body’s origin – but it’s not. The effect of this is a vector that doesn’t always point to the body’s origin, and which can’t represent the distance from the origin.
Remember that the vector is being used to project the velocity onto such that the constrained_velocity – which should be tangent to the constraint circle – can be derived.
This is probably the reason for:
Mistake: not using the body’s state
self.global_position = constrained_position
Change to:
state.transform.origin = constrained_position
# Transform might be an immutable struct (I can't remember).
# In that case, use code below instead:
# var newTx = state.transform
# newTx.origin = constrained_position
# state.transform = newTx
Because you are working with physics, it’s best to work with the body’s state directly.
Correction: vector projection
var projected_velocity = state.linear_velocity.project(rope_to_anchor.limit_length(rope_length + max_additional_length))
Remove the rope_to_anchor.limit_length() part. It is unnecessary. Change to:
var projected_velocity = state.linear_velocity.project(rope_to_anchor)
With all this said, I do want to emphasize that you’re working with someone else’s extension. I have no idea how CRope2D works under the hood, and so I can’t confidently recommend any solutions that will solve your current issues. That jitter sure looks annoying though.
Sorry I can’t be of any more help. Please update the post if you figure out the issue.