Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

Check if a model is intersecting another model?

Asked by 10 years ago

I'm making a building game where you can click to place structures, but obviously you can't make the structures clip/intersect into each other. How would I check if two models are intersecting?

1
Downvote, no reason why. Thanks c: Nikola7007 25 — 10y
0
Please read this, it will help you. https://scriptinghelpers.org/help/how-post-good-questions-answers Perci1 4988 — 10y
0
To be honest, I don't know what I did wrong... Expect for that fact that there is no code to post, because I don't know where to start. Nikola7007 25 — 10y

1 answer

Log in to vote
1
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
10 years ago

There is one easier way to do this and another more complicated way. If you want to make it so models can overlap as long as no parts touch each other or have shapes more complex than boxes then you are going to need to set up your own bounds around the object to compare other object's bounds to. If you are doing something with uniform square or rectangular shapes, then it is much easier.

There are two nifty function of every model, one called GetExtentsSize which gives you the bounds of that model and the other called GetModelCFrame which gives you the CFrame of the model. However you should set the PrimaryPart of the model as the bounding box is retaliative to the rotation of the PrimaryPart. Here is how you would check if they collide.

01local Model1 = game.Workspace.Model1
02local Model2 = game.Workspace.Model2
03 
04function checkForCollision(model1, model2)
05    local collided = false
06    local x, y, z = false, false, false
07    if (model1:GetModelCFrame().p.X < model2:GetModelCFrame().p.X + model2:GetExtentsSize().X and model1:GetModelCFrame().p.X + model1:GetExtentsSize().X > model2:GetModelCFrame().p.X) or (model1:GetModelCFrame().p.X > model2:GetModelCFrame().p.X + model2:GetExtentsSize().X and model1:GetModelCFrame().p.X + model1:GetExtentsSize().X < model2:GetModelCFrame().p.X) then
08        x = true
09        -- X Axis Collision
10    end
11    if (model1:GetModelCFrame().p.Y < model2:GetModelCFrame().p.Y + model2:GetExtentsSize().Y and model1:GetModelCFrame().p.Y + model1:GetExtentsSize().Y > model2:GetModelCFrame().p.Y) or (model1:GetModelCFrame().p.Y > model2:GetModelCFrame().p.Y + model2:GetExtentsSize().Y and model1:GetModelCFrame().p.Y + model1:GetExtentsSize().Y < model2:GetModelCFrame().p.Y) then
12        y = true
13        -- Y Axis Collision
14    end
15    if (model1:GetModelCFrame().p.Z < model2:GetModelCFrame().p.Z + model2:GetExtentsSize().Z and model1:GetModelCFrame().p.Z + model1:GetExtentsSize().Z > model2:GetModelCFrame().p.Z) or (model1:GetModelCFrame().p.Z < model2:GetModelCFrame().p.Z + model2:GetExtentsSize().Z and model1:GetModelCFrame().p.Z + model1:GetExtentsSize().Z > model2:GetModelCFrame().p.Z) then
View all 25 lines...

Now every time the model is moved you can call this function to determine if it should be able to be placed there or not.

NOTE This provided algorithm only works on models parallel/perpendicular to the world axis

0
I'm not sure how useful axis-aligned collision logic is going to be, but the questioner hasn't specified any criteria so you can have my vote. ;) duckwit 1404 — 10y
0
I figured there wasn't going to be any rotation involved other than 90 degree angles in his building game BlackJPI 2658 — 10y
0
Yeah, it's very simple building at the moment. No elevation change or rotation. This looks great though, I'll try it out tomorrow. Thanks! Nikola7007 25 — 10y
Ad

Answer this question