Triangle And Node Help!

Hello! In this video you can see the color blue which is the LEFT surface of the wedge and green which is the RIGHT surface. While moving my nodes around, you can see the wedge flips upside down. I’m using Egomooses triangle function. Does anyone know how I can prevent the flipping?

function service.drawTriangle(a,b,c,w1,w2)
	local ab, ac, bc = b - a, c - a, c - b
	local abd, acd, bcd = ab:Dot(ab), ac:Dot(ac), bc:Dot(bc)

	if (abd > acd and abd > bcd) then
		c, a = a, c
	elseif (acd > bcd and acd > abd) then
		a, b = b, a
	end

	ab, ac, bc = b - a, c - a, c - b

	local right = ac:Cross(ab).unit
	local up = bc:Cross(right).unit
	local back = bc.unit

	local height = math.abs(ab:Dot(up))

	w1.Size = Vector3.new(THICKNESS, height, math.abs(ab:Dot(back)));
	w1.CFrame = CFrame.fromMatrix((a + b)/2, right, up, back)

	w2.Size = Vector3.new(THICKNESS, height, math.abs(ac:Dot(back)));
	w2.CFrame = CFrame.fromMatrix((a + c)/2, -right, up, -back)

	local modifier = ((w1.CFrame * CFrame.new(-1,0,0)).Position.Y < w1.Position.Y) and 1 or -1
	
	w1.CFrame *= CFrame.new(modifier * -THICKNESS/2,0,0)
	w2.CFrame *= CFrame.new(modifier * THICKNESS/2,0,0)

	return w1, w2
end

My primary goal is to fix this issue. Switching the plugin on and off you can see the nodes are generated to the edges. If the triangle is in the wrong direction when the nodes are generated, the triangle positions itself lower than what its suppose to be. My guess is the flipping wedges are causing it?

This is my function that positions the nodes to the wedge points.

function service.linkNode()
	local function offset(wedge,size,height)
		return wedge.CFrame * CFrame.new(wedge.size.x/height,wedge.Size.y/size,wedge.Size.z/size)
	end

	for t,triangle in ipairs(CollectionService:GetTagged('Triangle Terrain')) do
		local configuration = triangle:FindFirstChild('Configuration')

		if configuration then
			local wedge1,wedge2 = triangle.Wedge1,triangle.Wedge2

			local node1 = createNodePart(triangle)
			node1.CFrame = offset(wedge1,2,2)

			local node2 = createNodePart(triangle)
			node2.CFrame = offset(wedge1,-2,2)

			local node3 = createNodePart(triangle)
			node3.CFrame = offset(wedge2,-2,-2)
		end
	end
end

This code can be replaced by:

if (abd > acd and abd > bcd) then
	a, b, c = c, a, b
elseif (acd > bcd and acd > abd) then
	a, b, c = b, c, a
end

ab, ac, bc = b - a, c - a, c - b

Which will preserve the rotational direction of the original a, b, and c. You can then replace:

With:

local offset = right * THICKNESS/2
w1.Size = Vector3.new(THICKNESS, height, math.abs(ab:Dot(back)));
w1.CFrame = CFrame.fromMatrix((a + b)/2 + offset, right, up, back)

w2.Size = Vector3.new(THICKNESS, height, math.abs(ac:Dot(back)));
w2.CFrame = CFrame.fromMatrix((a + c)/2 + offset, -right, up, -back)

Optionally making offset negative, depending on the original rotation of a, b, and c coming into the function. If they are clockwise, the offset remains positive. If they are counter-clockwise, either negate offset or reverse the cross product that defines right.

If the rotation of a, b, and c changes between runs, then this function will fail. You should try to keep the rotation always the same. You can use this function to determine if the rotation of your points, given you know the upwards direction:

local function print_rot(a, b, c, up)
	local ab = b - a
	local ac = c - a
	print(ab:Cross(ac):Dot(up) > 0 and 'clockwise' or 'counter-clockwise')
end