How can I do this more efficiently?

I made a dice that is rolled by setting the AssemblyAngularVelocity, then after some time, I set it to 0, 0, 0, anchored the dice, and then calculated which side the dice rolled on. It’s calculated through its orientation plus the perfect xyz values that can give you that dice number, but there are some flaws with this.

  • The orientation can sometimes be values such as -40, 120, -56 which cannot be calculated and just causes the script to stop moving on due to me looking at the perfect orientations that the dice need to be at.
  • In certain cases when the dice is on three it will be calculated as being on two due to the orientation not being perfectly 0, 0, -90.

So how can I find the correct dice number by calculating its orientation?

Script

	local diceRotationTable = { -- A table that contains the values for the possible x, y, and/or z rotations of the dice depending on where the dice lands
		[1] = Vector3.new(0, 0, 0),
		[2] = Vector3.new(90, 0, 0),
		[3] = Vector3.new(0, 0, -90),
		[4] = Vector3.new(0, 0, 90),
		[5] = Vector3.new(-90, 0, 0),
		[6] = Vector3.new(0, 180, 180)
	}
	
	local diceRolledNumber : number -- The number that the dice rolled on.
	local function getDiceNumber() -- Calculates which number the dice rolled on by rotation values in the rotationTable and the bombOrientation.
		local bombOrientation = bombClone.Orientation
		for index, value in pairs(diceRotationTable) do
			if bombOrientation == value + Vector3.new(0, bombOrientation.Y, 0) then diceRolledNumber = 1 break end
			if bombOrientation == value + Vector3.new(0, bombOrientation.Y, bombOrientation.Z) then diceRolledNumber = 2 break end
			if bombOrientation == value + Vector3.new(bombOrientation.X, bombOrientation.Y, 0) then diceRolledNumber = 3 break end
			if bombOrientation == value + Vector3.new(bombOrientation.X, bombOrientation.Y, 0) then diceRolledNumber = 4 break end
			if bombOrientation == value + Vector3.new(0, bombOrientation.Y, bombOrientation.Z) then diceRolledNumber = 5 break end
			if bombOrientation == value + Vector3.new(bombOrientation.X, 0, 0) then diceRolledNumber = 6 end
		end
	end

Hi there, It seems like you’re trying to determine the face of a dice based on its orientation. The issue you’re facing is due to the fact that the orientation of the dice can vary beyond the perfect orientations defined in your diceRotationTable.

One way to solve this is by comparing the orientation of the dice with each of the perfect orientations and choosing the one that is closest. This can be done by calculating the magnitude of the difference between the dice’s orientation and each perfect orientation, and choosing the smallest one.

I ended up fixing this by using normals and finding the normal with the highest y-value, which was a MUCH easier approach.

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