Say I want to rotate an object using around a certain point and I want to be able to change the position of that point without it affecting the object's position or rotation

I tried using the AnimationPlayer Node however I later realized I wanted to repeat the same animation relative to the object's new position which is a functionality I don't think the AnimationPlayer Node provides

Thank you

Duplicate of the following: How do I rotate an object around a certain point using code?

Please avoid creating duplicate posts, as it is against forum rules (rule 13). Instead, please bump the post with what you have already tried, or something like that.


To answer the question, there are a number of ways you can do it. Are you using 3D or 2D? Its easier to do in 2D, since there are just two coordinates.

Here's how you could do it in 2D (untested):

var rotation_point = Vector2(100, 100)
var rotation_around_point = 0
var distance_from_point = 100

func _process(delta):
	# set the position to the point
	global_position = rotation_point
	# offset using the rotation
	global_position += Vector2(cos(rotation_around_point), sin(rotation_around_point)) * distance_from_point

However, this will always keep the object a certain distance away. To keep the object in the same position and rotation, you need to do the following right after you move the point:

# we'll assume the script above is on a node and that we have a reference
# to the node in a variable called "other_node"

# Move the rotation point node (example)
position += Vector2(10, 10)
# update the other_node's variables so it stays in the same position
other_node.rotation_point = global_position
var angle_from_point_to_node = global_position.direction_to(other_node.global_position)
other_node.rotation_around_point = atan2(angle_from_point_to_node.y, angle_from_point_to_node.x)
other_node.distance_from_point = global_position.distance_to(other_node.global_position)

Then when you want to rotate the node, you can just change rotation_around_point and then it should rotate around that point.

I've changed the topic type to a question so you can mark the post with the answer as the solution and I've also removed the unnecessary " (Answered!)" from the topic title. Cheers.

2 years later