I’m trying to find the center of mass for four different spheres that all have sort of random positions. I thought it was a fine equation, but the center of mass ends up being SUPER far off from where it should be.
my equation:
local totalMass = 0
local SumofXPos = 0
local SumofYPos = 0
local SumofZPos = 0
local massOnX = 0
local massOnY = 0
local massOnZ = 0
for i, v in pairs(stellarObjectsFolder:GetChildren()) do
local object2 = v
totalMass += object2.Mass
SumofXPos += object2.Position.X
SumofYPos += object2.Position.Y
SumofZPos += object2.Position.Z
if object2.Position.X ~= 0 then massOnX += object2.Mass end
if object2.Position.Y ~= 0 then massOnY += object2.Mass end
if object2.Position.Y ~= 0 then massOnZ += object2.Mass end
end
local CenterOfMass = Vector3.new((SumofXPos * massOnX / totalMass), (SumofYPos * massOnY / totalMass), (SumofZPos * massOnZ / totalMass))
workspace.Center.Position = CenterOfMass
I looked up videos and everything for the formula and it seems fine, but it ends up looking like this:
circled in red is the location that the script thinks the center of mass is
circled in blue is an object that has an extremely high mass, around 100x the other three objects.
I thought making a super heavy center object would make the center much more central, like a solar system, but it didnt change anything. when i move one of the light objects towards that “center of mass,” it goes super fast away from it, as if mass was completely irrelevent and its as if its just adding all the positions together.
Please help!
the three spheres each have around 33 mass while the center sphere has 3300