Block Collisions

I’m trying to build an advanced build system where the player has a number of options like, collisions, grid Size, Block Size, Block Color and more. One thing I have been struggling with is the collisions, I want to make it so if the block position is inside of an other block, the block fixes its position to nearest position and or orientation of the block not being in an other block, if that makes any sense at all, could I get some help with my issue, thanks :kissing_heart:.

The direction you need to “push” is the normal of the closest face. This might help you:

--Snap vector to cardinal unit length vector such as (1, 0, 0)
local function VecSnap(norm : Vector3)
	if math.abs(norm.X) > math.max(math.abs(norm.Y), math.abs(norm.Z)) then
		return Vector3.xAxis * math.sign(norm.X)
	elseif math.abs(norm.Y) > math.abs(norm.Z) then
		return Vector3.yAxis * math.sign(norm.Y)
	else
		return Vector3.zAxis * math.sign(norm.Z)
	end
end

As for orientation, if you use CFrame.lookAt in the direction of the face normal, the parts will have faces aligned. You’re going to realize the hard part is actually calculating how far to push the block so the surfaces touch exactly, though.

When I used this, I put the blocks positions on norm, and set the blocks position to the vecsnap, But the block kept on changing its position and had started like vibrating, Did i do something wrong?

VecSnap gives you the closest face normal if you feed it the difference between a point and the center of the block, or more accurately, if you give it a point in the block’s local space (CFrame:PointToObjectSpace()), it will give you the block local space normal of the closest face. You can turn this back into a world space position to use as an offset direction. If the size of any block is the same in all 3 directions, you just need to multiply that offset by that width.

I have gotten the rotation of the block, but I’m still confused on the vec snap.

I’ll get back to you with an example in a sec.

ok sounds good, I’ll be gone for like 10 - 15 minutes

Move the white point around to see where the block being placed would snap.

ok, I see what you mean, but I’m not sure how this could help me with collisions

What do you mean by collisions.

well, I’m trying to make it so the block can collide, so like if its about to collide/go in side of another block then it fixes it position

I think you need to spend some more time looking at the example I gave.

how do I know what block to fix its self on

Use raycasting to to determine which block is under the player’s mouse.