Is there a mathematical model to work out which face of a dice will appear when you turn it

488 Views Asked by At

I written a program to draw a 3D cube [a dice]. And I hard coded which face appears when you move it from one face to another. So imagine I start with a 1 facing me; I have a 5 above, a 4 to the right, a 3 to the left and a 2 under.

Now I rotate the dice one place down, so now I have a 5 facing me; I have 4 to the right, a 3 to the left, a 1 under and a 6 above.

I can rotate again so I have a 6 face on, 4 to the right, 3 to the left, 5 below and 2 above. Now if I roll once more than I back at 1.

But wait, if I turn the dice to the right two places so I looking at 6 and go down; I should be looking at 2. But my code gives me a 5 cause that is the face I coded to make the answer correct above.

Is there a math formula or some theory to predict which face should appear.

1

There are 1 best solutions below

1
On BEST ANSWER

There are several options.

The easiest one is to map the dice to axis-aligned cube at $(-1,-1,-1)-(+1,+1,+1)$, and identifying each face based on their unit normals: $$\begin{array}{l|c|cccc} \text{Face} & \text{Normal} & \text{Vertices} & ~ & ~ & ~ \\ \hline 1 & (1, 0, 0) & (1,1,1) & (1,-1,1) & (1,-1,-1) & (1,1,-1) \\ 2 & (0, 1, 0) & (1,1,1) & (-1,1,1) & (-1,1,-1) & (1,1,-1) \\ 3 & (0, 0, 1) & (1,1,1) & (-1,1,1) & (-1,-1,1) & (1,-1,1) \\ 4 & (0, 0, -1) & (1,1,-1) & (1,-1,-1) & (-1,-1,-1) & (-1,1,-1) \\ 5 & (0, -1, 0) & (1,-1,1) & (1,-1,-1) & (-1,-1,-1) & (-1,-1,1) \\ 6 & (-1, 0, 0) & (-1,1,1) & (-1,1,-1) & (-1,-1,-1) & (-1,-1,1) \\ \end{array}$$ The initial orientation/rotation matrix is the identity matrix, $$\mathbf{I} = \left[\begin{matrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{matrix}\right]$$ The six possible ninety degree turns are described by matrices $$\begin{array}{cc} \mathbf{R}_{x+} = \left[\begin{matrix} 1 & 0 & 0 \\ 0 & 0 & -1 \\ 0 & 1 & 0 \end{matrix}\right], & \mathbf{R}_{x-} = \left[\begin{matrix} 1 & 0 & 0 \\ 0 & 0 & 1 \\ 0 & -1 & 0 \end{matrix}\right], \\ \mathbf{R}_{y+} = \left[\begin{matrix} 0 & 0 & -1 \\ 0 & 1 & 0 \\ 1 & 0 & 0 \end{matrix}\right], & \mathbf{R}_{y-} = \left[\begin{matrix} 0 & 0 & 1 \\ 0 & 1 & 0 \\ -1 & 0 & 0 \end{matrix}\right], \\ \mathbf{R}_{z+} = \left[\begin{matrix} 0 & -1 & 0 \\ 1 & 0 & 0 \\ 0 & 0 & 1 \end{matrix}\right], & \mathbf{R}_{z-} = \left[\begin{matrix} 0 & 1 & 0 \\ -1 & 0 & 0 \\ 0 & 0 & 1 \end{matrix}\right] \\ \end{array}$$ To apply one of the rotations, just multiply the rotation matrix and the current orientation matrix (they are multiplied with oldest rightmost and newest rotation leftmost): $$\mathbf{R}_\text{new} = \mathbf{R}_\text{rotation} \mathbf{R}_\text{old}$$ To determine which face is currently showing, decide which direction is towards the user/eye; say $-z$, or $(0, 0, -1)$. Just apply the rotation matrix, $$\left[\begin{matrix} x \\ y \\ z \end{matrix}\right] = \mathbf{R}_\text{new} \left[\begin{matrix} 0 \\ 0 \\ -1 \end{matrix}\right]$$ The result will be one of the $\text{Normal}$s above. You can for example calculate the vector dot product between $(x, y, z)$ and each of the six $\text{Normal}$s above; the one with the largest dot product corresponds to the face most toward the user/eye.

In a computer program, you should use integers instead of floating-point numbers to avoid compounding rounding errors. (Finite precision means rounding errors start pushing the transformation matrix out from orthonormal. Renormalization tends to favour some directions over others, and can be noticeable. With integers, there is no rounding error, although you can only rotate in 90 degree increments here.)

A better option would be to use unit quaternions to describe both the orientation, and the rotations. Unit quaternions can be safely normalized (and it does not introduce any bias), so they don't suffer from compounding rounding errors. Furthermore, you can rotate "just a little bit", i.e. interpolate between unit quaternions.

A third option, and one that you can use to create "magic" dice (that for example have more or fewer than six faces; think of the dice analog of the rooms in the Cube movie series), is to use a graph (data structure) to describe the relationship between the faces. Each face shares an edge with four other faces. In your computer program, each face would link to four other faces, with each link also describing the orientation if turned to that face (rotated $0$, $+90^o$, $180^o$, or $-90^o$). You can combine this with e.g. the quaternion representation, if you want to do a "freely rotating 'magic' dice". Then, you just need to keep track of the face most toward the user (using the dot product with the current face and its four neighboring faces), and advance the graph whenever that changes, based on which face became the one most toward the user/eye.