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 9 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 — 9y
0
Please read this, it will help you. https://scriptinghelpers.org/help/how-post-good-questions-answers Perci1 4988 — 9y
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 — 9y

1 answer

Log in to vote
1
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
9 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.

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

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 — 9y
0
I figured there wasn't going to be any rotation involved other than 90 degree angles in his building game BlackJPI 2658 — 9y
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 — 9y
Ad

Answer this question