I am trying to make an orbital trajectory generate instantly and be efficient
The issue is that I don’t know how to do it efficient nor calculate, kinda just waiting for it to pass by and recording Here is a video of the orbital path
local constants = require(game.ServerScriptService.CONSTANTS)
local attractor = script.Parent.Attractor.Value
local initialDistance = (attractor.Position - script.Parent.Position).Magnitude
local velocity = constants.grav * attractor.Mass * ((2/initialDistance) - (1/275))
local gravForce = Vector3.new(-3,0,0)
while wait() do
local part = Instance.new("Part", workspace)
part.Position = script.Parent.Position
part.CanCollide = false
part.BrickColor = BrickColor.Red()
part.Anchored = true
part.Material = Enum.Material.Neon
local distance = (attractor.Position - script.Parent.Position)
local force = (constants.grav * 100 * 1) / distance.Magnitude ^ 2
gravForce += (force * distance.Unit)
print(gravForce)
script.Parent.Position += gravForce
end
If it’s a two-body system, the orbit is going to in the shape of an ellipsis. You can determine a number of points along that ellipsis path with just trigonometry, and then create parts that represent the straight line between these points. More line = less jagged ellipsis.
For an n-body system, the approach is the same except there’s no neat formula for points along the path. You’ll have to simulate the path of a particle by keeping track of its velocity and position, and updating it in small time-steps to get the points along the curve it follows.
I don’t know how that compares to straight up Verlet integration or Euler integration, but any method that can give you a series of points along the orbital path will work.
for i=0, 500 do
if i==500 then
generatedState = true
end
local distance = (attractor.Position - script.Parent.Position)
local force = (constants.grav * 100 * 1) / distance.Magnitude ^ 2
gravForce += (force * distance.Unit)
script.Parent.Position += gravForce
local part = Instance.new("Part", workspace)
part.Anchored = true
part.CanCollide = false
part.Position = script.Parent.Position
end