Getting the degree amount to make 2 faces perfectly align each other

Hey there! Basically I’ve been trying to get a number (basically degrees for rotation) to make 2 faces perfectly align / face each other.

Basically I have this table where I store the possible degree options

["Degrees"] = {
				["Front"] = 0,
				["Left"] = 270,
				["Back"] = 180,
				["Right"] = 90,
			},

The 2 faces / objects I want to align both have values inside of them looking like this:

StartConnection = "Front",
		EndConnection = "Back",

They correspond to the degree table.

I had issues with actually getting the perfect number to make the EndConnection of the previous tile and the StartConnection of the new tile / object align.

Here is an example of some connections if that helps:
Previous tile:
StartConnection = “Front”,
EndConnection = “Right”,

To place tile (new):
StartConnection = “Front”,
EndConnection = “Back”,

How it looks like:
Bildschirmfoto 2024-10-07 um 21.33.57
How it’s supposed to look like:

Does maybe anyone know how I could achieve the thing I’d like to do?

Yea help would be greatly appreciated!!

Get the start and end connections for the old tile and another two for the new tile, and do this formula: (endDegree - startDegree) % 360, (get the degrees from the table of degrees you already have) and you should get the rotation angle:

local degrees = {
	["Front"] = 0,
	["Left"] = 270,
	["Back"] = 180,
	["Right"] = 90,
}

function RotateAngle(startConnection, endConnection)
	local startDegree = degrees[startConnection]
	local endDegree = degrees[endConnection]
	local rotateAngle = (endDegree - startDegree) % 360
	return rotateAngle
end

-- Example use:
local oldTile = {
	StartConnection = "Front", 
	EndConnection = "Right" 
}
local newTile = {
	StartConnection = "Front", 
	EndConnection = "Back"
}

local rotateAngle = RotateAngle(oldTile.EndConnection, newTile.StartConnection)
print("Rotation Angle: " .. rotateAngle)

Weeell that didn’t quite work out.
As seen the corner is still off and the straights are facing the wrong way (are 180 degrees turned too much).

That’s my code:

local function RotateAngle(startConnection, endConnection)
			local startDegree = directions.Degrees[startConnection]
			local endDegree = directions.Degrees[endConnection]
			local rotateAngle = (endDegree - startDegree) % 360
			return rotateAngle
		end
		
		local newRotation = RotateAngle(previousBackEnd, selectedFrontEnd)

Well for the negative degrees, add 360 to the result. (check if its less than 0 to know if its negative)

That didn’t change anything…

local function RotateAngle(startConnection, endConnection)
			local startDegree = directions.Degrees[startConnection]
			local endDegree = directions.Degrees[endConnection]
			local rotateAngle = (endDegree - startDegree) % 360
			
			local negativeAdd = 0
			if rotateAngle < 0 then
				negativeAdd = 360
			end
			return rotateAngle + negativeAdd
		end

Was my code correct or did I do a mistake?

I meant add 360 not to set it to 360.

Huh? I am adding 360 (negativeAdd) to the “rotateAngle” var which is the result. NegativeAdd is 0 if the result isn’t negative so uhhh what?

My bad i didn’t notice that you added that on the return

Yea so… Any ideas why it couldn’t work? Maybe it would be a good idea to step away from that degrees table and just find a way to make the back end of old tile and front end of new tile align?

You mean like this:

local connections = {
    ["Front"] = "Back",
    ["Left"] = "Right",
    ["Back"] = "Front",
    ["Right"] = "Left",
}

Not sure… I was considering maybe finding a whole new method (what would that table you just suggested even do, it’s just strings…).

I fixed it! I just hardcoded every possibility :skull:. Thanks for the help though!!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.