How to dynamically set up a DampedSpringJoint2D?

Godot Version


How to dynamically set up a DampedSpringJoint2D ?

I’m trying to configure a DampedSpringJoint2D (hook) between a PhysicsBody2D (my player character) and a RigidBody2D. Through the editor, it pretty easy to set up and working fine, I set the length + rotation to fit the center of my PhysicsBody2D and the RigidBody2D. However, when I’m trying to do dynamically, here when the player enters the Area2D of my PhysicsBody2D, the behavior is weird. I tested multiple things, with global_position, normalized or un-normalized positions, sometimes with better results, sometimes not, but never really close to what I get when using the editor. Any ideas ? Here is the relevant code, where I try to set my Hook (DampedSpringJoint2D) to RigidBody2D (body):

func _on_grappling_hook_range_body_entered(body):
  if body != self:
    Hook.rotation = (body.position.normalized() -         position.normalized()).angle()
    Hook.length = position.distance_to(body.position)
    Hook.node_a = get_path()
    Hook.node_b = body.get_path()