How are your planets moving currently? Is it an equation or is it physics based?
im still kinda deciding on making moving CFrames or line forces so not sure
If you use a CFrame, then interpolate the full planetary cycle on a 24 hour cycle based on os.time().
Edit: or however long you want the cycle of the planet to be
hey there,
i made a CFrame script of orbit but i dont know how i can integrate it with os.time().Any Help?
local count = 0
local dist = 100 --How far the planet is from the star in studs
local spd = 0.001 --How fast the planet orbits.
local xangle = 0--how the planet is tilted in its orbit x axis
local yangle = 0--how the planet is tilted in its orbit y axis
local orbitparent = script.Parent.Parent.Sun
while true do
count += 1
wait()
script.Parent.CFrame = orbitparent.CFrame*CFrame.fromEulerAnglesXYZ(xangle,count*spd,yangle)*CFrame.new(0,0,dist)
end
count = os.time()
And please use task.wait(), lol. It’s far more reliable and consistent than wait().
Edit: or better yet, don’t use count variable; directly reference os.time() for less redundancy.
Keep in mind, your equation relies on spd to calculate the time for a full orbit. 0.001 would take 1000 seconds to complete a full orbit, which is 16.67 minutes. You can calculate minutes quite easily like so;
local spd = 1/60*10 -- a full orbit would be 10 minutes
or
local spd = 1/60*60*3 -- a full orbit would be 3 hours
Edit: My math here wasn’t quite right, see answer for correct math.
so i followed that
local count = os.time()
local dist = 100 --How far the planet is from the star in studs
local spd = 0.001 --How fast the planet orbits.
local xangle = 0--how the planet is tilted in its orbit x axis
local yangle = 0--how the planet is tilted in its orbit y axis
local orbitparent = script.Parent.Parent.Sun
while true do
task.wait()
script.Parent.CFrame = orbitparent.CFrame*CFrame.fromEulerAnglesXYZ(xangle,count*spd,yangle)*CFrame.new(0,0,dist)
end
and it didnt move a single bit and i checked the position property and saw no numbers moving
That’s because you set count to os.time() outside the loop so it’s only checking os.time() once. os.time() increases by one each second.
local dist = 100 --How far the planet is from the star in studs
local spd = 0.001 --How fast the planet orbits.
local xangle = 0--how the planet is tilted in its orbit x axis
local yangle = 0--how the planet is tilted in its orbit y axis
local orbitparent = script.Parent.Parent.Sun
while task.wait() do
script.Parent.CFrame = orbitparent.CFrame*CFrame.fromEulerAnglesXYZ(xangle,os.time()*spd,yangle)*CFrame.new(0,0,dist)
end
ye i also tried putting the local count thing in the loop and it didnt work too
script.Parent.CFrame = orbitparent.CFrame*CFrame.fromEulerAnglesXYZ(xangle,os.time()*spd,yangle)*CFrame.new(0,0,dist)
so i did this
local dist = 10 --How far the planet is from the star in studs
local spd = 1/60*10 --How fast the planet orbits.
local xangle = 0--how the planet is tilted in its orbit x axis
local yangle = 0--how the planet is tilted in its orbit y axis
local orbitparent = script.Parent.Parent.Sun
while true do
print("working")
wait()
script.Parent.CFrame = orbitparent.CFrame*CFrame.fromEulerAnglesXYZ(xangle,os.time()*spd,yangle)*CFrame.new(0,0,dist)
end
and it prints but the planet doesnt move at all and i put the equation at the spd to make the planet orbit around star for 10 min and did not find a single movement after 10 min
Did it work before? When os.time() wasn’t in the code
yes when i used the count variable like the one i had at start the planets move normally
Oh I see what happened. I made a couple mistakes.
I will get back to you later today. I’m not home and I want to test my answer before I submit.
Okay so I made a couple of mistakes:
- os.time() is too large of a number for a CFrame to handle.
- spd needs to incorporate a full rotation in radians (math.rad(360), or it won’t be accurate.
- directly incorporating os.time() causes the interpolation to be stuck at a rate of once per second.
local dist = 60 --how far the planet is from the star in studs
local spd = math.rad(360)/10 --where 10 is how many seconds per orbit
local xangle, yangle = 0, 0 --how the planet is tilted in its orbit x/y axis
local orbitparent = script.Parent.Parent.Sun
local lastTimeLogged, count = os.time(), 0
while task.wait() do
if os.time() > lastTimeLogged then --if its been 1 second, restart count at 0
count = 0
lastTimeLogged = os.time()
end
count += 1
script.Parent.CFrame = orbitparent.CFrame*CFrame.fromEulerAnglesXYZ(xangle,((lastTimeLogged%1000)+(count/60))*spd,yangle)*CFrame.new(0,0,dist)
--lastTimeLogged%1000 <- gets us the last 4 integers of os.time(); gives us 1/10000 of accuracy between all servers planetary positions
--for greater accuracy especially at very long orbits, you could increase this to 5+
--count/60 <- this tells us how many 1/60 of a second have passed since the last second; in my tests this was extremely reliable
end
I’ve tested this code, if you have any issues let me know!
Hey your code is working! thx for the code.Seems like the yangle variable makes the planet tilt axis like earth? and also how would i add spin to the planets? i tried CFrame.Angles and didnt work
I don’t know what you mean by this, please explain
CFrame.Angles is definitely a step in the right direction. You will need to incorporate the same kind of equation we did for the orbit, for the angle.
I got excited, because one day I want to make a space game; so I’ve already went ahead and made an example of using the above code to add multiple planets in orbit, as well as, the sun has an orbit, and all of the planets and the sun have angular rotation. (the largest cube in the center is the “sun”, each consecutively smaller body is a “planet”, and they all have unique sizes, rotational speeds, and orbiting speeds); the speeds are made using a formula, so there is some symmetry that you may notice. Since I used our equation to incorporate os.time(), all of this should be synchronized between any and all servers within a very small margin of error.
i meant like this File:Earth tilt animation.gif - Wikimedia Commons
as you can see the gif that the earth is tilted and i want to do that and also the earth spins around which makes day/night
wow that looks cool… would you mind sharing the fomula of the speeds? its not a problem if you dont want
I just sent you the file.
Ah, yes… I see. The X axis is at a static angle and the Y axis is rotating slowly. You should be able to do this once you take a look at that file and try to understand everything in it. Good luck!