fox investigating an open PC

snotgrl.xyz

windows XP windowframe

dice roller devlog

so i got bored today and started to program a dice rolling program. like you press a button and some physics objects that look like dice spew out, and then it adds up the score on the dice and displays the result. its a cute idea right?

spawning objects and doing math, and even interpreting what ‘2d6’ or ‘1d20’ means is pretty straightforward for most people who’ve done programming before. for me the biggest challenge actually comes in when you try to read the dice results (hereafter called the ‘score’).

like let’s think about it. you have a cube and it rolls for a little, and then stops, and we want to realize that it’s displaying a 3 on the top.

we could point a vector to every face, and map each vector to a score, and see which vector is closest to pointing straight upwards (Fig. A), but that quickly becomes difficult the more faces we add to a die.

we could make a point on every face and assign it a score, and see which of the points is highest up to determine which face is facing upwards (Fig. B) .. but again, that’s a lot of work for dice with high face counts, and we could run into precision problems. i mean how do you make sure that each point is really in the middle of its assigned face?

‘course you can solve these problems but i only wish to draw attention to the sort of difficulties and roadbumps one might face. personally, i found a unique method which makes use of my 3d modelling technical know-how, hence the blog post >u>

something i know about 3d models is that they can have vertex colors. each vertex of the model can be painted it’s own color, which is basically just a 3D vector with an R, G, and B value, each of which are a value from 0 to 1 encoded in a byte. so like, the red channel for example can have 255 different values spanning from 0.0 to 1.0. yes this WILL come up on the test.

here is the game plan: you assign a color representing a score to each vertex. the vertices make up faces, and so those faces will be colored in by their constituent vertices. when the time comes to read the score, you perform a raycast from inside the die, shooting straight upwards. the first face it hits is the most upwards facing face. we read that face’s color, and convert it back into the score. bingo!

[ actually assigning the vertices their color is done automatically using geometry nodes in blender, taking data from a custom attribute which we assign to each face. the only thing we have to do manually is select a face, click ‘set attribute’, and enter in the desired score. it’s very efficient~! ]

‘but zoe’, you might say, ‘every vertex is actually part of more than one face! how can you assign a score (and therefore a color) to a given vertex if it represents two faces, or more?’ well when it comes to 3d models, when you define a sharp edge, the faces on either side of that edge get their own separate vertices. this is what i mean by ‘3d modelling technical know-how’. this shit is obscure XD

so instead of doing some crazy shit and mapping like RED to 3 or BLUE to 5 we’re just gonna say that the color of a vertex = the face score * 0.05 . that gives us enough room for 20 face scores, which is enough for a d20 (duh) and also a percentile die so we’re golden. using 0.05 as our multiple instead of 0.01 also gives us extra wiggle room against byte precision problems. let’s diagram it out:

lights, camera, action!!

im WEAeally proud of this solution because of how it uses my skillset in particular.. noone else is doing it this way thats for sure. and im also really excited to find out how other people went about this when im done with this project >u< you gotta imagine there’s some really intuitive way to bypass all this right? we’ll see sooner or later xD!! thanks for dropping by !


4 responses


no login required 🙂 comments are anonymous, so leave your name if you want to be recognized~!

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. this is such a clever way to handle reading the values off of physical dice! i looove when devs use the RGB vertices to do non-color related things, it’s such a smart way to make use of these stored values :)!!!

    already mentioned by my beloved fellow commenter pee but this is the most fun i’ve ever had reading a design document — your figures are simultaneously illustrative and just the right amount of silly, and as someone who has written SOOO MANY BORING DRY ASS design docs i am truly inspired.

    1. <3 THANK you im glad you like my draws.

  2. i wanna read this over more thoroughly in the morning, but for now i wanna letchu kno i love all the lil accompanying doodlez

    1. theyre fun to make :p

Pinkie Pie sitting down

This web server runs on a Pinkie Pi.