Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Custom planetary gravity = slowly enlarging orbits?

Asked by
thebayou 441 Moderation Voter
6 years ago
Edited 6 years ago

Intro

For an upcoming space game I've scripted a basic custom universal gravity simulator. I've set the workspace's gravity value to 0 and decided to use BodyForces to simulate gravitational attraction.

The Problem

I've gotten pretty far on the grav sim, but I've encountered an issue regarding the trajectories of the parts. Namely, when a part starts orbiting another part, its orbit inexplicably gets larger each revolution. An example: https://imgur.com/gallery/G4h1sd4

As you can see, the orbits get progressively wider, and yes, there's only two bodies involved.

Code

Force Updater

01runService.Stepped:Connect(function()
02 
03    local parts = {}
04 
05    for _, part in pairs(workspace:GetDescendants()) do
06        local mass = getMass(part)
07        if part ~= workspace.Terrain and not part:FindFirstChild("DoNotBeAttracted") and mass > 0 then
08            parts[part] = {["pos"] = getPos(part), ["mass"] = mass}
09        end
10    end
11 
12    for part, attr in pairs(parts) do
13 
14        local pos = attr.pos
15        local mass = attr.mass
View all 48 lines...

Gravitation Calculation Function

1function module.calculateGravitationalAttraction(pointA, massA, pointB, massB)
2    return module.gravitationalConstant*massA*massB/(pointB-pointA).Magnitude^2
3end

Theories

Could the forces not be updating frequently enough, leading to a delayed gravity, in a sense, which would allow the bodies to drift more than they should? If so, how should I fix this?

Could this be similar to what's happening in real life to the Earth and Moon (very very doubtful)?

Any help is appreciated

Answer this question