Problems with duplicate and pinjoint2d

Godot Version

4.1.2.0

Question

When I create a duplicate of the object the rope that is attached to it by pinjoint2d doesn’t work properly. I tried to fix it by making the rope components return to their original position but it doesn’t work.


This is how it ends up looking

Do you update the node_a or node_b NodePath property of the last PinJoint2D that attaches the rope with your duplicated object?

Something like:

var node = ball.duplicate()
add_child(node)
node.position = Vector2(100, 100)
pin_joint.node_a = pin_joint.get_path_to(node)

How do I access a component if I’m using a variable? Because if i use: $node the variable doesn’t appear and if I use: node/pinjoint2d pinjoint it’s the one that doesn’t appear

I don’t know. Post your code and your scenes structure. We can’t see your project.

Here is the code:

extends RigidBody2D

var in_balloon = false
var enable_movement = false

func balloon():
pass

func _ready():
pass

func _physics_process(_delta):
if in_balloon:
if Input.is_action_just_pressed(“left_clic”):
enable_movement = true
sleeping = true

  if enable_movement:
  	sleeping = false
  	var vel: Vector2 = (get_global_mouse_position() - global_position)
  	self.apply_central_impulse(vel/3)
  	
  
  if Input.is_action_just_released("left_clic"):
  	enable_movement = false
  
  if Input.is_action_just_pressed("right_clic"):
  	var clon = self.duplicate()
  	add_sibling(clon)
  
  if Input.is_action_just_pressed("middle"):
  	#self.queue_free()
  	$PinJoint2D.position = Vector2(-0.441, 32.152)
  	$PinJoint2D.node_a = $PinJoint2D.get_path_to(self)
  	$PinJoint2D.node_b = $PinJoint2D.get_path_to($rop)
  	$PinJoint2D2.node_a = $PinJoint2D2.get_path_to($rop)
  	$PinJoint2D2.node_b = $PinJoint2D2.get_path_to($rop2)
  	$PinJoint2D3.node_a = $PinJoint2D3.get_path_to($rop2)
  	$PinJoint2D3.node_b = $PinJoint2D3.get_path_to($rop3)

func _on_mouse_entered():
in_balloon = true
$Timer.stop()

func _on_mouse_exited():
$Timer.start()

func _on_timer_timeout():
in_balloon = false
enable_movement = false
$Timer.stop()

func _on_hitbox_body_entered(body):
if body.has_method(“damage”):
global.game_inprocess = false
self.queue_free()

I guess a image of the scene is enough right?

1 Like

I don’t think a Joint2D being a child of the node it’s trying to pin is a good idea as it will move with the body. Try adding a plain Node as a parent of the PinJoint2Ds and the other RigidBody2Ds to break the transform propagation. Something like:

RigidBody2D
	Sprite2D
	CollisionShape2D
	Node
		PinJoint2D
		RigidBody2D
		PinJoint2D
		RigidBody2D
		PinJoint2D
		RigidBody2D
	Area2D
	Timer

Also, your duplicated node clon is probably colliding with the original RigidBody2D and messing up everything. Try changing its position so it does not spawn at the same position as the original RigidBody2D

1 Like

It’s not working, and the pinjoints are not moving

I tried with node2D too.
Also I’m getting this error:

Attempt to disconnect a nonexistent connection from ‘rop:<RigidBody2D#1749461037079>’. Signal: ‘tree_exiting’, callable: ‘Joint2D::_body_exit_tree’.