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?