How can I make my wall slicer cut properly regardless of the wall's orientation to the window?

Hi,

I’m currently working on a wall cutter that cuts a part for windows, however I for some reason can’t think of how I’d go about making it work regardless of the window part’s orientation.

Currently, my methodology is this for the left face, right part is the same but opposite operations:

    -- boundingBox is the window's bounding box (the size of the part that the wall should be sliced around)
    -- origin is just the part to be cut's CFrame
    -- get the left face of both the wall and the window's bounding box
    local windowLeft = boundingBox.CFrame * CFrame.new(-boundingBox.Size.X / 2, 0, 0)
	local wallLeft = wallPart.CFrame * CFrame.new(-wallPart.Size.X / 2, 0, 0)
    -- relative to the wall
	local wallLeftRelativeToWall = origin:ToObjectSpace(wallLeft)
	local windowLeftRelativeToWall = origin:ToObjectSpace(windowLeft)
    -- if the part to be cut's left face is further than the wall's leftmost face, we don't cut it because there would be a very thin part that's to the right of the part, just what we don't want
	if wallLeftRelativeToWall.Position.X < windowLeftRelativeToWall.Position.X then
		local part = wallPart:Clone()
		part.Parent = self._cutResults

        -- get the difference between the leftmost face of the wall and the window which is the length of the left part
		local difference = windowLeftRelativeToWall.X - wallLeftRelativeToWall.X
        -- sizing of course
		part.Size = Vector3.new(
			difference,
			part.Size.Y,
			part.Size.Z
		)
		part.Transparency = 0
		part.CanCollide = true
        -- finally, position it to be positioned between the part's leftmost face and the wall's leftmost face
		part.Position = origin:PointToWorldSpace(
			Vector3.new(
				wallLeftRelativeToWall.Position.X + difference / 2,
				0,
				0
			)
		)
	end

This works for one wall, but because my walls are oriented away from each other, only one wall is cut properly, the other wall has overlap because of the inverted CFrame. So in result, I get this:

As you can see, only the part facing me is cut properly, the part facing away from the camera is not cut properly and extends past the part’s bounds. I know why this happens, it is due to the part’s orientation to the wall’s CFrame. That’s why rotating the part 180 degrees makes the other part cut properly but makes the issue happen on the wall that worked correctly before.

Each of the wall’s parts’ LookVectors are like this, pointing away from each other:

What I need is a solution to this, I can switch the right and left part calculations, I just need to know when to switch them. Any ideas?

1 Like

Okay I actually found when to switch, I don’t know why I didn’t think of it before. When correctly slicing, windowLeftRelativeToWall.X > windowRightRelativeToWall.X is false, however when incorrectly slicing, it is true. So windowLeftRelativeToWall is usually 4 studs less than right when placing a 4-stud-wide part.

So basically when windowLeftRelativeToWall is greater than windowRightRelativeToWall, switch the axes (which is what this line does):

if windowLeftRelativeToWall.X > windowRightRelativeToWall.X then
	windowLeft, windowLeftRelativeToWall, windowRight, windowRightRelativeToWall = windowRightRelativeToWall, windowRightRelativeToWall, windowLeft, windowLeftRelativeToWall
end

And a video:

1 Like