Most efficient way of detecting a part in an area

Hello!

I’m trying to find the most efficient way of detecting if a part overlapps an area. I already tried the following method but they all have a drawback :

Raycasting : Very fast an efficient but it only detect object in front of it so I misses a lot.
GetPartsInPart : This technic does what I need to do but It is not efficient at all. Reading a small mesh on only 2 sides took half a second.

Would there be a more efficient way of doing that?

Thanks !

You could do multiple raycasts being seperated at a calculated distance so the part couldn’t be missed

not sure if that would be very performance heavy or not

1 Like

Like a touching function? Im not very good but this should work and isn’t that slow!

game.workspace.anypart.Touched:Connect(function(Part)
print(Part) 
end) 

Now I don’t think that’s the best solution but it works if the part was moving into…

Raycasts are very efficient ~250 in 0.02 seconds so I could maybe use them. They could still miss but it would be rare so it is an option I’m considering.

In my case, I’m trying to approximatly read a mesh collision to detect hitpoints so this won’t work.

Theres a way to actually send a raycast towards an object.

wasn’t there a onTouch() function?

https://developer.roblox.com/en-us/api-reference/event/BasePart/Touched
this one is kinda similar

Yeah, I know how to do that but it “won’t work” in my case as in need more of an area.

This is more of what I need, but what you are saying is kinda similar which is were the “” come from.

This only detects collision with physic I think but I’m not sure.

Nope, it activates on touch (its related to the “canTouch” button that shows up if you turn off can collide)

I was right about that it only detects if it is physically simulated but I’m going to try to use that to check if it is touching and see if it is any better.

I tried it and it doesn’t work as physic is simulated each frame; 1 frame has to pass between each check so 60 check == 1 second. I will try the raycast tough and see if it is more effective.

I may be wrong, but wouldn’t using GetPartBoundsInBox work well? It works similar to GetPartsinPart but I’m fairly certain that for actions liike this it’d be more efficient.

https://developer.roblox.com/en-us/api-reference/function/WorldRoot/GetPartBoundsInBox
a link to read up on it

1 Like

whay

use FindPartsInRegion3

it only works for cuboid stuff

local PartTarget = game.Workspace.Part
local Part = game.Workspace.Part2
local Region = Region3.new(Part.Position - (0.5 * Part.Size), Part.Position + (0.5 * Part.Size))

local PartsInRegion = workspace:FindPartsInRegion3(Region, nil, math.huge)

if table.find(PartsInRegion, PartTarget) then
	print("the ")
end

Well as I don’t really like multiple raycasting method, I do this kind of stuffs like this,

1, Get 8 Positions of a part’s corners.

So for this, you can use this script.

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
}

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

2, Check if all 8 vectors are in the specific area. (Or more than 1 vector)

And for this, you acn use this one.

function isInsideBrick(position, brick)
	local v3 = brick.CFrame:PointToObjectSpace(position)
	return (math.abs(v3.X) <= brick.Size.X / 2)
		and (math.abs(v3.Y) <= brick.Size.Y / 2)
		and (math.abs(v3.Z) <= brick.Size.Z / 2)
end

So you can combine those 2 scripts properly to check a part is in an area without multiple raycasting or region3 thing.

2 Likes

Use the zone+ Module. It has a feature that allows you to detect if parts enter other parts.

for ex:

partContainer.itemEntered:Connect(function(item)
     print(item.Name)
 end

In my case, I’m trying to get the approximate mesh geometry. GetPartBoundsInBox only check if it is touching the bounding box (blue selection box) so this won’t work.

It might help if you described exactly what application this would be for, so that people don’t need to blindly suggest options :slight_smile:

I’m trying to get an approximation of a mesh geometry to be able to then detect the hitpoints when it gets hit. My way of doing this is using a cube/area to check if it is overlapping the mesh to know if there is something or not.

This is the current result and is exactly what I need but it is a bit slow 0.075s per mesh which is why I made this post to try finding a more efficient way of doing this.

Doesn’t roblox already compute an approximate mesh collision with CollisionFidelity? What is the purpose of the custom collider?