Hello,
I'm trying to create a rope that will connect two objects and that will not tear itself in the process.
Here's where I'm at now:

As you can see, the rope is tearing at the left object. Middle parts are being added from the right obj to the left obj in this case. If I switched the objects same would happen just on the other side, so let's call them left and right objs and know that we start from the right one creating middle parts to the left.
I created a Node
object called 'Chain' (read: Rope) that will create a rope that connects any two objects.
It will be as long as the distance between them with some minor errors. Just like a Joint2D, this object takes 2 NodePath
s into account, and when both are present it connects them.
Here's the code for connecting the nodes:
func connect_nodes():
if !has_node(nodeAPath) or !has_node(nodeBPath): return
var nodeA = get_node(nodeAPath)
var nodeB = get_node(nodeBPath)
var posA = nodeA.get_global_position()
var posB = nodeB.get_global_position()
var middleForDuplication = load("res://Scenes/Objects/Chain/Middle.tscn").instance()
var midRotation = nodeA.get_angle_to(posB)
var midPos1 = middleForDuplication.get_node('Position1').position
var midPos2 = middleForDuplication.get_node('Position2').position
var midLength = (midPos1 - midPos2).length()
var midRotationV2 = Vector2(cos(midRotation), sin(midRotation))
var midsToMake = floor((posA - posB).length() / midLength)
var pinFrom = nodeA
for i in range(midsToMake):
var middle = middleForDuplication.duplicate()
middle.rotation = midRotation
middle.position = pinFrom.position + midLength * midRotationV2
middle.add_collision_exception_with(pinFrom)
add_child(middle)
var pin = PinJoint2D.new()
pin.position = middle.get_node('Position2').position
pin.node_a = middle.get_path()
pin.node_b = pinFrom.get_path()
middle.add_child(pin)
pinFrom = middle
# trouble starts down here
var pin = PinJoint2D.new()
pin.position = posB
pin.node_a = nodeB.get_path()
pin.node_b = pinFrom.get_path()
nodeB.add_child(pin)
I'm having trouble with the last pin I'm adding.
How do I make the last pin actually not detach from the last middle part and the left obj?
I tried to add this pin to nodeB
and to pinFrom
with pretty much same results.
The PinJoint2D.softness
is set to 0 by default, so the bond can't be more rigid than it already is.