Godot Version
4.2
Question
Hi there, I am struggling with a way to get a result value from a thrown 1D4 dice (or any other with different faces).
I already got the result by checking the Y position value on each face, but I wanted to get a more controlled way to do it, I mean, by positioning the dice with the result I want to roll, for that I created a mesh for every face with their own local position pointing towards its normal, and my idea was to do this:
extends Node3D
@onready var dice=get_node("d4n")
@onready var face=dice.get_node("Normals/2") #i.e. the result I want..
func _ready():
#replace the dice axis by the face axis
dice.transform.basis=face.transform.basis #??
#rotate the local dice UP axis to point global UP
var direction=Vector3.UP-dice.rotation
dice.rotation=direction
I got more or less what I wanted, but the face is pointing the opposite way, not sure why…
Another doubt I have is if there’s a function to work with plane mesh normals in Godot instead of having to create them manually for each face.
Thanks in advance
Best regards
For my VR dice, I added a Position3D (I did this in Godot 3, but it will also work with Marker3D in Godot 4) on each face, which gives me the normal for each face. I then created an array with all the position nodes.
Thanks for the answer, I assume then that there’s no function to work with normals in Godot.
I created Mesh3D representing each normal, since I don’t know how to place markers3d or node3D with normal directions, so I made them in Blender.
The point of my question is:
How can I rotate this dice so the result instead 1 is 2?
Thanks in advance
You use the Vector3 class to deal with most normal math, since normals are just vectors.
To get the normal for each face, you just normalize the position of each marker:
var normal1 = $MarkerFace1.position.normalize()
var normal2 = $MarkerFace2.position.normalize()
The normal that is facing down indicates the corresponding value of the die. What that value is will depend on the shape of the die.
I didn’t have a need to force the dice onto specific numbers, but it would involve choosing the normal for the desired number, and rotating the die to align the selected normal with the Vector3.DOWN vector (calculate the cross product between the selected normal and the DOWN vector, calculate the angle between the two, and rotate the die around the cross product).
the idea is to get control of the result so even you have a random roll, I can take this roll and show the result bigger or centered or even being able to manipulate the result…
In any case and I come back to my original question, this is exactly what I am struggling with… 
“rotating the die to align the selected normal with the Vector3.DOWN vector (calculate the cross product between the selected normal and the DOWN vector, calculate the angle between the two, and rotate the die around the cross product).”
I am very bad with 3D rotations, this is why I was looking for help. 
Thanks anyway
Most people hate to hear this, but I highly recommend taking a few weeks to learn the basics of Linear Algebra. It’s pretty straightforward, and will take much of the mystery out of 3D game math. The prerequisites for understanding Linear Algebra are general arithmetic and a basic understanding of what the trigonometry functions do. You don’t need to know much trig, but it will help in understanding matrix rotations.
When someone made this same recommendation to me years ago, I hated to hear it but knew he was right. You don’t have to master it, though that would be helpful.
Not understanding basic vector and matrix math will severely hamper your game development endeavors, as game development (2D and 3D) is heavy in both. Godot has classes and functions that do the math for you, so you don’t have to implement them yourself, but you REALLY need to know how and when to use them.
2 Likes
I really appreciate your advice, I wish I had time and age to learn all math theory I need, but this is just a hobby for me, just a way to make my brain move by the practice of programming which I like, learning linear algebra is spending probably a lot of time trying to learn also a lot of stuff that I won’t probably use, and believe me, as you get older is quite difficult to retain what you learn ;-), specially when you don’t practice the matter quite often, but as I said I appreciate your advice to encourage people to get more knowledge.
You can manually rotate your dice via the editor into each position you want to later force, record those rotations in an array, then set the dice into those rotations via code. It won’t be as pretty as can be accomplished mathmatically, but it will work.
1 Like
Yes, that was my last resource, the problem is that I want to do the same with D4, D6, D8, D10, D12 and D20 so imagine… 
No problem, I’ll figure it out in some other way, but thanks anyway.
I think if you already know which ‘side’ of the die is the official result, as it seems you have, as you asked ‘How can I rotate this dice so the result instead 1 is 2?’, why not instead of rotating the result, you just change the way you display the sides of the die itself, and just put the result you want on that side (and the rest if visible).
If you don’t need actual mesh geometry for the numbers or pips, you could just do this with some material magic by using an atlas texture, or sprites on top of the blank sided die mesh that you alter the sprite texture reference for the result you want.
If you need mesh geometry, you could model the die into a ‘main’ shape with holes on the sides, and then you model each side on it’s own. Then on the die scene you include mesh instances for each side of the die where you just plug in the source side mesh for the results you want when you know which side of the die is right.
This will always result in some weird pop when you ‘swap’ the sides when it stops rolling, unless the throwing and physics is quick enough to hide it, if that is a problem you can look into recording some sims of the dies being thrown, then you will know which side each sim comes up as the right side, then you can pre-assign the side values and it’ll always come up what you want, even during the animation.
Sometimes I just want the dice to work without all the fiddling around. I’ve messed around with stuff like this before, and honestly, when I just need quick dice rolls without overthinking, I use dice.onl—saves me a ton of hassle. For your issue, it sounds like the normals might be flipped or off somehow. I had something similar happen once, and fixing the normals in the mesh made all the difference. Also, lining up the face with Vector3.DOWN using the cross product and rotation angle worked for me to get it rolling the way I wanted.