GetBoundingBox returning incorrect numbers

The random model placement system I have been developing for my Backrooms game and currently have this code:

bases = {}
basesFolder = game.Workspace.bases
spawnsFolder = game.Workspace.spawn
-- Self explanatory variables
spawns = {}
minCount = 0 -- These 2 refer to minimum and maximum placements, don't modify
maxCount = 0 -- These 2 refer to minimum and maximum placements, don't modify
partCount = 0 -- Total parts in the folder, don't modify this
spawnedParts = {} -- FOlder to store spawned parts for debug purposes.

for i, v in ipairs(spawnsFolder:GetChildren()) do
	spawns[i] = v
	partCount += 1
	print("Part ", i, "was added to list")
end
-- Add all the objects to spawn into a list
for i, v in ipairs(basesFolder:GetChildren()) do
	bases[i] = v
	print("Spawn base ", i, "was added to list")
end
-- Add all the spawn bases into a list
function randPlace(rotation, minRotation, maxRotation)
	for i, v in bases do
		minCount = v.minValue.Value
		maxCount = v.maxValue.value
		-- Get minimum and maximum spaned part counts.
		maxX = v.Position.X + v.Size.X / 2
		minX = v.Position.X - v.Size.X / 2
		maxZ = v.Position.Z + v.Size.Z / 2
		minZ = v.Position.Z - v.Size.Z / 2
		-- Determines the edges of the base to prevent parts from generating outside
		for i=1, math.random(minCount, maxCount) do
			print(i)
			-- Test print statement
			place = spawns[math.random(1, partCount)]:Clone() 
			place.Parent = game.Workspace.spawned
			-- Make a new random part
			maxX -= place.PrimaryPart.Size.X
			minX += place.PrimaryPart.Size.X
			maxZ -= place.PrimaryPart.Size.Z
			minZ += place.PrimaryPart.Size.Z
			-- Modify edges to prevent spawned parts from sticking out
			--place.PrimaryPart.Position = Vector3.new(math.random(minX, maxX), place.PrimaryPart.Size.Y / 2 + v.Size.Y / 2 + 0.001, math.random(minZ, maxZ))
			place.PrimaryPart.Position = Vector3.new(0, 0, 0)
			print(place.PrimaryPart.Position) -- Debug print statement
			if rotation then
				place.PrimaryPart.Orientation = Vector3.new(0, math.random(minRotation, maxRotation), 0)
			end -- Randomise rotation if it's enabled
			place.PrimaryPart.Anchored = true
			spawnedParts[i] = place -- Add the new part to spawned parts, this lets the deletion function find it.
			--print(place:GetBoundingBox())
			boxFrame, boxSize = place:GetBoundingBox() -- Get the part's bounding box location and size, currently returns very wrong number
			print(boxFrame, " ", boxSize)
			--boxSize = place:GetExtentsSize()
			local exclude = OverlapParams.new()
			exclude:AddToFilter({v, place:GetChildren()}) -- Parts that are children of the spawned parts and will be ignored by the GetPartBoundsInBox method
			--print(exclude)
			repeat
				testPos = CFrame.new(math.random(minX, maxX), place.PrimaryPart.Size.Y / 2 + v.Size.Y / 2, math.random(minZ, maxZ))
				--print(testPos)
				--print(boxSize)
				--print(#workspace:GetPartBoundsInBox(testPos, boxSize, exclude))
				--print(workspace:GetPartBoundsInBox(testPos, boxSize, exclude))
				--print({place, v})
				task.wait()				
			until #workspace:GetPartBoundsInBox(testPos, boxSize, exclude) == 0
			--until workspace:GetPartBoundsInBox(testPos, boxSize) == {v} or workspace:GetPartBoundsInBox(testPos, boxSize) == {place, v}
			print("Location: ", testPos)
			place:PivotTo(testPos) 
			print("Placed") --Debug print statement
			maxX += place.PrimaryPart.Size.X
			minX -= place.PrimaryPart.Size.X
			maxZ += place.PrimaryPart.Size.Z
			minZ -= place.PrimaryPart.Size.Z
			-- Reset edges
		end
	end
end

randPlace(true, 0, 90)

And for some reason, place:GetBoundingBox returns 93.8763656616211, 7.170000076293945, 27.984848022460938 instead of the actual dimensions of the model currently being used, which are 3.5, 3.5, 3.5. I can’t manually set those since there will be models of different sizes being used when the system is ready. I have tried using GetExtentsSize instead but that also returns incorrect numbers. This is what the model looks like in the explorer in case that has an effect

I guess first obvious ques is what makes you think the actual dimensions are 3.5 rather than what the GetBoundingBox returns? Are you seeing those values somewhere or are you basing that off expectations of how the model was built?

Initial assumption should prob be that you’re missing something rather than that the method is broken. Will want to make sure that some part inside the model isn’t anchored when you’re moving the model or going off by itself somehow (unwelded, rolling away due to physics, etc). Check any invis parts that might be locked in place when you move the rest of the model. When running and testing, use the Studio sel tool to select the model and see if everything is where it should be. In the View tab, you can set Selection Style to “bounding boxes” or “both” so you can see the bounding box even when using the sel tool while the game is running.

All the parts are anchored and I got the 3.5 measurement by making a part that encompasses the entire model. All the parts are where they should be to so I really don’t understand why this is happening.
Edit: After some more testing it seems the X and Z values are different each time I test.

So, the issue is with your code, the model, or the bounding box.

in your code you:

  1. clone a place (a model)
  2. parent it to workspace.spawned
  3. change its primary part position to (0,0,0)
    print pos
  4. change its primary part orient if needed
  5. anchor its primary part
  6. add the place to a table
  7. get bounding box of place and see an issue
  8. then some other stuff after the issue is known

None of those things should change the bounding box. A quick test would be to print the bounding box between each of those things listed. Print the bounding box right after parenting (maybe even after cloning…or on the orig model) then again after moving to (0,0,0), etc.

Two things I’d look for are any bounding box changes between cloning and moving to origin and between moving and anchoring. It’s possible that late anchoring is creating an issue. Since the big differences to the calc bounding box and the expected one are along x and z, that suggests something is either not moving with the model when you move it to the origin or that something is rolling away later. Simplify your testing (fewer bases/spawns for testing) to make it easier to see if the differences in bounding boxes are consistent between runs or if they are different each time. Calculating the bounding box on the original and on the clone can show if the problem is already there in the model.

I tried a bunch of things I could think of, and the bounding box values seem fine, so it seems a remote (unlikely) possibility to me that the prob would be with the Rblx b-box implementation.

Edit: just noticed that you added that the x,z values are diff each time. Hard to see where your code might be introducing a problem beyond maybe that anchor thing. Really seems most likely there is a part of the model that isn’t anchored/welded/whatever as expected.

I think I’ve found the issue, the cloned part has a new part inside it called Bound at 0, 0, 0 that’s throwing off the numbers, I have no idea why it’s there though since it doesn’t appear under the part that gets cloned.
Edit: Nvm I’m stupid I was looking in the wrong model but Bound was unanchored, after anchoring it it’s still in the wrong place though.

OK so turns out I had a line of code that set the Bound’s position to 0, 0, 0 but now the code works fine.

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