Detecting Certain Parts in a Sphere

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to find a method that allows me to detect certain parts which cannot be detected except in a square using Region3 (seemingly).

  2. What is the issue? Include screenshots / videos if possible!
    Region3 doesn’t work, if I understand it correctly, it only detects within a square.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Still looking. I looked for solutions for my problem but I didn’t find any. I don’t need anyone to script it, would just like a point in the right direction.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

-- This is an example Lua code block

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

EgoMoose has a good custom GJK Region3 implementation that supports spheres, that you might want to check out:

1 Like

Thank you! Checking it out right now.

You could do a magnitude check, and then do some math to see if a part is in a sphere.

Edit: Here is a code example:

for i, v in pairs(workspace:GetDescendants()) do
    if v:IsA("BasePart") then
        local magnitude = (v.Position - Sphere.Position).Magnitude
        if magnitude <= (Sphere.Size.X + Sphere.Size.Y + Sphere.Size.Z) / 3 then
            -- Boom. Part is in the sphere

            -- Do something...
        end
    end
end
4 Likes

Have you tried using:

GetTouchingParts()

It can be used like this:

local Part = -- The sphere

local Parts = Part:GetTouchingParts()

if #Parts > 0 then
    for i,v in pairs(Parts) do
        print(v)
    end
end
3 Likes

I would agree that magnitude is the best option for a spherical check.

1 Like

I haven’t, but when I create a sphere part it is surrounded by the square highlight, which exceeds the physical sphere and was confused. Will the GetTouchingParts() include the parts that are also inside that highlight/selection or just within the physical sphere?

I’ll check it out, thank you.
30chars

The highlight is the bounding box, so it doesn’t affect it at all.

1 Like

You can use magnitude to check since it’s a sphere.

1 Like

Why is a magnitude check more preferred than the GetTouchingParts() function? It doesn’t look more efficient? Is there some kind of outside variable that may affect it?

Magnitude is going to be cheaper than using GetTouchingParts because it doesn’t preform any intersection tests, all Magnitude is the distance from 2 vectors.

Also i +1 GetTouchingParts, because you might run into some rotation and size issues/complications with just using Magnitude alone (if you care about accuracy).

1 Like

Well, it isnt preferred but its simpler, one downside to GetTouchingParts is that you cant use it on parts with can collide set to false.

Magnitude isn’t preferred, because then you would have to check lots of instances 10 times or more per second. Unless, of course, there are not many instances.

@FastAsFlash_Dev Yes, you can, just add a touched event:

local Part = -- The sphere with can collide set to false

Part.Touched:Connect(function() end)

local Parts = Part:GetTouchingParts()

if #Parts > 0 then
    for i,v in pairs(Parts) do
        print(v)
    end
end

You arent disconnecting the function which is expensive

I recommend this topic

Honestly, this is all great info to know. I’ll use magnitude as I’m already afraid the amount of tiles in the game may be costly.

What are you talking about? Every can collide true part has a touched event. In games there might be thousands of them! Please don’t blabber out of nowhere(sorry for being rude). The reason I didn’t disconnect it is because they might have to run the function the whole game.

@WarInEuropeRBLX If there are many tiles, using the GetTouchingParts() method would be better, unless you are only using magnitude occasionally. Imagine looping through 100 parts every second. But it won’t cause much of a difference.

How are you going to reference the part later and disconnect it? It’s not recommended to leave the signal running.

And also since op might use a lot of parts for it, having so many running signals is ridiculous

1 Like

Have you read my post?

Basically every part has a touched event, adding a few more does nothing.

True, but he might need it the whole time. The only time you disconnect it is if you were to never use it again.

The sphere will be created once and will run only to detect 19 particular parts within itself. It wouldn’t take a lot of resources, I think, but it would be best for the game, practice, and familiarity.

The sphere will afterwards be deleted after the detection, as it would no longer be used.

1 Like