How do you get the furthest corner / Position of a Part / Bounding Box on an axis?

  1. I want to know how to get the furthest corner/position on an axis

  2. Cant figure it out. Works on “straight” axis aka 0,90,180,-90 degree, https://gyazo.com/2389435f0d3c7d87f5cdae1064a9a4a1 ,bec i dont have to care about the furthest out corner and can just use the normal x and z size. But when rotated to for instance 45° here it wont work anymore https://gyazo.com/8f63531f96a3bb795fe6efce26a087be . so in total what i want is this:

    but i cant figure it out.

  3. I looked up things like finding corners of parts, etc but wasnt able to find what i was looking for

The Code i have so far:

local function ReturnPos(part,base,pos)
	local o,size = part:GetBoundingBox()
	--print(size)
	size = part:GetPivot():VectorToWorldSpace(size)
	size = base.CFrame:VectorToObjectSpace(size)
	local objectspace = base.CFrame:ToObjectSpace(CFrame.new(pos))
	
	--size = Vector3.new(math.abs(size.X),math.abs(size.Y),math.abs(size.Z))
	
	--print(size)
	local MinX,MaxX
	local MinZ,MaxZ
	local Y = base.Size.Y/2+size.Y/2
	
	MinZ,MaxZ = size.Z/2-base.Size.Z/2,-size.Z/2+base.Size.Z/2

	MinX,MaxX = size.X/2-base.Size.X/2,-size.X/2+base.Size.X/2
	objectspace = CFrame.new(math.clamp(objectspace.X,MinX,MaxX),Y,math.clamp(objectspace.Z,MinZ,MaxZ))
	
	local worldspace = base.CFrame:ToWorldSpace(objectspace)
	return worldspace.p
end

Thanks in advance for any help.

1 Like

The following code should work.

local function ReturnPos(part, base, pos)	
	local angle = part.Orientation.Y

	local xSize = part.Size.X
	local zSize = part.Size.Z
	
	local angledX = zSize * math.sin(math.rad(angle)) + xSize * math.sin(math.rad(90 - angle))
	local angledZ = xSize * math.sin(math.rad(angle)) + zSize * math.sin(math.rad(90 - angle))

	local MinX,MaxX
	local MinZ,MaxZ
	local Y = base.Position.Y + base.Size.Y/2 + part.Size.Y/2

	local xAdj = base.Size.X/2 - angledX/2
	local zAdj = base.Size.Z/2 - angledZ/2

	MinX,MaxX = base.Position.X - (base.Size.X/2 - angledX/2), base.Position.X + (base.Size.X/2 - angledX/2)
	MinZ,MaxZ = base.Position.Z - (base.Size.Z/2 - angledZ/2), base.Position.Z + (base.Size.Z/2 - angledZ/2)

	local posX = math.clamp(pos.X, MinX, MaxX)
	local posZ = math.clamp(pos.Z, MinZ, MaxZ)
	
	return Vector3.new(posX, Y, posZ)
end

Sadly this doesnt work.
Im trying to clamp the position (and size) of a rotated part to the bounds of another rotated part

Have you tried getting it to work for a rotated part on an unrotated base?

If you can get it to work for that, it should be a simple modification to make it work on rotated bases too. I can fix this part for you if you successfully implement the code for the situation above.

image
it doesnt work on an unrotated basepart

i think i have an idea how i can make this work.


i would get all the corners then check them against each other for the furthest out one and then get the X and Y difference of each of them to the normal position and offset the clamp with that

okay in the end i was able to get it to work, thanks for helping.
I basically lopped through all corners and got the ones that was the furthest on x and z
Its prop not the optimal solution but atleast it works


local Vertices = {
	{1, 1, -1},
	{1, -1, -1},
	{-1, -1, -1},
	{-1, 1, -1}, 

	{1, 1, 1}, 
	{1, -1, 1}, 
	{-1, -1, 1},
	{-1, 1, 1}
}

local function GetSizeOffset(base,part)
	local results = {}
	local a = part.PrimaryPart
			local cframe,size = a.CFrame,a.Size
			cframe = cframe-a.Position
			for i,v in ipairs(Vertices) do
				local cf =cframe*CFrame.new(v[1]*size.X/2,v[2]*size.Y/2,v[3]*size.Z/2)
				table.insert(results,cf)
			end
	local minx,maxx,minz,maxz = nil,nil,nil,nil
	for i,v in ipairs(results) do
		if not maxx or v.X < maxx then
			maxx = v.X
		end 
		if not minx or v.X > minx then
			minx = v.X
		end
		if not maxz or  v.Z < maxz then
			maxz = v.Z
		end
		if not minz or  v.Z > minz then
			minz = v.Z
		end
	end
	return minx,maxx,minz,maxz
end

nvm… still got problems nice…
Epic text bec character limit yes

Okay now it finally works, i forgot to add the rotation of the basepart to the corner find

local function GetSizeOffset(base,part)
	local results = {}
	local a = part.PrimaryPart
	local cframe,size = a.CFrame,a.Size
	cframe = cframe-a.Position+base.Position
	cframe = base.CFrame:ToObjectSpace(cframe)
	for i,v in ipairs(Vertices) do
		local cf =cframe*CFrame.new(v[1]*size.X/2,v[2]*size.Y/2,v[3]*size.Z/2)
		table.insert(results,cf)
	end
	local minx,maxx,minz,maxz = nil,nil,nil,nil
	for i,v in ipairs(results) do
		if not maxx or v.X < maxx then
			maxx = v.X
		end 
		if not minx or v.X > minx then
			minx = v.X
		end
		if not maxz or  v.Z < maxz then
			maxz = v.Z
		end
		if not minz or  v.Z > minz then
			minz = v.Z
		end
	end
	return minx,maxx,minz,maxz
end

https://gyazo.com/ee290abec2c3f24c13029b1254d845a8

2 Likes