0 votes

I have the following block of code to create a rope, one segment at a time:

func add_segment(direction):
   var new_segment = Segment.instance()
   segments.add_child(new_segment)
   var new_segment_path = new_segment.get_path()

   new_segment.rotation = direction

   # Node a is this node, node b is next node
   if length > 0:
      var prev_segment = segments.get_child(length-1)
      prev_segment.connection.node_b = new_segment_path 
      new_segment.position = (prev_segment.north_pole.position.rotated(direction) +
                            prev_segment.position)

   else:
      host_joint.node_b = new_segment_path

   length += 1

Each segment is a RigidBody2D with origin at the bottom of the segment and the north_pole at the top. Using this code causes strange behavior of the segments, and it seems like forces pull on them incorrectly. The joints are PinJoint2D, and node_a is defaulted to the segment it's a child of. Host is a KinematicBody2D. I'm fairly certain the position I set new_segment.position to is correct.
However, when I add each segment to segments (a Node2D child of the rope structure) and connect its joint in the editor, everything behaves normally. Why is this the case?

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

Please log in or register to answer this question.

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 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 webmaster@godotengine.org with your username.