Detecting Value of a Dice Face

I’m trying to make a game that requires the use of dice, and not just a simple random 1-6 generator. The dice is the actual playable character, and when you move, your dice rotates accordingly in that direction on a grid. I need to know how to detect which face is currently the one facing up at any point, and be able to get a value from it.

I have been toying around with the 3DBox object, but I cannot find any way to detect faces and its value. I’m also up for simulating it in 2D, if it’s somehow easier, though it seems to be VERY tricky at first glance, due to having to keep track of 3 axis rotations.

I love the concept. I’ve never played with dice in GD. Here are my thoughts. There might be an easier way. I’ve never made dice before in GD.

My first thought was to use the angle or rotation. If you use Round(rotation/90) then it would give you a number from 0 to 3. You could use those numbers to look it up on a table using an array or structure. Maybe a 2 or 3 dimension array or an array of strings.

IDK if you would need the rotations plus angle or just the rotations. You could use an array of strings to find the face. Pick the index and then the character in the position of a string. It would be easier to setup. Or maybe you could use the array tools extension to split the text with commas into an array.

X is Round(x rotation/90)
Y is Round(y rotation/90) 
Side = StrAt(faceTable[x], y)

The array would be like: 
Table[0] "1364"
Table[1] "2354"

If you need x, y and z then
Table[x] [y] [z] 

The setup might be slow but the events would be simple.

I tried a chatbot. It suggested a collision or raycast from above to detect the top side using separate objects.

I kind of like the idea of positioning a hit box on each side but instead of checking for the top object you could check for the collision between it and the floor. An object variable would contain the value of the opposite side.

I prefer the array but the collision might be easier. I haven’t used 3D much.

Just thought I’d update my progress! After manually rolling a dice around and recording the faces, I found a pattern, and have come up with a surprisingly simple system!

Dice

This can be represented in two different arrays:

XAxis = [ 4, **1**, 3, *6* ]
YAxis = [ 2, **1**, 5, *6* ]

If you roll the dice along the one axis, just move the values in the array.

Moving to the right (X axis) will be turn the values from:

XAxis = [ 4, **1**, 3, *6* ]

to:

XAxis = [ 6, **4**, 1, *3* ]

Then, simply update the 2nd and 4th values on the opposite axis to match the changes!

XAxis = [ 6, **4**, 1, *3* ]
YAxis = [ 2, **1**, 5, *6* ]

Update:

XAxis = [ 6, **4**, 1, *3* ]
YAxis = [ 2, **4**, 5, *3* ]

While this is perfect for the game I want to make, it does not take into account the rotation angles of the faces. My next challenge will be using GDevelop to implement this logic, but that should be easy enough.

While I’d consider the issue resolved, I’m still curious on how to set up the 3D method. Detecting the collision of the face on the ground sounds like a simple enough idea, but the ‘how’ is the tricky part. I recall finding a plugin to stick objects together, although I don’t know if that applies to 3D objects.

1 Like

I don’t know if the sticker works in 3D but it should be as simple as placing the objects in relation to the center point. It wouldn’t need to happen in realtime. Only when the die was checked.

I guess, you could also use a ray cast to detect the floor. Aim it in the 6 directions and get the side based on which ray cast triggered. A simple repeat would detect 4 of the sides and then check in the other directions. There’s a 3D raycast extension.