I have made use of others' rigid body designs for rope for some time now but cannot find a good method to add links to the rope or chain. For a game I am designing (2D platformer) I need to be able to lengthen a hose as the player vehicle moves away from a starting position. The code for the most part is working - as the Player moves away a comparison is done in code and when the hose is stretched too far a segment is duplicated and attached to the previous segment.
Now the trouble and question: I modeled my code after how I would duplicate the segments in the editor, then move the segment and attach node_b. That seemed to work but each successive segment seems to be attached at one corner where the entire length of rope (initially 20 segments) are all aligned on center. I am uncertain why the offset.
![](https://i.imgur.com/p4wrgpk.png)
Key Part of the Code: "link_players()", "calc_length()", and "add_segments()".
func link_players():
#prevents the rope from freaking out, when relocated
yield(get_tree(), "idle_frame")
pjoint = get_parent().get_node("RescueSub/Player_pinjoint")
ppos = pjoint.global_position
pjoint.node_a = $Segments.get_child(19).get_path()
player2 = get_parent().get_node("VOO")
p2pos = player2.get_node("Position2D").global_position
var p2seg = $Segments.get_child(0)
var p2joint = p2seg.get_node("PinJoint2D")
p2joint.node_b = player2.get_path()
linked = true
func calc_length():
hose_length = num_segs*9.7
func add_segments():
for chld in $Segments.get_children():
chld.mode = 1
chld.set_physics_process(false)
player = get_parent().get_node("RescueSub")
var init_vel = player.velocity
player.set_process_input(false)
player.set_physics_process(false)
player.velocity = Vector2.ZERO
var segs_list = $Segments.get_children()
var last_seg = segs_list[num_segs-1]
var seg_dup = last_seg.duplicate(15)
print("did we pause")
seg_dup.rotate(180)
seg_dup.set_physics_process(false)
$Segments.add_child(seg_dup, true)
yield(get_tree(), "idle_frame")
var dupjoint = seg_dup.get_node("PinJoint2D")
dupjoint.node_b = last_seg.get_path()
num_segs = $Segments.get_child_count()
ppos = pjoint.global_position
seg_dup.get_node("North_Pole").global_position = ppos
pjoint.node_a = seg_dup.get_path()
player.velocity = init_vel
player.set_process_input(true)
player.set_physics_process(true)
for chld in $Segments.get_children():
chld.mode = 0
chld.set_physics_process(true)