GameMaker Rotating dice in 2D

R

Ragis

Guest
So imagine a square moving on a grid, one tile at once, up, down, left or right. Easy enough.

Except it's not a square, but a cube - or rather a die - each of its sides is different.
When the die moves, it doesn't slide, but simply rolls to another of its sides.

It sounds easy, but the die rotates in three axes, and I cannot figure out how to code that.
For now, I just need it working for 6 side sprites, I'll take care of animations later.
If someone could point me in the right direction I would be grateful
 
Last edited by a moderator:
Assuming you are using GMS 1.4

(1: Make a 3d model of dice with the texture of a dice on it!
(2: Rotate the model using d3d functions,
(3: Boom It looks real but you have to make some calculations to get the data which face is on top, by checking the values of the dices default angle angle angle after which stop!

3d works on the 2d game look 2d,
I have done that before for one of my clients I know that works :)
 

TheouAegis

Member

16
32 04 01 08
02

Convert a d6 such that each face corresponds to a single bit as seen above. This conveniently means that for each face, the product of its value and that of its complement will equal 32: 1*32 = 32, 2*16 = 32, 4*8 = 32. So we should store these values in a global array for quick reference.

///Game Start Event
for (var i=0; i<6; i++) {
global.bits[ i ] = 1 << i;
global.complement[ i ] = 32 >> i;
}

When we roll the die, we need to pick a number to start on. It doesn't matter what it is, so we'll just pick a random value from 0 to 5. This will also be used to tell us what value the die will actually be when it stops rolling. We need to decide how long the die will roll, so we can pick a random range for that. We also need a measurement of how long it takes to roll from one side to the next. We also need to know if the die is on its edge or on a face.

///Die Create Event
face = irandom(5);
value = global.bits[face];
rolls = irandom_range(4,16);
spin = 4;
alarm[0] = spin;
edge = 0;

///Die Alarm 0 Event
if !edge {
edge = pow(2,irandom(5));
while edge == global.complement[value] edge=pow(2,irandom(5));
value |= global.bits[edge];
}
else {
value ^= global.bits[face];
face = log2(value);
rolls--;
spin += 16 - rolls;
edge = 0;
image_angle = irandom(7) * 45;
}
alarm[0] = spin;
image_index = value-1;

You draw the sprite based on value-1. So if the value is 1, face 1 is shown. If the value is 2, face 2 is shown. If the value is 4, face 4 is shown. BUUUUUTTTT if the value is 3, then the edge between 1 and 2 would be shown. The image_angle is to make it look like it's bouncing.

Edit: If you want more frames of animation, image_index would be (value-1) multiplied by the number of filler frames. If youbdid that, yhen the "spin" variable I used here would need to be a constant multiple of how many filler frames there are.
 
Last edited:

Fabseven

Member
If you want a 2D DICE - not rotating its easier.
Did it many times, you have to build a state machine ( states could be _start , _rolling , _wait , _end )
In step event when stat = _start you will initialize values ( set if the dice is a 6 faces dices or more for examples, set the sprites, set the end timer and step_timer ) and set stat to _rolling , set alarm[0] = end_timer
In step event when stat = _rolling you will change the value and so the sprite of your object each step_timer ( if( step_timer mod end_time == 0 ) then ....<use irandom_range to change dice value and sprite> ))
In step event when stat = _rolling and alarm[0] <= 0 then chance set stat to _wait , set alarm[0] with the value you want
In step event when stat = _wait and alarm[0] <= 0 then set stat to _end
....
Same stat machine in draw or draw gui
 
N

NeZvers

Guest
If I'm understanding question right, you can simulate rotation with draw_sprite_pos()
And simplest way that I could think at the moment is to have some kind ds or variables that store for information for each side's surrounding numbers, so in case of 1 you know that going up will give you 2. Or something like that.
 
R

Ragis

Guest
Thanks for all the feedback guys, but I need something a bit different. I don't need the die to randomly rotate and generate random numbers. I need a die (or simply a cube) that moves like this (just in top-down view): https://cdn.dribbble.com/users/612275/screenshots/2997676/cube-rolling.gif
The problem is that when you rotate a die like this, the sides rotate, so I cannot simply use a switch or an if statement that says "when it shows '1' and you go up, change to '3', and when you go down, change to '4'" cause if the cube is rotated 90 degrees, then when you go up from '1' it will be '5' and down will be '2' for example. So I need to take the die rotation (in all 3 axes) into account somehow.
 
D

Death

Guest
Nah, just keep it simple. A die has six sides, BUT all opposite sides ALWAYS add up to seven. So you know when a 3 is showing (up) a four is on the bottom. A 2, a five, etc etc. The rest is is just deciding which face you want to make your "top" face, the one on the front, or the real top. Now all you have to consider are the 3 other face possibilities you need to show, a simple switch case thingy.
 

GMWolf

aka fel666
Thanks for all the feedback guys, but I need something a bit different. I don't need the die to randomly rotate and generate random numbers. I need a die (or simply a cube) that moves like this (just in top-down view): https://cdn.dribbble.com/users/612275/screenshots/2997676/cube-rolling.gif
The problem is that when you rotate a die like this, the sides rotate, so I cannot simply use a switch or an if statement that says "when it shows '1' and you go up, change to '3', and when you go down, change to '4'" cause if the cube is rotated 90 degrees, then when you go up from '1' it will be '5' and down will be '2' for example. So I need to take the die rotation (in all 3 axes) into account somehow.
just keep it simple:

Keep a rotation matrix of the dice. Then you can use the left, right or up vector to determine what faces are facing in what direction.
To rotate your dice, you just have to apply a rotation matrix to your current dice matrix. The math takes care of itself!
If you are not familiar with transform matrices, go learn them now! They will make your game dev career sooo much easier!
 
Top