Help with Center of Mass Equation

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

1 Like

solved:

local totalMass = 0
local weightedSumOfPositions = Vector3.new(0, 0, 0)

for i, object in pairs(stellarObjectsFolder:GetChildren()) do
    totalMass += object.Mass
    weightedSumOfPositions += object.Position * object.Mass
end

local CenterOfMass
if totalMass ~= 0 then
    CenterOfMass = weightedSumOfPositions / totalMass
else
    -- Handle the case where total mass is zero if necessary
    CenterOfMass = Vector3.new(0, 0, 0)
end

workspace.Center.Position = CenterOfMass
3 Likes

I wonder if part of your original issue was that you used Position.Y instead of Position.Z. in the third line.

1 Like

You can do it this way since it’s more managed:

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 = totalMass + object2.Mass
    SumofXPos = SumofXPos + object2.Position.X
    SumofYPos = SumofYPos + object2.Position.Y
    SumofZPos = SumofZPos + object2.Position.Z

    if object2.Position.X ~= 0 then massOnX = massOnX + object2.Mass end
    if object2.Position.Y ~= 0 then massOnY = massOnY + object2.Mass end
    if object2.Position.Z ~= 0 then massOnZ = massOnZ + object2.Mass end
end

local CenterOfMass = Vector3.new(SumofXPos / totalMass, SumofYPos / totalMass, SumofZPos / totalMass)
workspace.Center.Position = CenterOfMass
1 Like

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