Is it possible to check if 2 parts overlap with code only?

Hello guys. I’m fixing bugs in my game’s building system before test release.
But I got one major bug which IDK if I’ll be able to fix.

My game has client sided building - I choose that because it’s fast, and can be done at zero position. Cuz players can cheat with their builds, I did server recorder which checks if smth is able to be placed, and if not - rejects changes.
But, I forgot that I should have build zone, and players shouldn’t be able to exceed it. That’s where problems come.
I need to check if Part is inside build zone (which has cube shape, with N size). Cuz client building is unreliable, I must do check on inside zone being on server, via script. But I need some help with understanding how I can do that with CODE ONLY, without the use of any instances. And it won’t be that hard, but I have some complex primitives, such as dodecahedrons, slopes, Half-corner-cylinder, and also models, such as chests, tables, machines.
Is it possible to do such check in any way, even with hard calculations?

2 Likes

If a non-spherical shape is outside of the building zone in any way, at least one of its corners must also be outside of the building zone. This means that for any given shape you could loop through all of its corners and check if any of those corners is not inside the box.

For a sphere, you should first make sure the center of the sphere is inside the box. Then, check if the distance between the sphere’s center and each of the box’s planes is greater than the radius of the sphere.

An ellipse would be a bit trickier. However, you can actually simplify the calculation to that of a sphere by converting the box and the ellipse’s coordinates/sizes into a vector space where the ellipse is a sphere of radius 1. To achieve this ,simply multiply the size and position vectors of both the box and the sphere by Vector3.new(1/ellipse.Size.X, 1/ellipse.Size.Y, 1/ellipse.Size.Z), then do the original sphere calculations with those new vectors.

Hope this helps!

4 Likes

Sadly, such checks can be applied only to relatively simple shapes. It’s ofc possible to do with more complex ones, but this will require really ton of work specifying corners.


There’s example of shapes I have, not including hundreds of models with their own, complex hitboxes, dynamically created tools, and many other features I have somewhy implemented.

3 Likes

I believe you could use :GetTouchingParts() on the zone. Loop through the entire table, and check if the object which is currently being iterated over is the part you are checking for.

2 Likes

But this works only if parts are actually exist. I work with direct data values, like Position, Size, CFrame, which are stored in table. No parts are created on server.

3 Likes

If you don’t want to specify a ton of corners and are restricted to using code only, you should probably use axis aligned bounding boxes (AABBs). AABBs are essentially a rectangular prism that approximates a shape. They’re very easy to define and scale, and very efficient to detect intersection with. Here’s a document that explains AABB detection, and here’s an example of it in use.

Edit: Fixed link to AABB example. Also, you should check the far face instead of the close face during the AABB check in order to ensure the whole model is inside the build zone.

4 Likes

After reading that, I think I’ll do AABB for complex models, and corner specifying for simple shapes.

1 Like

Just use spatial queries. They’re pretty good but if you’re looking for speed you should write your own spatial queries using math since Roblox spatial queries are kinda slow. My friend had custom spatial queries that he wrote which are 30x faster than the roblox spatial queries so I’ll see if he can give me the code.

2 Likes

If you have an area you want to be said zone just put a part there and then you can use workspace:GetPartsBoundInBox().

If said thing is in the zone then you can place it, if not then dont place it.

1 Like

You know the size and position of your build zone, and the size and position of your part, just use math to see if the parts position and size are within the bounds of the build zone. Build on client, then verify on server.

2 Likes