I want to be able to get the length, width and hence area of a surface of an object that I have my cursor on. For example I have my mouse on the right face of a rectangular prism part. In this case I can simply determine which face I have the cursor on with Mouse.TargetSurface and then use the object’s Z and Y sizes for my length and breadth. The issue I have is when there are more unique shapes like wedges where the right, up, and look vectors are not aligned with the faces of the object and I can no longer use the size of the object to find length and width.
I am aware I can just use pythag on shapes like wedges but I would rather not brute force a calculation for each individual shape in my game. Is there any way to easily get the length and width of any 2D surface (excluding circular/curved objects)?
You need this just for parts, right? Assuming this based on you saying excluding circular/curved objects.
--!strict
local player = game.Players.LocalPlayer or error("Could not get player")
local mouse = player:GetMouse()
-- Obtain the dimensions of the face of a Part
local function getFaceDimensions(part: BasePart, face: Enum.NormalId): (number, number)
local size = part.Size
-- Determine which axes correspond to the face dimensions
if face == Enum.NormalId.Top or face == Enum.NormalId.Bottom then
return size.X, size.Z
elseif face == Enum.NormalId.Left or face == Enum.NormalId.Right then
return size.Z, size.Y
elseif face == Enum.NormalId.Front or face == Enum.NormalId.Back then
return size.X, size.Y
end
-- fallback to top face
return size.X, size.Z
end
-- Event when player clicks
mouse.Button1Down:Connect(function()
local target = mouse.Target
local face = mouse.TargetSurface
if target and face then
local width, height = getFaceDimensions(target, face)
print(`Face dimensions - Width: {width}, Height: {height}`)
end
end)
This seems to work for me. I’ve put it to on mouse click so you can easily shove it into a LocalScript and run it. For the other shapes like wedges, you should be able to also just use formulas other than than L*W.
a^2+b^2=c^2 shouldn’t be too terribly intensive
Sorry if I misunderstood your problem.
edit: yes, i did in fact misunderstand. I’ll try to see if I can extend what I have
edit 2: i forgot how to find the area of a side of a wedge part (the rectangle side, not the triangle side), i probably won’t be able to extend my code
if you only have to calculate each time a new part is hovered over, so it should be fine for performance
maybe you can determine the face by adding the lookvector/rightvector/upvector to the position of the part and seeing which one is closer to the mouse hit position? Then you can determine which side of the shape the mouse points at
Getting the side can be done with mouse.TargetSurface. Somehow it seems that we both misunderstood OP. After I re-read his question it seems like he already has something similar to what I wrote, but he wants it to work for WedgeParts and CornerWedgeParts too.
Yeah I was looking for a way to do it without the use of making a formula for each individual shape so that I could do it even if the shape was something ridiculous like a star prism but I think the only way is to do seperate code for each shape . I am good at math but new to scripting so I was hoping there was some in built function for this but it seems that there is not.
Yeah I can get the side of the face easily, but getting the legnth and bredth of said face is what I am trying to achieve without making different equations for each shape type.
Do you need precise length and width or only surface area?
I think just .Size or :GetExtentsSize for models can get length and width
And if you only want the area of a 2d surface you can use a monte carlo method where you plot random points in the bounding shape and then calculate how many points are on the shape