Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Check if a model is intersecting another model?

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?

3 comments

1

Downvote, no reason why. Thanks c:

0
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.

1 answer

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

3 comments

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. ;)

0

I figured there wasn't going to be any rotation involved other than 90 degree angles in his building game

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.