How to script orbital Gravity?

I’m surprised, from a few videos I’ve seen, to turn on the ball around another ball,similar to gravity. I have tried to imitate this video,but can’t get it right. Every time I copy the video, I get the number “NAM, NAM, NAM”. How can I do it in a different way???

5 Likes

You can actually manipulate gravity without the use of scripts!

In Studio in your place go to Home Tab > Settings > Game Settings > World > and Gravity

If you know what you want your gravitation acceleration to be (in meters/second^2) you can multiply that by the generic mass of the ROBLOX character (3.5714285714285714285714285714286) and that will give you the value (in Newtons) that you should input into this field!

1 Like

I know this, but i want make a orbital gravity (sorry for the misstake, this is my first post in the forum)

To simulate gravity in an object, add a BodyForce object inside.

Create a large object you want the gravity to act around.

For a simple model of 1 attractor (assuming all the other objects don’t interact with each other, only with the fixed attractor) the force can be calculated by this:

local G = 6.67408 * 10 ^ -11 -- gravitational constant
local massivePart = yourPlanet -- attractor, fixed in space

for _, moonPart in pairs( yourMoons ) do
    local r = massivePart.CFrame.p - moonPart.CFrame.p
    local F = G * moonPart:GetMass() * massivePart:GetMass() / r.Magnitude ^ 2
    moon.BodyForce.Force = F * r.Unit
end

Swap out yourPlanet and yourMoons for a reference to your fixed body planet (assuming that’s what you want) and your free body moons. They should be a single part to allow for mass calculations. You can always weld low mass objects to them if you want to stick something to their surface, but beware that any large objects you stick to them would change the way the force acts.

If you find the objects are not being attracted as strongly as you’d like, adjust their density in CustomPhysicalProperties.

Ensure to set game.Workspace.Gravity to 0 to disable global downwards gravity.


If you wish to, you can expand this code to every object in your Workspace and you can unanchor the fixed planet, but you will have to do some more maths to work out the interaction between different gravitational fields as in reality every object is gravitationally attracted to every other object in the universe. The Earth is just so big compared to us that it dominates pretty much every interaction here on the surface.

12 Likes

How did you find out, and does that really work (i write
with a IPad, i dont kann test studio now :neutral_face: ), thx for this good reply.

Can i run this script with normal gravity in workspace?

As stated in the reply, Workspace gravity should be set to 0, otherwise it interferes and provides a global down component on every body. If you want it to be like space, where discrete bodies have their own gravitational field, you need to disable the Workspace one by setting it to 0.

It’s just physics. I think they cover gravity and electromagnetism around age 16-18. Here in the UK that would be sixth-form. If you’re from another country it is likely to be a similar age.

You can go much deeper and start modelling interactions and full orbital mechanics, but I think it’s best to stick with a simple model of a fixed body and one or more free bodies around it as explained in my reply before.

4 Likes

Thx (i will trie it on the windwos)

Just to add on, G is the gravitational constant, approximately equal to 6.67408 x 10^-11, or 0.0000000000667408. Make sure you use that or you’ll get an error!

2 Likes

Ok thx you for this information

Thanks for pointing that out - wrote it in a rush on mobile and forgot to define G! Very important constant

2 Likes

@BanTech, i tested your script a few days ago and it worked, but how can I do that the moon is attracted to the earth and that the moon does not touch the earth.
[P.S.: How can I send a video in this topic to show you the problem? (And what is P.S.?)]

If your moon keeps falling to earth, it isn’t orbiting fast enough. If you are doing things at small scale, which I imagine you are, then you’ll probably want to ignore gravity for the moon and instead just place it using BodyPosition and BodyGyro to orbit around earth at a steady rate and steady height with the correct side of the moon always facing earth.

How to make this? I can‘t it visualize.