Hello All!
Today and Yesterday I am trying to make my Planetary Orbit Module far more advanced but I have came across an issue. In my module right now I have two functions the circular orbit function and the eccentricty orbit function. Eccentricty works well but the circular not so much. In the circular the planet orbiting goes through a really bumby orbit.Sometimes the planet that I want to move goes off course somewhere else.It is supposed to be a perfect circle.
How I want it to act : How I want it to act
What’s happening now : Whats happening right now
Current Code
--Code Made By Tomroblox54321 and IlyasTawawe
--Eccentricty Code made tweaked by Creeperman16487
--All gave permission to use code in the module
--Hope you Enjoy!
local OrbitModule = {}
--Getting RunService
local RunService = game:GetService("RunService")
--Getting Our Math Variables
local CosineMath = math.cos
local SineMath = math.sin
local Atan2Math = math.atan2
local PIMath = math.pi
local TAU = 2*PIMath
--Creating the Circular Orbit Function
function OrbitModule:CircularOrbit(PlanetOrbiting : PVInstance, PlanetGettingOrbited : PVInstance, TimeToOrbit : number, NormalOffset : number)
assert(PlanetOrbiting ~= PlanetGettingOrbited, "Cannot orbit itself, PlanetOrbiting and PlanetGettingOrbited should be different")
--Getting Our Difference, Radius and Angle
local DifferenceVector = PlanetOrbiting:GetPivot().Position-PlanetGettingOrbited:GetPivot().Position --Differnce
local Radius = DifferenceVector.Magnitude
local Angle = Atan2Math(DifferenceVector.Y, DifferenceVector.X)--Angle
--Creating the HeartbeatCobnection
local HeartbeatConnection
HeartbeatConnection = RunService.Heartbeat:Connect(function(DeltaTime)
-- Disconnect the event if one of the planets got destroyed
if not (PlanetOrbiting or PlanetGettingOrbited) then
HeartbeatConnection:Disconnect()
HeartbeatConnection = nil
assert("Orbit Disconnected")
end
-- Polar coordinates 2D
local x = Radius*CosineMath(Angle)
local y = Radius*SineMath(Angle)
local z = NormalOffset and NormalOffset(Angle, x, y) or 0
--Putting it all together
PlanetOrbiting:PivotTo(PlanetGettingOrbited.CFrame*CFrame.new(x, y, z))
Angle += DeltaTime*TAU/TimeToOrbit
end)
-- Return the heartbeat connection, so we can disconnect it if we no longer wants the part to orbit
return HeartbeatConnection
end
function OrbitModule:EccentricityOrbit(PlanetOrbiting : PVInstance, PlanetGettingOrbited : PVInstance, TimeToOrbit : number, Eccentricity : number)
assert(PlanetOrbiting ~= PlanetGettingOrbited, "Cannot orbit itself, PlanetOrbiting and PlanetGettingOrbited should be different")
--Getting Our Difference, Radius and Angle
local DifferenceVector = PlanetOrbiting:GetPivot().Position-PlanetGettingOrbited:GetPivot().Position --Differnce
local Radius = DifferenceVector.Magnitude
local Angle = Atan2Math(DifferenceVector.Y, DifferenceVector.X)--Angle
--Creating HeartbeatConnection
local HeartbeatConnection
HeartbeatConnection = RunService.Heartbeat:Connect(function(DeltaTime)
-- Disconnect the event if one of the planets got destroyed
if not (PlanetOrbiting or PlanetGettingOrbited) then
HeartbeatConnection:Disconnect()
HeartbeatConnection = nil
assert("Orbit Disconnected")
end
-- Polar coordinates 2D
local x = (Radius+Eccentricity/2)*CosineMath(Angle)
local y = Radius*SineMath(Angle)
local z = 0
local Offset = CFrame.new(Eccentricity,0,0)
PlanetOrbiting:PivotTo((PlanetGettingOrbited.CFrame*Offset)*CFrame.new(x, y, z))
Angle += DeltaTime*TAU/TimeToOrbit
end)
-- Return the heartbeat connection, so we can disconnect it if we no longer wants the part to orbit
return HeartbeatConnection
end
--Returning the whole module
return OrbitModule
Thanks for any help!