I'm currently making a really simple solar system and would like to create a script that iterates through the planets and rotates their position depending on their radius from the sun. I think object orientated programming is the best solution for this, so I gave it a go. The following code works but I was wondering if there were simpler methods of approaching this problem.
local planet = {} --empty class for the planets
function planet.new(host, radius)
return setmetatable({host = host, radius = radius, theta = 0}, planet)
end
local planets = {
["p1"] = planet.new(script.Parent.Planet1, 5, 0),
["p2"] = planet.new(script.Parent.Planet2, 8, 0)
}
for _,v in pairs(planets) do
print(v.radius)
print(v.host)
end
local sun = script.Parent.Sun.PrimaryPart
local centerGrav = sun.Position
local mass = 10 --Mass of star in 10^29 kg (Our sun is 2)
local RunService = game:GetService('RunService')
RunService.Heartbeat:connect(function()
for _, v in pairs(planets) do
local V_orbit = mass/(v.radius*10)
v.theta = v.theta + V_orbit
if v.theta >= 360 then --keeps theta within 0-360 degrees.
v.theta = v.theta - 360
end
local rads = math.rad(v.theta) --convert theta from degrees to radians (are there lua functions that accept angles in degrees?)
v.host.BodyPosition.Position = centerGrav + Vector3.new(v.radius*math.cos(rads), 0, v.radius*math.sin(rads))
end
end)

2 comments
What is it exactly you want to do
Rotate x number of planets around a sun given the radius and the current angle with respect to the sun.