Issues Simulating Gravity

I’m trying to simulate gravity using the formula G * ((m1*m2)/d). G is the gravitational constant of 6.674, m1 and m2 being the masses of the two objects. Can someone help me understand why the force of gravity is the same for two differently sized objects? For some reason, smaller objects will not move as quickly compared to larger objects. Larger objects will come hurling towards smaller objects as if the smaller object’s gravitational pull is greater.

local ServerStorage = game:GetService("ServerStorage")

local FirstObject = nil
local SecondObject = nil

local UniverseConfigurationModule = require(ServerStorage.UniverseModule)
local SpeedOffset = 10^6

while true do
	local Objects = workspace.Objects:GetChildren()

	for n, obj in pairs(Objects) do
		FirstObject = obj

		for n2, obj2 in pairs(Objects) do
			if FirstObject ~= obj2 then
				SecondObject = obj2
				
				--Gravitation and Spin
				local Properties = PhysicalProperties.new(UniverseConfigurationModule.Gravity.Density, .3, .5)
				FirstObject.CustomPhysicalProperties = Properties

				local G = UniverseConfigurationModule.Gravity.Constant
				local m1 = FirstObject.Mass/SpeedOffset
				local m2 = SecondObject.Mass/SpeedOffset
				local d = (FirstObject.Position - SecondObject.Position).Magnitude

				local F = (G * ((m1*m2)/d))

				FirstObject.AssemblyLinearVelocity += CFrame.lookAt(FirstObject.Position, SecondObject.Position).LookVector * F


				local ObjectUnitVelocity = FirstObject:FindFirstChild("UnitVelocity")
				local ObjectForce = FirstObject:FindFirstChild("GForce")

				local UnitVelocity, VelocityUnitPerSecond = UniverseConfigurationModule.GetVelocityUnitRate(FirstObject.AssemblyLinearVelocity)

				if ObjectUnitVelocity then
					ObjectUnitVelocity.Value = UnitVelocity
				else
					local NewObjectUnitVelocity = Instance.new("NumberValue", FirstObject)
					NewObjectUnitVelocity.Name = "UnitVelocity"
					NewObjectUnitVelocity.Value = UnitVelocity
				end

				if ObjectForce then
					ObjectForce.Value = F
				else
					local NewObjectForce = Instance.new("NumberValue", FirstObject)
					NewObjectForce.Name = "GForce"
					NewObjectForce.Value = F
				end
				
				
				--Collision And Particles
				 
			end
		end
	end
	wait()
end
1 Like

It’s d * d
- Newton's law of universal gravitation - Wikipedia

This can be simplified:

local d = SecondObject.Position - FirstObject.Position
FirstObject.AssemblyLinearVelocity += d.Unit * F

This makes it easier to see that your application of acceleration is also incorrect, it’s dv = a(t) * dt, and a = F / m so dv = (F(t) / m) * dt where dt is your timestep.
- Derivatives in Science
- Newton's laws of motion - Wikipedia

So try

local dt = 1/30 --approximately how fast a while wait loop can run, at best. Use RunService to get an actual real-time timestep.
local dv =  d.Unit * (F / m1) * dt --i.e. change in velocity, probably just do this on one line combined with the next one, but splitting it up illustrates what dv is meant to be
FirstObject.AssemblyLinearVelocity += dv

It’s practically been a year but thanks for explaining that, I’d already fixed a few things relating to this topic looks like I may still need some changes, thanks :smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.