Hello, I wanted to try and simulate the real solar system in Roblox Studio using scripts for calculating resultant forces and speeds for all of the celestial bodies et cetera, however, when I tried to implement my knowledge about physics, it seems that even though as far as I can tell all the values are right and relatively the same to the real solar system we have, the example planet seems to just slowly move away.
My code:
local sun=script.Parent
local G = 6.67*10^-11
local timeStep = .1
local speedUp=1
local function updateSpeed()
local val = game.ReplicatedStorage.SecondsTo.Value
local val2 = game.ReplicatedStorage.SecondsPerSecond.Value
if val=="sec" or val=="seconds" then
speedUp=1
elseif val=="min" or val=="minutes" then
speedUp=60
elseif val=="hour" or val=="hours" then
speedUp=60*60
elseif val=="day" or val=="days" then
speedUp=60*60*24
elseif val=="month" or val=="months" then
speedUp=60*60*24*30
elseif val=="year" or val=="years" then
speedUp=60*60*24*365
end
speedUp*=val2
end
game.ReplicatedStorage.SecondsPerSecond.Changed:Connect(function()
updateSpeed()
end)
game.ReplicatedStorage.SecondsTo.Changed:Connect(function()
updateSpeed()
end)
while true do
task.wait(timeStep)
for _,planet in pairs(workspace.Planets:GetChildren()) do
local m,M = planet.Mass*10^27,sun.Mass*10^27
local r = (planet.Position-sun.Position).Magnitude*10^8
local Fg = G*(m*M)/(r^2)
local T = math.sqrt((r^3*4*math.pi^2)/(G*M))
local v = (math.pi*2*r*10^-8)/(T/speedUp)
local Ga = Fg/m
local Gv = Ga*(timeStep*speedUp*10^-8)
local dU=(sun.Position-planet.Position).Unit
local gravityVector = dU*Gv
print(Ga)
local SolA = Fg/M
local SolV = SolA*timeStep*speedUp*10^-8*30
local solarPull = (planet.Position-sun.Position).Unit*SolV
workspace.Sun.Gravity.VectorVelocity=solarPull
planet.Gravity.VectorVelocity=gravityVector
planet.Speed.VectorVelocity=dU:Cross(CFrame.new(planet.Position).UpVector)*v
planet.Resultant.VectorVelocity=gravityVector+dU:Cross(CFrame.new(planet.Position).UpVector)*v
end
end
The result:
As you can see in the video, the planet isn’t able to maintain proper orbit, I don’t know how to fix this.