# Help With Making An Elliptical Orbit Module

Hello there,

Over the past couple days I’ve been trying to completely remake my Planetary Orbit Module. Currently I have Circular and Eccentrical Orbits. My goal is to have Circular, Eccentrical, and maybe hyperbolic/parabolic orbits. Right now I want to make the Elliptical Orbit System. I have this quick script for calculating my ellipse:

``````function ellipse(centrePos, Y, r1, r2, t) --t between 0 and 1, Y represents rotation away from global x-axis (wind), r1 represents length stretched in wind direction, r2 in perpendicular direction
local _, _, _, rVx, uVx, bVx, rVy, uVy, bVy, rVz, uVz, bVz = CFrame.Angles(0, Y, 0):components() --get components of CFrame representing wind-rotation
local scaledCF = CFrame.new(0, 0, 0, r1*rVx, uVx, r2*bVx, r1*rVy, uVy, r2*bVy, r1*rVz, uVz, r2*bVz) --stretch along the vector representing direction of wind (rV or rightVector) by r1 and its perpendicular vector (bV or -lookVector) by r2
return scaledCF*(CFrame.Angles(0, 2*math.pi*t, 0)*CFrame.new(1,0,0)).p + centrePos --multiply each position vector on circle by rotated + stretched cframe then re-centre
end

``````

So if anyone could help me make a planet(part) orbit around another in an elliptical way that would be heavily appreciated.

Also if anyone could help me making when the planet orbiting is getting close to the planet getting orbited the eccentrical orbit speed would speed up and down when the orbit is happening.

Here is my module script currently:

``````--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!

BTW I’m not asking for a full script but maybe some ways I could do this. Also if you want to write it all for me that would be great.