How to make a node rotate around another one?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By SebSharper

I have already seen some answers about this same problem, but I need something a bit different. Let me explain.
I am making kind of a simulation of a Solar System, so I need the “planets” to rotate around a “sun”, and I should be able to set the angle of these planets manually.
I would need to be able to control how fast the planet rotates around the Sun, as not all planets rotate at the same “speed”.
Should I use any kind of body? Or is this possible only using a Node2D?
I have also thought about using a pre-made Animation in the Animation Player, because I think that would be better (in terms of performance, because the game would not actually rotate and execute the code in _process()) but the main problem I see is that, in that case, it would be hard to set an angle manually.
How could I make this work?
Thanks .

:bust_in_silhouette: Reply From: MitchReidNZ

I’m doing a kind of similar thing in a little test project, where I have a reticule circling around my player based on mouse position.

In my case I have an “anchor” node, which I’d assume you’d position over your sun. I just use a plain ol’ Node2D.
My reticule (in your case, a planet) is then a child of the anchor (in my case it’s just a sprite). My reticule is then offset from the anchor by a certain distance in local transform - I adjust my position.x to 100, so it’s always 100 units from my anchor.
Because the reticule/planet is a child, any rotation on the anchor will swing it around.

In my case I find the angle to the mouse, and set my anchor rotation to that, so that my reticule / planet is always reaching toward the mouse.
In your case you could skip the get_angle_to call, and just set it to the angle that you want.

In gdscript, my code is a one-liner in _process:

anchor.rotate(anchor.get_angle_to(get_global_mouse_position()))

Then, if you want the planet to move closer/further away you can just modify the local position.x to be smaller or larger.

This is working for now, but I got two questions.
Is there any way to make it rotate by itself? I mean, like rotate a degree every x time. And also, based on that, do you think it would be better to animate the rotation with a tween (related to previous question) or to execute the code in _process()?

SebSharper | 2020-05-18 04:19

:bust_in_silhouette: Reply From: PeterA

You can make curves around the sun, each with a different radius

var center = Vector2(0,0)
var curve = Curve2D.new()
var circle_slice = 100
var radius
for i in range(circle_slice):
   	curve.add_point((center+Vector2(radius,0).rotated(2*PI*i/circle_slice))

Then just add that curve to a path2D, and then add a PathFollow2D as a child of that.

Then in the _process() you can add just one line for a sprite to follow the PathFollow2D with different speeds. The angle is the angle of your sprite.position and Vector(1,0)

And don’t worry about optimization before you even make a thing. Finish it in whatever way first, and if the end product is lagging, then optimize.