Problem with objects moving relative to other objects

I’m trying to make a solar system with planets and moons and I’m using this code to create the orbits of planets around the sun.

function init(self)
	self.center = vmath.vector3(go.get_position("Sun")) 
	self.radius = 5500 
	self.speed = 0.07 
	self.t = 0

function update(self, dt)
	self.t = self.t + dt
	local dx = math.sin(self.t * self.speed) * self.radius
	local dy = math.cos(self.t * self.speed) * self.radius
	local pos = vmath.vector3()
	pos.x = self.center.x + dx 
	pos.y = self.center.y + dy
	go.set_position(pos) 
end

This works fine, but now I want to add in moons too, but replacing the Sun with the name of a planet does not work.

function init(self)
	self.center = vmath.vector3(go.get_position("Earth")) 
	self.radius = 5500 
	self.speed = 0.07 
	self.t = 0

function update(self, dt)
	self.t = self.t + dt 
	local dx = math.sin(self.t * self.speed) * self.radius
	local dy = math.cos(self.t * self.speed) * self.radius
	local pos = vmath.vector3()
	pos.x = self.center.x + dx 
	pos.y = self.center.y + dy
	go.set_position(pos) 
end

The Moon does not orbit around the planet. It stays next to the planet as it moves around the Sun, but not orbiting it. I believe that this is because it cant figure out the movement of the moon relative the planet because the planet moving? Is there any way to make this work?

You probably want to update the center position in the update function as well, as the parent object moves.

Speaking of parent objects, it might suffice to parent the game objects to each other. That way you can calculate the movement in local space.

Unfortunately I also have a gravity functionality for the planets and moons which beaks if I put the planet as the parent of the moon it breaks that. How would I go about doing the other thing that you mentioned? I thought that given that the moon still follows the planets position that part doesn’t seem to be broken, its just the orbiting part?

Unfortunately I also have a gravity functionality for the planets and moons which beaks if I put the planet as the parent of the moon it breaks that.

Are you using physics + forces for this? Or just moving game objects manually?
(I didn’t see any gravity code in your example?)

self.center = vmath.vector3(go.get_position("Earth")) 

Since you only get this position once, it will have the position of that game object at that time. The self.centerwon’t update after that.

I thought that given that the moon still follows the planets position that part doesn’t seem to be broken, its just the orbiting part?

I cannot deduce much more without looking at the code, but I think there’s something else at play here.