How to rotate an object around a point

Hmm…
Actually, it’s better to use the displacement/offset instead of the velocity for the constraint computation.

I imagine that your artefact/bug is caused by the velocity vector “extending past” the trajectory circle. The constraint is currently trying to correct the velocity based on how far the entity moves in one second (velocity is in m/s). It should actually be based on the offset produced by the velocity:

offset = velocity * speed * delta
Code: move_and_collide(velocity * speed * delta)

After having another look at your code, I also missed the fact that the speed is multiplied at the end instead of the beginning. The speed (i.e. the length of the velocity vector) is paramount to constraining the motion correctly; otherwise the entity will overshoot the pos_on_circle that we’re targeting as the speed was not accounted for when computing said position.

This is a direction: Vector2(0, -1).rotated(angle)
This is a velocity: Vector2(0, -1).rotated(angle) * speed

This is my bad. I gave a long description of how computers simulate dynamic systems – and then I forget about it when making the example…

Here’s your code corrected with the above considerations in mind:

func handle_movement(delta):

	var angle = get_angle_to(click_pos)
	var new_velocity = Vector2(0, -1).rotated(angle) * speed

	# The change in position caused by the velocity (in one frame/tick/update)
	var offset = new_velocity * delta

	var new_pos = (global_position + offset)
	pos_on_circle = ((new_pos - click_pos).normalized() * to_center.length()) + click_pos

	# OLD CODE
	#var corrected_velocity = pos_on_circle - global_position
	#velocity = corrected_velocity
	#move_and_collide(velocity * speed * delta)

	# NEW CODE
	var corrected_offset = pos_on_circle - global_position
	move_and_collide(corrected_offset)

	# NOTE: I opted to not use the velocity-variable as you're not using move_and_slide()
	#       If you later choose to use move_and_slide(), you have to derive the velocity
	#       from the offset like so: velocity = corrected_offset * (1.0/delta)

Hopefully this completely solves your issue(s).

2 Likes