0 votes

I'm trying to create a ninja rope with grappling hook, and am struggling on how to correctly use the Joint2D nodes.

I'm creating instances of a Rope segment scene (just a RigidBody2D with a sprite and collision shape), then creating joints (I'm using DampedSpringJoint2D because I want the rope to be somewhat elastic, I've also tried with PinJoint2D but no luck) and adding the rope segments as nodea and nodeb.

However, they behave wild and unpredictable and don't seem to truly follow the constraints I've set (length, rest_length). I've experimented a bit with the code and now it's fairly messy and I still don't seem to understand how to add two bodies together with a joint. As you'll see I'm only a beginning developer so any advice on how to correctly go about this is appreciated. Here is the relevant code:

extends Node2D

const rope = preload("Rope.tscn")
var rp_array = []
var joint_array = []
var rp_amount = 14
var rp_segment_length = 14
onready var pos_start = Vector2(200, 200)
var rp_anchor2 = Node2D

const staticbody = preload("staticbody.tscn")

func _ready():
    var rp_anchor = staticbody.instance()
    rp_anchor.position = Vector2(pos_start.x - rp_segment_length, pos_start.y)
    var rp_previous = rp_anchor

for n in range(rp_amount):

    #Create rope segments

    var rp_current = rope.instance()
    rp_current.position = Vector2(pos_start.x  + rp_segment_length * n, pos_start.y)

    #Create joints 

    var joint_current = DampedSpringJoint2D.new()
    joint_current.position = Vector2(rp_current.position.x + (rp_segment_length/2), rp_current.position.y)
    joint_current.node_a = rp_previous.get_path()
    joint_current.node_b = rp_current.get_path()
    joint_current.length = 4
    joint_current.rest_length = 2
    joint_current.stiffness = 100
    joint_current.bias = 10
    joint_current.disable_collision = true  
    rp_previous = rp_current

rp_anchor2 = staticbody.instance()
rp_anchor2.position = Vector2(pos_start.x + rp_segment_length * (rp_amount +1), pos_start.y)
var joint_last = DampedSpringJoint2D.new()
joint_last.length = 4
joint_last.rest_length = 2
var rp_last = rp_array.back()
joint_last.node_a = rp_last.get_path()
joint_last.node_b = rp_anchor2.get_path()

var joint_origin = joint_array[0]
in Engine by (12 points)

I'm not sure what your current problem is and I don't have time right now to check it, but I did something similar in a personal project a while back and maybe you can check and see if it helps you, here is the link. The rope like logic is all between the String.tscn and the scenes-characters/Puppet.gd. Hope it helps.

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 Frequently asked questions and 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 [email protected] with your username.