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?
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.
local Model1 = game.Workspace.Model1 local Model2 = game.Workspace.Model2 function checkForCollision(model1, model2) local collided = false local x, y, z = false, false, false 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 x = true -- X Axis Collision end 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 y = true -- Y Axis Collision end 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 z = true -- Z Axis Collision end if x and y and z then collided = true end return collided end checkForCollision(Model1, Model2)
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