How to find corners of part?

I want to find corners of part but I dont know how. So I have a part in workspace and I know its position and size, and I need to find 8 corners positions like Vector3 values, but position and size are vector3 too. This is my part (the red and blue circles are corners I need to find positions of):


Thank you for any help.

8 Likes

You can have a look here: Checking corners of a Part?

1 Like

Thank you for your solution but I cant understand the code because Im not very good at vectors. I’d be happy if you could explain how it works :smiley:

1 Like

Sure,

On the solution, see you can see that you can get one corner by using:

local Size = Part.Size
local CornerPos = (Part.CFrame * CFrame.new(Size .X/2, Size .Y/2 , Size .Z/2)).Position

That will give you one corner so all you have to do is indicate where each corner using a table!
After you just need to multiple each vertex by the corresponding value:

for _, Vector in pairs(Vertices) do
local CornerPos = (Part.CFrame * CFrame.new(Size .X/2 * Vector[1], Size .Y/2 * Vector[2], Size .Z/2 
Vector[3])).Position
end

If you wanted to get/find the bottom front right corner, then you would multiply the top front right corner (the corner got from doing local CornerPos = (Part.CFrame * CFrame.new(Size .X/2, Size .Y/2, Size .Z/2)). Position, by that. So if the top front right corner had a position of 10, 10, 10, then the bottom front right corner would have a position of 10, -10, -10!

I hope this helps!

This post simplifies it a bit, Simplifying Code? - #3 by TOP_Crundee123

1 Like

Do you have any idea on how I wrap it into a function ? Like: function getcorners(part) … end
Because I need it to be a function returning a table with corners, so I can take a corner key from table and get its position as value…

so how do i find corners of part? it didn help

The previous link that @luckysuperman2 had the answer:

What part of that answer are you having trouble with?

I dont understand it it is complicated. Basically, I want just a simple function that returns a table, so i can use keys as corner names to refrence vector3s.

I probably didnt make it clear , sorry. You dont have to solve it for me though.

As of right now I almost found a solution myself, it involves lookvector/upvector/rightvector. I will add them to part’s cframe and multiply by value of part’s half size on that axis to get part’s face center. Then do this 2 more times but with other vectors and axes and so I get to my target corner. Now I just need to repeat it for every corner but in different order.

I will send my solution in here when its ready.

I understand what your question is. The answer I linked gives you the exact code you need, though. You can put it in a function if you want:

local Vertices = {
	{1, 1, -1},  --v1 - top front right
	{1, -1, -1}, --v2 - bottom front right
	{-1, -1, -1},--v3 - bottom front left
	{-1, 1, -1}, --v4 - top front left
	
	{1, 1, 1},  --v5 - top back right
	{1, -1, 1}, --v6 - bottom back right
	{-1, -1, 1},--v7 - bottom back left
	{-1, 1, 1}  --v8 - top back left
}

local function GetCorners(Part)
    local corners = {}
    for _, Vector in pairs(Vertices) do
        table.insert(corners, (Part.CFrame * CFrame.new(Size .X/2 * Vector[1], Size .Y/2 * Vector[2], Size .Z/2 * Vector[3])).Position)
    end
    return corners
end

Then call GetCorners to get a table of the corners. Again, credit to @VegetationBush.

This isnt quite what I want. It doesnt have to involve loops. Just let me solve it myself and I will send it in here.

Done:

function getCorners(part)	
	local cf = part.CFrame
	local size = part.Size
	
	local corners = {}
	
	-- helper cframes for intermediate steps
	-- before finding the corners cframes.
	-- With corners I only need cframe.Position of corner cframes.
	
	-- face centers - 2 of 6 faces referenced
	local frontFaceCenter = (cf + cf.LookVector * size.Z/2)
	local backFaceCenter = (cf - cf.LookVector * size.Z/2)
	
	-- edge centers - 4 of 12 edges referenced
	local topFrontEdgeCenter = frontFaceCenter + frontFaceCenter.UpVector * size.Y/2
	local bottomFrontEdgeCenter = frontFaceCenter - frontFaceCenter.UpVector * size.Y/2
	local topBackEdgeCenter = backFaceCenter + backFaceCenter.UpVector * size.Y/2
	local bottomBackEdgeCenter = backFaceCenter - backFaceCenter.UpVector * size.Y/2
	
	-- corners
	corners.topFrontRight = (topFrontEdgeCenter + topFrontEdgeCenter.RightVector * size.X/2).Position
	corners.topFrontLeft = (topFrontEdgeCenter - topFrontEdgeCenter.RightVector * size.X/2).Position
	
	corners.bottomFrontRight = (bottomFrontEdgeCenter + bottomFrontEdgeCenter.RightVector * size.X/2).Position
	corners.bottomFrontLeft = (bottomFrontEdgeCenter - bottomFrontEdgeCenter.RightVector * size.X/2).Position
	
	corners.topBackRight = (topBackEdgeCenter + topBackEdgeCenter.RightVector * size.X/2).Position
	corners.topBackLeft = (topBackEdgeCenter - topBackEdgeCenter.RightVector * size.X/2).Position
	
	corners.bottomBackRight = (bottomBackEdgeCenter + bottomBackEdgeCenter.RightVector * size.X/2).Position
	corners.bottomBackLeft = (bottomBackEdgeCenter - bottomBackEdgeCenter.RightVector * size.X/2).Position
	
	return corners
end

Testing:

for cornerName, cornerPos in pairs(getCorners(workspace.CornersPart)) do
	newPart(cornerPos)
end

Final results:

27 Likes

I feel for your fingers.

There is a special function of CFrame called CFrame:PointToWorldSpace()

Basically it transforms the entire world to the part. This makes it easy to get Position relative to the part.

Also, this is if you want to get a Direction relative to the part CFrame:VectorToWorldSpace()

-- Pro tip: if you find yourself repeating code there is probably a better way of writing it.
local FRT = Part.CFrame:PointToWorldSpace(Vector3.new(Part.Size.X/2,Part.Size.Y/2,Part.Size.Z/2)
local Vis = Instance.new("Part",workspace)
Vis.Size = Vector3.new(0.1,0.1,0.1)
Vis.Position = FRT
3 Likes

I will look into it. Thank you for your tips.

1 Like

does this work on complex shapes?

@sktvladimir535 No, only cubes. However, it does support CFrame translations such as Position and Rotation. Please do some testing yourself before you bump a 3-year-old post.

Also, I want to thank you, @koziahss because this helped me out tremendously with one of my projects. Thank you so much <3