Ray not detecting edge of part; bug or not?

I’m currently making a script that detects if any two parts are intersecting by using ray casts from every vertice to the center of another part. I’ve come among a bug tho where if a ray intersects a part through an edge then it doesn’t hit; like this:

I’ve tried using the old raycasting but that didn’t change anything; I’ve also moved the part to ensure that the problem is the angle the ray enters the part.

Does anyone know a way around this bug or not a bug?

Heres the exact code that I use to detect if the point is in the part:

local rayPerms = RaycastParams.new()
rayPerms.FilterType = Enum.RaycastFilterType.Whitelist
rayPerms.FilterDescendantsInstances = {part.Part}

local ray = workspace:Raycast(pos, part.CenterOfMass - pos, rayPerms) -- Part.CenterOfMass == Part.Position
if not ray then
	CreateOutlineTwoPoints(pos, pos + (part.CenterOfMass - pos), .03, workspace, Color3.new(1, 0.533333, 0))
	error()
end

Please help and thank you in advance!

Can’t tell what’s going on in the image, or in the code for that matter. What is “pos”?

Anyway, are you working with Parts only? If so you might want to check out

https://developer.roblox.com/en-us/api-reference/function/WorldRoot/ArePartsTouchingOthers

https://developer.roblox.com/en-us/api-reference/function/WorldRoot/GetPartsInPart

and the other WorldRoot methods.

1 Like

pos is a vector 3 argument I passed in. I’m checking if pos is in the part by casting a single ray from pos to the center of the part. If the ray doesn’t hit then it makes a line/part representing the ray, which is what the image shows. sadly neither of resources you shared would help since I’m not trying to detect if two parts are touching; just which vertices are in the other part. sorry that I didn’t make that clear in the beginning.

Since I don’t need a position or a normal when detecting if the point is in the part; I used a region3 with 0,0,0 size. Sadly this didn’t work for wedges but it did for boxes so I just checked the part type and used rays and region3 accordingly. Though rays not detecting edges is problematic; if anyone finds a way around this, please say something.

Oh, sorry I misunder stood then.

If you want to check if a point is inside a normal Part, you can convert it to the Part’s object space and just use AABB point-box collision detection.

It’s normal for rays to not detect parts if they start inside them or exactly at their surface. If you want to detect those situations, you must do another raycast which starts a bit outside the part and which goes through the surface you want to detect. Then compare the detected surface point to the point you want to check, and if they’re the same then your ray started at the surface of a part.

I don’t know if raycasts will detect rays that go through edges. If they do, you can use the same approach to see if a ray starts at an edge.

2 Likes

Could you elaborate on this:

If you’re talking about a point being in the bounding box of the part, I still need it to work for things like wedges and spheres.

I have the code return not ray, so if the ray hits, that means the point is outside of the part. As you can see in the code, the if statement checks if the point is in or outside the part, and since the ray never hit, it made the outline (that’s what the image shows). I also already have a piece of code that detects if the point is on the surface of the object, though your approach could be faster so I might change it.

They don’t, and that’s what I’m trying to address in this post. How do I detect if a ray is going through an edge?

You can pretty easily incorporate spheres and wedges into an AABB check after converting it to object space. For spheres just compare distance to center against its radius. For wedges check if the height of the diagonal edge at any point is greater than or less than the point using basic geometry.

1 Like

But then this would limit me to just basic geometry. Whenever I make a mesh, even just like a basic pentagon, I would have to implement its own detection for each unique shape or not be able to do it at all because the mesh is too complex or concave. it would be much better to just use raycast one time instead of implementing each and every function for each and every shape.

IIRC there’s an algorithm for that here: https://geomalgorithms.com/

Well, not exactly but it has them for line-line and segment-segment collision detection. Look for dist3D_Segment_to_Segment() and try adapting it to ray-segment detection.