How can I get the center of multiple Vector3's


I’m trying to get the center of N amount of Vector3’s, kinda of like the position from Model:GetBoundingBox().

I have thought of just creating part’s with the positions and parenting them to a model then using GetBoundingBox() but in certain cases I cannot use a method and for performance.

1 Like

The center of the part positions, or the center of the parts extents?

My bad, kinda open to either.

My case would go with position’s

In that case, you can just take the average of all the positions:

local function AveragePositions(parts: {Part}): number
  local total =
  for _, part in pairs(parts) do
    total = total + part.Position
  return total / #parts

You could use a different algorithm for averaging, if you’re worried about overflow or numeric stability.

See How can I get the center of multiple Vector3's - #5 by Prototrode

1 Like

Average position ≠ Central position, so if the OP is asking for the physical center of the Vector3s, it would be something like this instead

local function CentralPosition(parts: {BasePart}): Vector3
	local minExtents: {number} = {math.huge, math.huge, math.huge}
	local maxExtents: {number} = {-math.huge, -math.huge, -math.huge}
	for _, part in parts do
		local v: Vector3 = part.Position
		minExtents[1] = math.min(minExtents[1], v.X)
		minExtents[2] = math.min(minExtents[2], v.Y)
		minExtents[3] = math.min(minExtents[3], v.Z)
		maxExtents[1] = math.max(maxExtents[1], v.X)
		maxExtents[2] = math.max(maxExtents[2], v.Y)
		maxExtents[3] = math.max(maxExtents[3], v.Z)
	return, .5)

Also small nitpick

It should return a Vector3 not a number :slightly_smiling_face:


Their interpretation would still return a Vector3 as when a Vector3 is divided by a number it stays as a Vector3.

I’m referring to the type annotation. They declared that the function will return a number when it will return a Vector3.

Oh yeah sorry about that I usually use those annotations inside the parentheses, I have hardly used them outside.

Thank’s for the help :grin:

(Many more characters)

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