Skip to content

Instantly share code, notes, and snippets.

@Chuzzy
Created January 27, 2021 16:40
Show Gist options
  • Select an option

  • Save Chuzzy/4da70794b078d0d89f8c546780279329 to your computer and use it in GitHub Desktop.

Select an option

Save Chuzzy/4da70794b078d0d89f8c546780279329 to your computer and use it in GitHub Desktop.
Rolls a die in Tabletop Simulator by launching it across the table
function rollDieRoutine()
local twopi = 2 * math.pi
-- First, generate a random angle
local angle = math.random() * 2 * twopi
-- Calculate position based on the angle
local x = die_launch_radius * math.cos(angle)
local z = die_launch_radius * math.sin(angle)
-- Move the die to that position
die.setPosition({x, 11.3, z})
-- Calculate launch angle - opposite initial angle
-- This way the die is launched towards the center
local launch = (angle + math.pi) % twopi
-- Calculate launch vector coords
local launch_x = die_launch_radius * math.cos(launch)
local launch_z = die_launch_radius * math.sin(launch)
-- Launch the die
-- Add some y force to counter-act gravity
die.addForce({launch_x, 2, launch_z})
die.addTorque({
randomFloat(1, 20),
randomFloat(1, 20),
randomFloat(1, 20)
})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment