How do I find the absolute and the relative thickness of a part?

i am stuck trying to figure out how to find the relative and absolute thickness from a part from a raycast, see below:


what i’m trying to find is the distance of the section of the red line that is IN the box, marked with green, i am aware that the formula for relative thickness is this (in 3 dimensions)
image
where T is relative thickness, w is actual thickness, the vector h is the direction from where i fired it, and n is just the raycast normal. that is a dot product by the way, also i don’t know THAT much about things like these, programming these kinds of stuff is disasterous

if you have any solutions in mind, thank you and please put them here!

4 Likes

I personally am not that great when it comes to this kind of math, if it even is possible like that. However I can think of a relatively fast work-around. Once you hit the part, you can cast a new raycast from further down the ray backwards, and get the hit point there. Then just simply find the distance between two hit positions. The raycast would need to only have the part as a possible target, so you can set FilterDescendantInstances to that one part and FilterType to Include, so you’d only raycast with that one part, which should be quite fast.

2 Likes

What Hannes said. It probably won’t be the most performant but will save you some very valuable time.

local RP = RaycastParams.new()
RP.FilterDescendantsInstances = {Part}
RP.FilterType = Enum.RaycastFilterType.Include
local R1 = workspace:Raycast(pos1, pos2-pos1, RP)
local R2 = workspace:Raycast(pos2, pos1-pos2, RP)
if R1 and R2 then
	local Distance = (R1.Position - R2.Position).Magnitude
	print("Thickness: "..Distance)
else
	print("Line did not cross the part")
end
1 Like

i tried solving this with my older brother to not that much of an avail, i have looked into the raycasting behind part method but the specific requirements on this are for it to get the actual thickness without too many hiccups. a problem i can see arising from this is 1. what if another part interrupts the other raycast and 2. what if the other caster’s origin is literally occupied? a solution that immediately came to mind was just making the casters ignore any parts that aren’t the target part itself, however this will not be very performant with many parts. something i actually just realized is that my relative thickness formula as i programmed it was wrong, here is my code

local caster = script.Parent
local castPoint: Attachment = caster.Attachment
local initialCast = workspace:Raycast(castPoint.WorldPosition, castPoint.WorldCFrame.LookVector * 1600)
local initialCastHit = initialCast.Instance :: BasePart
local castThicknessVisual = Instance.new("Part")

local function normalIdFromVector(vector)
	local epsilon = 0.01
	for _, normalId in pairs(Enum.NormalId:GetEnumItems()) do
		if vector.unit:Dot(Vector3.FromNormalId(normalId)) > 0.05 then
			return normalId
		end
	end
end

local function getThickness(cast: RaycastResult, castPoint: Part)
	if not cast then
		return nil
	end
	
	local thickness = 0 -- pLAVEOHLDER
	
	
	return thickness  -- Use the absolute value of the dot product
end

castThicknessVisual.Anchored = true
castThicknessVisual.Color = BrickColor.new("Bright red").Color
castThicknessVisual.Name = "Marker"
castThicknessVisual.Size = Vector3.new(1, 1, 1)
castThicknessVisual.Transparency = 0.5
castThicknessVisual.Position = initialCast.Position + (castPoint.WorldCFrame.LookVector * (getThickness(initialCast) / math.sin(math.rad(90) - math.rad(initialCastHit.Rotation.X))))
castThicknessVisual.Parent = workspace

and the getThickness() function was kinda just wiped out of existence. thank you for your responses, but i’m about to look into this a bit deeper to see what went wrong

2 Likes

since you are only trying to find the thickness of one part, you can cast the ray with parameters of filtertype = include and filterdescendantsinstances = {only that one part}.

you would cast the second ray from the first ray’s hit position + (the first ray’s direction).Unit * maximum thickness of your part, in the direction of -(the first ray’s direction)

i don’t know who told you that, but raycasts via worldroot:Raycast() are very efficient. I’d look up some benchmarking people have done previously if youre unsure.

Keep in mind that this method will only work for convex shapes but i’m pretty sure its the industry standard getting part thickness

2 Likes

you are smarter than me. this solution is actually more useful, thank you for telling me this (the exclude and filter through method was actually dumb realizing Include exists)

3 Likes

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