Finding if a portion of a part is inside cone-like angle

(Sorry for the weird name, I struggled to find one :sweat_smile:

  1. What do you want to achieve?

I want to check if a part is partly inside a cone shape (Think NPC Field of view)
image

  1. What is the issue? Include screenshots / videos if possible!

Currently my code only detects the origin of the object, and not if a corner is inside the cone.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Dot Product - Currently I’m using dot product, where I use the direction between the purple part and blue part, and the lookvector of the purple block. After that I check if the dot product is within the angle. Problem is this doesn’t work if the parts origin isn’t inside the angle.

Clamped raycasting - I clamped the direction between the blue part and purple part of the raycast to only be within the cone’s angle. It did work, however I’d like to find if there is anything less performance-heavy (I know it isn’t very performance heavy, but hundreds each frame wouldn’t be the best)

Weird PartsInPart + Cone mesh deformation - Haven’t actually put this into action, but maybe I could try to make a literal cone mesh and deform it into whatever angle I need, then just GetPartsInPart, however this likely will be the least optimized version.

Most accurate way to do this would to be to use WorldRoot:GetPartsInPart() with a cone mesh as you’ve said. However, with a far distance parameter, the drop in performance may quickly become an issue. For a less accurate and more performant method, you could perform the dot product check on all 8 vertices on bounding boxes. Either depends on the use case of the FoV check system, how you want to implement it, etc. Unfortunately, Roblox doesn’t have better methods without some kind of major cost.

2 Likes

GetPartsInParts takes an OverlapParams parameter that lets you specify either a blacklist or whitelist of parts to either ignore (blacklist) or exclusively check against (whitelist). Using a whitelist with only the single Part should (IMO) make it perform well in all situations, no matter how large the cone or how many other parts are potentially inside it.