Dragging a box with physics (2D, top-down) (Problem 1. solved)

Godot Version

4.5

The Problem

I have a player character (RigidBody2D) and I want it to be able to “hold” a box
hold = drag it in front of itself.

However…

  1. The box gets further and further from its intended position as the character moves, which is not intended.
  2. The box should face the same direction as the player, but I do not know how to do that.

Details:

Scene tree:

The scene tree again, but also with node positions:

Movement code:

Moving:

func _physics_process(delta: float) -> void:
	var liike_suunta: Vector2 = Vector2.ZERO
	liike_suunta.y = Input.get_axis("Pohjoinen", "Etelä") # Up and down.
	liike_suunta.x = Input.get_axis("Länsi", "Itä")		  # Left and right.
	liike_suunta = liike_suunta.normalized()
	apply_force(liike_suunta * Nopeus)

Turning:

func _integrate_forces(state):
	var hiiren_piste: Vector2 = get_global_mouse_position()
	var kääntö_suunta:	float = get_angle_to(hiiren_piste)
	var kääntö_voima:	float = kääntö_suunta * 400.0 * state.step
	var pehmennys:		float = pow(0.001, 5 * state.step)
	
	state.angular_velocity += kääntö_voima
	state.angular_velocity *= pehmennys

(I am not that good with physics.)

The question

How do I keep the box in front of the character, without it being completely static, or having the problems listed in the “The problem” -section?

Extra context:

I do not want the box to be static, since in my original plans for this thing, I wanted to detect if the box gets caught on something and then detach it from the character. (This project has a lot of moving parts and obstacles.)

End

I can also give out extra information, if needed.

Thanks in advance!

I am amazing.


The first problem has been solved. I took out all the complicated bits and replaced 'em with a bit of code (translated to English):

func _physics_process(delta: float) -> void:
	...
	var box: RigidBody2D = $Anchor/RigidBody2D # "Anchor" is "Käsi", even though "Käsi" translates to hand/arm.
	var anchor_point: Vector2 = Anchor.global_position
	var pull_vector: Vector2 = anchor_point - box.global_position 
	box.apply_force(pull_vector)

I also made the box move just a bit towards the mouse, so it drags behind less:

func _physics_process(delta: float) -> void:
	...
	var box: RigidBody2D = $Anchor/RigidBody2D
	var anchor_point: Vector2 = Anchor.global_position
	var pull_vector: Vector2 = anchor_point - box.global_position
	var mouse_dir: Vector2 = (get_global_mouse_position() - anchor_point).normalized()
	box.apply_force(pull_vector + mouse_dir * 20)

Now I just have to figure out how to turn it in a way that it can also collide with stuff…
Current scene tree: