So im making a game where players place down buildings of different sizes and right now i dont have any way to detect collisions so is there a way for me to accurately and efficiently detect collisions? right now i dont have anything but i am looking for what method of doing this would be most efficient provided i have a large map.
collision detection help?
1 answer
Axis Aligned Collision Detection
If you are only placing buildings on a grid that is aligned with the global axes, then the collision detection is quite simple. All you need to check is if there is a collsion on each axis. If there is an overlap on all of the axes, then you can say that the two bounding boxes collide.
function checkForCollision(model1, model2)
local collided = false
local x, y, z = false, false, false
local size1, size2 = model1.PrimaryPart.Size, model2.PrimaryPart.Size
local pos1, pos2 = model1.PrimaryPart.Position, model2.PrimaryPart.Position
if (pos1.X < pos2.X + size2.X and pos1.X + size1.X > pos2.X) or (pos1.X > pos2.X + size2.X and pos1.X + size1.X < pos2.X) then
x = true
-- X Axis Collision
end
if (pos1.Y < pos2.Y + size2.Y and pos1.Y + size1.Y > pos2.Y) or (pos1.Y > pos2.Y + size2.Y and pos1.Y + size1.Y < pos2.Y) then
y = true
-- Y Axis Collision
end
if (pos1.Z < pos2.Z + size2.Z and pos1.Z + size1.Z > pos2.Z) or (pos1.Z < pos2.Z + size2.Z and pos1.Z + size1.Z > pos2.Z) then
z = true
-- Z Axis Collision
end
if x and y and z then
collided = true
end
return collided
end
In this example I used a PrimaryPart in each of the models that had completely encased the entire model, such that all parts were physically inside of the PrimaryPart and the outermost portions were lined up with the faces of the PrimaryPart. You could change it to use the method of model GetExtentsSize in order to determine the size, and find the position else how, but this seemed like to easiest way to do it for me.
This can be simplified down to use only one if statement, but I figure it's easier to visualize what all the different conditions are actually representing this way.
Collisions and Arbitrary Rotations
Detecting collisions starts to become a quite a bit more difficult when your bounding boxes are rotated at some arbitrary angle in each axis. It isn't as simple as a check which axis have overlaps, as both parts could have their own separate local axes.
The easiest way to do this, is probably by using the property of BasePart GetTocuchingParts. You'll just have to look through the parts and see if anything that should collide with it is touching it.
However, you should note that this method only captures parts that have CanCollide set to true.
1 comment
thanks this helped alot
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
Have you tried using raycast? http://wiki.roblox.com/index.php?title=API:Class/Camera/ScreenPointToRay
i looked into but then i realized for every time something was placed 4 rays would need to be cast and even then it might not work with larger buildings