Log inGet started
World

Rolling Bones: Physics Dice, Cups, Towers and Trays Kit

README.md

diceKit

Version 1.0.10 (DK.VERSION).

Physics dice for multiplayer Zero worlds. Real polyhedral d4, d6, d8, d10, d12, d20 and percentile d100, rolled by the engine's own rigid bodies, recorded and played back so every player sees the same dice tumble the same way and land on the same faces. One tray, every variety one setting away: nine looks, four ways to roll, four trays and four result displays.

local DK = require("diceKit.api")
DK.Tray { name = "Tray", position = { 0, 0.8, 0 }, style = "felt", dice = { "d6", "d6" }, look = "wooden", roll = "throw", display = "float" }

Install

Published on ZeroMind as diceKit.package (world "Rolling Bones: Physics Dice, Cups, Towers and Trays Kit"). Install it by guid:

tools.use("zeromind", "installAsset", "<diceKit package guid>")   -- from Luau

or zero zeromind installAsset <guid> in the engine shell. It lands at /source/diceKit.package and has no dependencies.

Quick start

  1. In your scene's build.luau, inside content(), place a tray where the dice should land (the position is the tray floor's top surface; put it on your table top):
local DK = require("diceKit.api")

function content()
    DK.Tray {
        name = "Tray", position = { 0, 0.8, 0 },
        style = "felt", dice = "2d6", look = "classic", roll = "throw", display = "float,banner",
    }
    DK.Gallery { tray = "Tray" }   -- optional: a button that flips through every option live
end
  1. Press Play. Press R, click or tap the tray, or press A on a pad to roll. Drag or swipe across the tray to aim the throw and set how hard. The dice land, the values float over them and the total shows in a banner.

  2. Hear the result in your game, on every peer:

DK.on("rolled", function(e)
    print(e.tray, "roll", e.seq, "by", e.by, "values", table.concat(e.values, ", "), "total", e.total)
end)
  1. Roll from code, for a turn-based game, and decide who may roll:
local tray = DK.tray("Tray")
tray:roll { by = currentSeat }                 -- any peer may call it; the roller simulates
DK.rules("Tray", {
    canRoll = function(t, by, peer) if by ~= currentSeat then return false, "not your turn" end return true end,
    onRolled = function(t, result, by) end,   -- runs on the roller only
})

Settings (DK.Tray fields)

SettingDefaultWhat it does
nameDiceTrayEntity name; what DK.tray(name) and DK.rules(name, ...) find it by.
position, yaworigin, 0Where the tray floor sits; turn in degrees. Players throw from the +z edge (before turning).
dice"d6,d6"A list { "d6", "d20" } or a phrase "2d6 + d20", "d100". Up to 20 dice.
lookclassicSee Looks. Forgiving: "See-through Casino", "wood", "glow" all work.
color""#rrggbb body colour for looks that take one.
stylefeltThe tray: felt, wooden, arena, none.
rollthrowthrow, cup, tower, flick.
displayfloat,bannerAny of float, banner, tally, or none, comma separated.
size0.09Die size in metres (a d6's width).
width, depth0Tray inner size in metres (0: the style's own, about 1.0 by 0.7).
seed00: random. Any other number: roll n uses seed * 1000 + n, a repeatable sequence.
inputtrueBuilt-in input (key, pad, click, tap, drag, swipe). Off for a game that only rolls from code.
keyKeyRThe key that rolls.
anywherefalseA click or tap anywhere rolls this tray, not only on the tray.
showUitrueThe Roll button and the hint, on every screen (see setLocalUi for one screen).
uiPlacebottomRightWhere the Roll button and hint sit on desktop: bottomRight, bottomLeft, bottom, right, topRight (phones use the bottom edge).
sound, volumetrue, 0.8Clatter, thud, clink, cup shake and tower rattle, timed to the recorded hits.
title""Shown on the banner and tally card.
rulesthe nameWhich DK.rules entry the tray runs.

Change any of them live: DK.tray("Tray"):set("look", "neon").

The controller: DK.tray(name)

CallAnswers
t:roll{ by?, dice?, seed?, aim?, strength?, roll?, look? }true, seq on the roller, true, "sent" elsewhere, false, reason (busy, refused by canRoll). aim = { x, z } is a direction across the tray (z = -1 is away from the thrower), strength 0 to 1. dice, roll, look override the tray's for this roll.
t:set(key, value), t:get(key)Change or read a setting (a non-roller's set goes to the roller).
t.busy()A roll is in the air.
t.last()The last result as kept for late joiners: { seq, values, total, by, kinds, dice, final }.
t.history()The rolls this peer saw: { seq, values, total, byText }.
t.kinds(), t.recording(), t.isRoller(), t.entity()The dice, the current recording, whether this peer simulates, the entity.
t:setLocalUi(on)Show (true) or hide (false) the Roll button and hint on THIS screen only; nil follows the synced showUi. For a room where only seated players should see it.
t:setLocalInput(on)Let this machine's R key, pad A and clicks or taps on the tray roll it (true) or not (false); nil follows the synced input. canRoll still decides on the roller.

Events (DK.on)

EventData
started{ tray, seq, by, kinds, method, peer } as the dice leave the hand, on every peer.
rolled{ tray, seq, values, total, by, peer, kinds, dice, digest } as the last die lands, on every peer. values has one entry per requested die (a d100 is one value, 1 to 100); dice lists every physical die { kind, face, score, flat }; digest fingerprints the recording (equal on every peer).
refused{ tray, by, peer, reason } when canRoll said no (on the roller and on the asking peer).
*Every event, with event naming it.

Catalog

DK.CATALOG lists every entry { kind, setting, id, name, tags, description }. DK.variants{ kind = "look", tag = "transparent" }, DK.variants{ search = "glow" } filter it; DK.normalize("look", "Cut Gemstone") answers gemstone.

Dice kinds (dice)

idShapeNumbering
d4tetrahedroneach face shows three numbers; the number at the top point is the roll
d6cubeopposite faces make 7; pips on looks with pips
d8octahedronopposite faces make 9
d10pentagonal trapezohedron0 to 9, 0 counts 10; opposite faces make 9
d12dodecahedronopposite faces make 13
d20icosahedronopposite faces make 21
d100a tens die (00 to 90) and a d1000 and 0 is 100

6 and 9 carry a dot so they read the right way up.

Looks (look)

idNameDescription
classicClassic WhitePolished white with black pips on the d6 and crisp black numerals.
woodenTurned WoodOiled maple with a visible grain, burned-in numbers and pips, soft corners.
casinoSee-through CasinoSharp-edged clear cherry resin you can see through, bright white marks.
boneOld BoneYellowed ivory-like bone, faint pores, hand-inked brown marks.
metalForged MetalHeavy brushed steel, mirror edges, black enamel numbers.
gemstoneCut GemstoneFaceted see-through amethyst with gold-leaf numbers.
cartoonChunky CartoonCandy orange, very round corners, fat white numbers and a dark ink outline.
pixelPixel BlockHard-edged blocks with a pixel checker skin and voxel numerals.
neonNeon CircuitGlossy black with glowing cyan edges and hot pink light-up numbers (their own emissive materials).

Add your own: DK.registerLook{ id = "jade", name = "Jade", body = { color = { 0.2, 0.6, 0.4 }, roughness = 0.2 }, glyph = { color = { 1, 1, 1 } } } (call it on every peer).

Rolls (roll)

idNameDescription
throwThrowToss from the near edge in an arc; drag or swipe to aim and pick how hard.
cupShaker CupDice rattle in a leather cup, then it tips them out across the tray.
towerDice TowerDice drop into a tower behind the tray, clatter down three baffles and spill out of the ramp.
flickFinger FlickDice wait at the near edge; flick or swipe to send them skidding and tumbling.

Trays (style)

idNameDescription
feltFelt BoxWalnut box lined with green baize: a muffled thud, dice stop quickly.
woodenWooden TrayShallow maple tray with low rounded sides and handles: lively bounces.
arenaMetal ArenaRound brushed-steel ring: dice ricochet off the curve with a clink.
noneBare TableNo tray: dice roll on your table inside invisible walls.

Displays (display)

idNameDescription
floatFloating NumbersEach die's value pops up over it as it lands.
bannerTotal BannerA banner across the top with every value and the total.
tallyTally CardA card at the side with the last rolls: who, values, total.
noneNoneNothing: read DK.on("rolled") and draw your own.

How everyone sees the same roll

The peer that owns the tray's entity (the room host for a tray placed in a scene) is the roller. A roll asked for anywhere goes to it. It checks canRoll, then simulates the dice as real Physics dynamic bodies (hull colliders shaped like the dice) in a private copy of the tray far below the world, where nothing of your game can touch them, and records every die's pose 30 times a second as whole numbers, with a sound cue for every hit. The recording goes out in small chunks while the dice are still rolling. Every peer, the roller too, draws the dice from the recording on the shared session clock about half a second behind, so all screens show the same tumble and the same faces, and raise rolled together as the dice land. A cocked die (leaning on a wall or another die) is nudged and the roll carries on. The result and the resting poses are kept in the tray's synced state, so a player who joins later sees the dice where they lie. If the roller leaves mid-roll, the tray passes to another peer, which carries the roll on from the last frame everyone holds.

Scene requirements and limits

  • Call DK.Tray in a scene's build.luau content() (it spawns a synced entity, like any shared game object).
  • The tray draws itself; it does not need a table collider under it. Give your table its own look.
  • A camera that sees the tray: clicks and taps are matched to the tray's floor plane.
  • The built-in input listens to R, pad A, the left mouse button and touch; set input = false if your game owns those, and roll from code.
  • About 20 dice per roll at most; a roll is capped at 10 seconds of simulation.
  • A seeded roll lands the same on the same engine build while nothing else is being built or removed in the world during the roll (new bodies change the solver's order). Players never rely on that: the recording, not the seed, is what keeps every screen in step.

Guides

  • How to add physics dice to my game (roll dice that everyone sees land the same): guides { path = "diceKit.package/how-to-add-physics-dice-to-my-game-roll-dice-that-everyone-sees-land-the-same" }
  • How to make a dice rolling game, step by step (a two-player highest-roll game): guides { path = "diceKit.package/how-to-make-a-dice-rolling-game-step-by-step" }

Modules

diceKit.api (the interface), diceKit.DiceTray and diceKit.DiceGallery (components), diceKit.shapes, diceKit.recording, diceKit.sim, diceKit.rolls, diceKit.trays, diceKit.looks, diceKit.catalog, diceKit.meshes, diceKit.materials, diceKit.textures, diceKit.quat, diceKit.registry. Tests: diceKit.testSuite.

Versions

  • 1.0.10: changing the dice look mid-game raises no errors on any screen. The cause: rebuilding the dice in a new look kept each old die's rotation as a live reference to that die, which was despawned before the new die read it; the pose is now copied as plain values. The floating numbers, spatial dice sounds and the host's cup and recording also skip anything already gone. Reproduced with two clients (gemstone on the race tray, bone on the gallery tray) and checked clean after.
  • 1.0.9: no errors for a player who joins late. A tray whose view was rebuilt forgets the dice that went with it instead of moving them, a die or cup that is gone is never moved, and a joiner after a dice setting changed shows today's dice at rest rather than the last roll's. DK.on(name, fn, opts) takes a key or an owner entity, so listeners registered again in every play session no longer pile up. The README's title no longer carries a version number that fell behind (it said 1.0.6); the version is the line under it.
  • 1.0.8: a landing beat. As the result shows, a warm golden ring flashes out from under each die while its number pops up. The wooden tray's grain is tiled finer (it was stretched into broad streaks across the floor).
  • 1.0.7: the Roll hint names the input the player is using: on touch "Tap the tray to roll, or swipe across it to throw" (or "Flick across the tray"), on a pad "Press A to roll", with keys and mouse the R key and a click; a phone held sideways counts as a phone, so the hint fits its width. Checked by touch at 390x844 and 844x390: a tap on the tray and a swipe across it each rolled.