Well, I suppose “roll” would be a more popular dice game term, but spin is actually more accurate in this case! I’m actually rotating the dice in a spin maneuver, and having them fall on the table, rather than rolling them across it from an angle or the side.
The first question I had to tackle was what type of object to use for the game. The three types are Static Body, Kinematic Body, and Rigid Body. Each have their own uses, and here I put into words my limited understanding. Be sure to comment if you can clarify any of these.
Static Body is an object that is not supposed to react to any force. It should instead stay right where it is when it is struck by another object. In this game, I used a static body for the table top. This way the force of being struck by objects, such as the dice, and physics powers, like gravity, have no affect on it. In fact, I didn’t even give the table legs, because nothing is needed to “hold” the table up, since it is a static body.
Kinematic Body is an object that is often used for making the main player of a game, or something like that. It is not affected by physics unless you program it too. Gravity and such have no affect on it until you tell it to through code. So, if I had a kinematic ball roll across the table, when it gets to the edge, it would keep rolling on air, as if the table were still under it. This is what we used in BornCG’s great video game tutorial for the main player. It is very easy to control movement of this object.
Rigid Body is a type of object that by nature is fully affected by physics. Gravity and inertia all come into play on this type of object. If the table were not there, a rigid body object would fall indefinitely. These objects are difficult to move in one sense, because you can’t just say, “go that way” like you do with kinematic bodies, instead you have to exert force against them in a direction to cause them to move that way.
To make the dice game as realistic as possible, I used rigid bodies for the dice. When they fall to the table, they can bounce, tumble, or bump and shove other dice. The big question was how to move them. Of course, once in the air, they fall to the table and tumble, etc. That was the easy movement, because it is not programmed by me at all, it is simply the engine physics taking place. The tough movement was what to do with them after they hit the table.
Originally, I was planning to move them aside, and set them out of the way so I could roll the remaining dice. Not having any 3D experience, I thought this wouldn’t be too hard, but it became a nightmare for rigid bodies. Here are several methods I tried after reading hours of documentation and QA forums:
var oldPosition = get_node("LowPolyDiceC").get("dicePosition") var targetPostion = get_node("pirateShip").get("shipPosition") var newDirection = targetPostion-oldPosition print(newDirection) $LowPolyDiceC.apply_impulse(Vector3(),newDirection) #translate(translation.direction_to(newDirection)) # This one only moves a tiny bit, and not in the right direction. #translation.direction_to(newDirection) # This one doesn't move at all. #apply_impulse(Vector3(),newDirection) # This one rockets it towards the ship, then it falls off the table. #translate(newDirection) # This one does not go to the ship, but goes up in the sky near it.
I found several ways to translate, or “teleport” the dice somewhere else, but not where I wanted them. And, once transported, they didn’t work with physics anymore. I also found ways to whack them with a force, but they arced up through the air and fell off the table (think golf club hitting golf ball). None of those would do. I tried asking my issue as a question on Godot’s QA forums, but I didn’t get a response from anyone, so I had to give this idea up.
Instead, I used the impulse (force) to send the dice straight up into the air again. Then, as they go up into the air, I perform several “spins” of the dice to randomize the result. Here’s my code:
func _make_dice_random (diceName): var x = randi() % 365 + 1 var y = randi() % 365 + 1 var z = randi() % 365 + 1 get_node(diceName).rotate_x(deg2rad(x)) get_node(diceName).rotate_y(deg2rad(y)) get_node(diceName).rotate_z(deg2rad(z)) func _spin_dice(): if tumbler > 0 : tumbler = tumbler - 1 #print (tumbler) var upAmmount = 4 if takenA != 1: $LowPolyDiceA.apply_impulse(Vector3(),Vector3(0,upAmmount,0)) _make_dice_random("LowPolyDiceA") if takenB != 1: $LowPolyDiceB.apply_impulse(Vector3(),Vector3(0,upAmmount,0)) _make_dice_random("LowPolyDiceB") if takenC != 1: $LowPolyDiceC.apply_impulse(Vector3(),Vector3(0,upAmmount,0)) _make_dice_random("LowPolyDiceC") if takenD != 1: $LowPolyDiceD.apply_impulse(Vector3(),Vector3(0,upAmmount,0)) _make_dice_random("LowPolyDiceD") if takenE != 1: $LowPolyDiceE.apply_impulse(Vector3(),Vector3(0,upAmmount,0)) _make_dice_random("LowPolyDiceE") $spinTimer.start() else : $spinTimer.stop() $Timer.start() func _roll_dice(): $ControlHUD/HUDrollButton.hide() remainingRolls = remainingRolls - 1 get_node("ControlHUD/HUDrollsNumLabel").set("howManyRolls", remainingRolls) tumbler = 3 var upAmmount = 6 if takenA != 1: $LowPolyDiceA.apply_impulse(Vector3(),Vector3(0,upAmmount,0)) if takenB != 1: $LowPolyDiceB.apply_impulse(Vector3(),Vector3(0,upAmmount,0)) if takenC != 1: $LowPolyDiceC.apply_impulse(Vector3(),Vector3(0,upAmmount,0)) if takenD != 1: $LowPolyDiceD.apply_impulse(Vector3(),Vector3(0,upAmmount,0)) if takenE != 1: $LowPolyDiceE.apply_impulse(Vector3(),Vector3(0,upAmmount,0)) $spinTimer.start()
It reads from the bottom up. First I call the _roll_dice() function to send any dice up that are not “taken” or in use by the player for their ship, captain, or crew. Then a timer is started, to allow the dice time to rise up off the table. Once the timer ends, it calls the _spin_dice() function, which has several calls to the _make_dice_random(dicename) function, picking random numbers of 365 degrees*. This is done as many times as the “tumbler” variable is set to, or in this case, 3 times. Since the dice are rotated on the x, y, and z axis, they get very randomly arranged, and then fall to the table, tumbling around to their final position.
Each dice has 6 small invisible cube areas on the faces of the dice that correspond to the opposing sides number. So, when they hit the table, that reports the number the dice has face up to the variables, so the dice number is set and known. That looks like this:
func _on_AreaA2_body_entered(body): if body.name == "TableObject" : $diceSoundPlayer.play() diceA = 2 #print (diceA)
And finally, once the dice have come to rest, they are checked for their number:
func _check_all_dice(): $Timer.stop() #print ("dice check") #print (diceA, diceB, diceC, diceD, diceE) # Check for Ship if hasShip == 0 : if diceA == 4 : hasShip = 1 $pirateShip.show() takenA = 1 elif diceB == 4 : hasShip = 1 $pirateShip.show() takenB = 1 elif diceC == 4 : hasShip = 1 $pirateShip.show() takenC = 1 elif diceD == 4 : hasShip = 1 $pirateShip.show() takenD = 1 elif diceE == 4 : hasShip = 1 $pirateShip.show() takenE = 1 # Now check for captain if hasShip == 1 : if diceA == 5 : hasCapt = 1 $pirateHat.show() takenA = 1 elif diceB == 5 : hasCapt = 1 $pirateHat.show() takenB = 1 elif diceC == 5 : hasCapt = 1 $pirateHat.show() takenC = 1 elif diceD == 5 : hasCapt = 1 $pirateHat.show() takenD = 1 elif diceE == 5 : hasCapt = 1 $pirateHat.show() takenE = 1 # Now check for crew if hasCapt == 1 : if diceA == 6 : hasCrew = 1 $piratecrewHat.show() takenA = 1 elif diceB == 6 : hasCrew = 1 $piratecrewHat.show() takenB = 1 elif diceC == 6 : hasCrew = 1 $piratecrewHat.show() takenC = 1 elif diceD == 6 : hasCrew = 1 $piratecrewHat.show() takenD = 1 elif diceE == 6 : hasCrew = 1 $piratecrewHat.show() takenE = 1 # Now check cargo if hasCrew == 1: if remainingRolls > 0: $ControlHUD/HUDkeepButton.show() else: $ControlHUD/HUDkeepButton.hide() _update_score(diceA + diceB + diceC + diceD + diceE - 15) else : _update_score(0) if remainingRolls > 0: $ControlHUD/HUDrollButton.show() if theCurrentPlayer == 0: $computerTimer.start() else : $ControlHUD/HUDrollButton.hide() $updateTimer.start()
This string of if/elif statements is really ugly, but gdScript doesn’t have a “switch” or “case” block method. It does have a “match” method, but I couldn’t understand how to implement it properly.
Hopefully that was interesting and or helpful to other newbies like myself. You can always check out the full source code for the game at my GitLab.
Linux – keep it simple.
* Yes, I know there are really only 360 degrees in a circle, but this actually improved the random factor, based on my research, because I was under the impression that the random number generator seemed to favor slightly lower numbers, so I added a few. If it goes over 360, it just keeps rotating, as if from 0.