Modular Marble Run
A marble run built from modules on a square grid: straight ramps, left and right curves, a spiral funnel, a drop tube, support columns and a pegged base plate, all in one file. Every module covers whole grid cells and plugs onto square pegs, track inlets and outlets sit on whole height units, so any outlet meets the next inlet when the next module stands one unit lower. The funnel spans 3 x 3 cells on four posts and empties through its centre hole onto the track below. The file checks that a marble rides every channel, that inlet and outlet floors land on the height grid, that modules seat on the pegs and that neighbouring modules do not collide. The default shows an example course of 160 x 200 x 126 mm on a 4 x 5 base; choose a piece to get one printable part (a 2-cell straight is 80 x 23 x 32 mm, the funnel 111 x 103 x 32 mm). Each part prints seat down without supports. Marble diameter (16 mm by default), grid pitch, height unit, wall, peg fit, straight length and fall, drop height, support height and base size are parameters.
# Marble Run - modular track system: straight, curve, funnel, drop, support column and base plate in one file
#
# One grid, one connector, one track section:
# - grid: every module covers whole cells of `grid_pitch` mm; tracks run along
# the cell centre lines and end exactly on cell borders.
# - connector: a square peg (base plate, top of every support) and a square
# socket (bottom of every module and support) at every cell centre. The
# square peg fixes a module in one of four directions.
# - track: a round-bottomed U channel sized from the marble diameter.
# - heights: measured from the plane a module sits on (its seat), every
# track inlet is 2 units and every outlet 1 unit of `height_unit` mm up (the
# drop tube's inlet is drop_units + 1 up, a straight can fall 2 units).
# Supports are whole units tall, so with the next module one unit lower
# an outlet always meets an inlet. The funnel empties through its centre
# hole into whatever stands below it.
#
# piece = straight | curve | funnel | drop | support | base are printable
# parts, each in its print orientation: seat flat on Z = 0, channels open
# upwards, sockets are short bridges, the exit tunnel of the drop has a
# 45 deg roof. No supports.
# piece = course shows an example: funnel -> straight -> curve -> drop ->
# straight on a 4 x 5 base, every module on support columns (not a print).
#
# Module frame: the first cell centre is the origin, the marble travels
# towards +X. A left curve leaves towards +Y.
@param choices:["straight", "curve", "funnel", "drop", "support", "base", "course"] desc:"Module to print, or the example course (assembled view)"
piece = "course"
@param 10..25 step:0.5 desc:"Marble diameter (mm)" group:"System"
marble_d = 16.0
@param 40..60 step:5 desc:"Grid pitch: cell size (mm)" group:"System"
grid_pitch = 40
@param 8..20 step:1 desc:"Height unit (mm)" group:"System"
height_unit = 10
@param 1.6..3 step:0.2 desc:"Track wall thickness (mm)" group:"System"
wall = 2.0
@param 0.1..0.4 step:0.05 desc:"Peg to socket clearance, per side (mm)" group:"System"
fit = 0.2
@param 1..4 step:1 desc:"Straight: length in cells" group:"Straight"
cells = 2
@param 1..2 step:1 desc:"Straight: fall in height units" group:"Straight"
fall = 1
@param choices:["left", "right"] desc:"Curve: turn direction" group:"Curve"
turn = "left"
@param 2..8 step:1 desc:"Drop: fall of the tube in height units" group:"Drop"
drop_units = 3
@param 1..12 step:1 desc:"Support: height in units" group:"Support"
units = 3
@param 1..6 step:1 desc:"Base: cells in X" group:"Base"
base_nx = 3
@param 1..6 step:1 desc:"Base: cells in Y" group:"Base"
base_ny = 3
# ---------------------------------------------------------------- shared dimensions
G = grid_pitch
hu = height_unit
R = marble_d / 2 # marble radius
rc = R + 1.5 # channel radius
tw = 2 * (rc + wall) # track outer width
rim = 2 # channel walls rise this far above the channel axis
eg = 0.2 # every module ends this far inside its cell border
peg_w = 10 # square peg
peg_h = 5
sock_w = peg_w + 2 * fit
sock_d = peg_h + 0.6
col_w = 20 # support column
base_t = 4
h_out = hu # outlet floor above the seat, every track module
h_in = 2 * hu # inlet floor above the seat: curve, funnel
h_in_s = (1 + fall) * hu # straight
h_in_d = (1 + drop_units) * hu # drop tube
assert G >= tw + 8 # room between neighbouring tracks
assert G / 2 >= rc + wall + 1 # the curve has an inner wall
assert hu - sock_d >= 2.4 # floor left over a socket under an outlet
assert col_w >= sock_w + 2 * 3
# ---------------------------------------------------------------- connector
peg0 = box peg_w peg_w (peg_h + 0.5) | edges =Z | fillet 1 | edges >Z | chamfer 0.8 | floor
sock0 = box sock_w sock_w (sock_d + 1) | edges =Z | fillet (1 + fit) | floor
def peg(x, y, z) = peg0 | translate x y (z - 0.5)
def sock(x, y) = sock0 | translate x y -1
# ---------------------------------------------------------------- track cutter
# U channel along +X from x = -10 to len - 10, axis on the X axis: a round
# bottom of radius rc and straight walls above it.
def ucut(len) = (cylinder rc len | rotate 0 90 0 | translate (len / 2 - 10) 0 0) + (box len (2 * rc) 80 | translate (len / 2 - 10) 0 40)
def ball(x, y, z) = sphere R | translate x y z
# a marble rides at (x, y) with its centre at z: free there, and carried
# (0.6 mm lower it is inside the floor)
def rides(m, x, y, z) = clear(m, ball(x, y, z)) and clear(m, ball(x, y, z - 0.6)) == false
# height of the highest material on the vertical line through (x, y)
needle = box 0.1 0.1 600
def needle_at(x, y) = needle | translate x y 0
def floor_z(m, x, y) = (m * needle_at(x, y)).bbox.max[2]
def on_grid(h) = abs(h - round(h / hu) * hu) < 0.05
nd = eg + 0.05 # the needles stand this far inside the cell border; floors are extrapolated to the border
def put(m, ang, i, j, z) = m | rotate 0 0 ang | translate (i * G) (j * G) z
# ---------------------------------------------------------------- straight
def s_ang(n, f) = atan(f * hu / (n * G)) # slope
def s_len(n, f) = n * G / cos(s_ang(n, f)) + 20
def s_body(n, f) = box (n * G - 2 * eg) tw ((1 + f) * hu + rc + rim) | edges =Z | fillet 1.5 | floor | translate (n * G / 2 - G / 2) 0 0
# channel and everything above the walls, along +X before it is tilted
def s_cut0(len) = (cylinder rc len | rotate 0 90 0 | translate (len / 2 - 10) 0 0) + (box len (2 * rc) 80 | translate (len / 2 - 10) 0 40) + (box len (tw + 2) 80 | translate (len / 2 - 10) 0 (rim + 40))
def s_cut(n, f) = s_cut0(s_len(n, f)) | rotate 0 s_ang(n, f) 0 | translate (-G / 2) 0 ((1 + f) * hu + rc / cos(s_ang(n, f)))
def s_socks(n) = union [sock(i * G, 0) for i in range(n)]
def straight_of(n, f) = s_body(n, f) - s_cut(n, f) - s_socks(n)
sL = cells * G
sa = s_ang(cells, fall)
s_h = h_in_s + rc + rim
straight = straight_of(cells, fall)
def s_floor(x) = h_in_s - (x + G / 2) * tan(sa)
def s_ride(t) = rides(straight, -G / 2 + t * sL, 0, s_floor(-G / 2 + t * sL) + R / cos(sa) + 0.1)
assert straight.solids == 1
assert straight.bbox.x ~ (sL - 2 * eg) tol:0.01
assert straight.bbox.y ~ tw tol:0.01
assert straight.bbox.min[2] ~ 0 tol:0.001
s_zin = floor_z(straight, -G / 2 + nd, 0) + nd * tan(sa)
s_zout = floor_z(straight, sL - G / 2 - nd, 0) - nd * tan(sa)
assert s_zin ~ h_in_s tol:0.05
assert s_zout ~ h_out tol:0.05
assert on_grid(s_zin)
assert on_grid(s_zout)
assert s_ride(0.1)
assert s_ride(0.3)
assert s_ride(0.5)
assert s_ride(0.7)
assert s_ride(0.9)
# ---------------------------------------------------------------- curve (left: in along +X, out along +Y)
c_r = G / 2 # centre line radius, about the corner (-G/2, G/2)
c_a = atan(hu / (pi * c_r / 2)) # slope along the centre line
c_h = h_in + rc + rim
c_ext = 12 # the cutter runs this many degrees past both ends
c_prof = (rect (2 * rc) 80 at:(0, -40)) + (circle rc)
c_sw = helix (4 * hu) (hu * (90 + 2 * c_ext) / 90) c_r | sweep (c_prof)
c_cut = c_sw | mirror "Y" | rotate 0 0 c_ext | translate (-G / 2) (G / 2) (h_out + rc / cos(c_a) - hu * c_ext / 90)
c_body = box (G - 2 * eg) (G - 2 * eg) c_h | edges =Z | fillet 1.5 | floor
curve_l = c_body - c_cut - sock(0, 0)
curve = if turn == "left" then curve_l else (curve_l | mirror "Y")
# centre line at angle q (-90 at the inlet, 0 at the outlet)
def c_x(q) = -G / 2 + c_r * cos(q)
def c_y(q) = G / 2 + c_r * sin(q)
def c_floor(q) = h_out - hu * q / 90
def c_ride(q) = rides(curve_l, c_x(q), c_y(q), c_floor(q) + R / cos(c_a) + 0.1)
assert curve.solids == 1
assert curve.bbox.x ~ (G - 2 * eg) tol:0.01
assert curve.bbox.z ~ c_h tol:0.01
c_zin = floor_z(curve_l, -G / 2 + nd, 0) + nd * tan(c_a)
c_zout = floor_z(curve_l, 0, G / 2 - nd) - nd * tan(c_a)
assert c_zin ~ h_in tol:0.05
assert c_zout ~ h_out tol:0.05
assert on_grid(c_zin)
assert on_grid(c_zout)
assert c_ride(-80)
assert c_ride(-60)
assert c_ride(-45)
assert c_ride(-30)
assert c_ride(-10)
# ---------------------------------------------------------------- funnel (3 x 3 cells, centre hole)
# The inlet runs along the cell row y = -G and meets the bowl tangentially,
# so the marble circles counter-clockwise on a floor that gets steeper
# towards the hole: z = zf0 - k (1/p - 1/G) - m (G - p) at radius p.
f_rb = G + rc # bowl radius; the outer lane is a round groove on radius G
f_ro = f_rb + wall
f_h = h_in + rc + rim
f_rh = R + 2.5 # hole radius
zf0 = h_in - 2 # floor on the outer lane
f_lip = 5 # floor at the hole
f_m = 0.06
f_k = (zf0 - f_lip - f_m * (G - f_rh)) / (1 / f_rh - 1 / G)
assert f_k > 0
assert 1.5 * G - eg >= f_ro
def f_floor(p) = zf0 - f_k * (1 / p - 1 / G) - f_m * (G - p)
fN = 16
def f_p(i) = G - (G - f_rh) * i / fN
f_arc = [(G + rc * cos(-5 * i), zf0 + rc + rc * sin(-5 * i)) for i in range(18)]
f_fl = [(f_p(i), f_floor(f_p(i))) for i in range(fN + 1)]
f_pts = [(0, f_h + 1), (f_rb, f_h + 1)] + f_arc + f_fl + [(f_rh, -1), (0, -1)]
f_air = polyline f_pts | revolve Y | rotate 90 0 0
f_pw = sock_w + 6
f_ph = hu + 2
f_post0 = box f_pw f_pw f_ph | edges =Z | fillet 2 | floor
f_posts = union [f_post0 | translate ((2 * (i % 2) - 1) * G) ((2 * (i // 2) - 1) * G) 0 for i in range(4)]
# a rib ties every post to the bowl wall, so the posts stay part of the funnel
# at any grid pitch (the post centres are G * sqrt(2) from the axis)
f_rl = max(G * sqrt(2) - f_ro + 4, 2)
f_rib0 = box f_rl f_pw f_ph | floor | translate (G * sqrt(2) - f_rl / 2) 0 0
f_ribs = union [f_rib0 | rotate 0 0 (45 + 90 * i) for i in range(4)]
f_spl = 1.5 * G - eg
f_spout = box f_spl tw f_h | floor | translate (-f_spl / 2) (-G) 0
f_sa = atan(2 / (1.5 * G))
f_in0 = ucut(1.5 * G + 10 + rc / 2)
f_in = f_in0 | rotate 0 f_sa 0 | translate (-1.5 * G) (-G) (h_in + rc / cos(f_sa))
f_socks = union [sock((2 * (i % 2) - 1) * G, (2 * (i // 2) - 1) * G) for i in range(4)]
f_cyl = cylinder f_ro f_h | floor
f_body = f_cyl + f_posts + f_ribs + f_spout
funnel = f_body - f_air - f_in - f_socks
assert funnel.solids == 1
assert funnel.bbox.z ~ f_h tol:0.01
assert funnel.bbox.x <= 3 * G - 2 * eg + 0.01
assert funnel.bbox.y <= 3 * G - 2 * eg + 0.01
f_zin = floor_z(funnel, -1.5 * G + nd, -G) + nd * tan(f_sa)
assert f_zin ~ h_in tol:0.05
assert on_grid(f_zin)
assert rides(funnel, -1.5 * G + 6, -G, h_in - 6 * tan(f_sa) + R + 0.1) # inlet
assert rides(funnel, 0, -G, zf0 + R + 0.1) # where the inlet meets the bowl
assert rides(funnel, G, 0, zf0 + R + 0.1) # outer lane, a quarter turn on
assert rides(funnel, 0, G, zf0 + R + 0.1)
def f_mid(p) = rides(funnel, -p, 0, f_floor(p) + R * sqrt(1 + (f_k / p / p + f_m) ** 2) + 0.15)
assert f_mid(0.8 * G) # spiralling in
assert f_mid(0.6 * G)
assert clear(funnel, ball(0, 0, f_lip)) # through the hole
assert clear(funnel, ball(0, 0, 0))
assert clear(funnel, ball(0, 0, -R))
# ---------------------------------------------------------------- drop (1 cell: in at the top, out at the bottom, both along +X)
d_h = h_in_d + rc + rim
d_ea = 15 # slope of the exit tunnel (deg)
d_za = h_out + rc / cos(d_ea) # tunnel axis at the outlet face
d_zb = d_za + G / 2 * tan(d_ea) # tunnel axis under the tube axis
d_body = box (G - 2 * eg) tw d_h | edges =Z | fillet 1.5 | floor
d_bore = cylinder rc (d_h + 1 - d_zb) | floor | translate 0 0 d_zb
d_tear = (circle rc) + (polyline [(-rc * sqrt(2), 0), (-rc * cos(45), rc * sin(45)), (-rc * cos(45), -rc * sin(45))])
d_tl = (G / 2 + rc) / cos(d_ea) + 6
d_tun = d_tear | extrude d_tl | rotate 0 90 0 | rotate 0 d_ea 0 | translate (-rc) 0 (d_zb + rc * tan(d_ea))
d_ia = atan(1.5 / (G / 2))
d_in0 = ucut(G / 2 + 12)
d_in = d_in0 | rotate 0 d_ia 0 | translate (-G / 2) 0 (h_in_d + rc / cos(d_ia))
drop = d_body - d_bore - d_tun - d_in - sock(0, 0)
assert drop.solids == 1
assert drop.bbox.z ~ d_h tol:0.01
assert d_h >= d_zb + rc * sqrt(2) + 2 # the tunnel roof stays inside the body
d_zin = floor_z(drop, -G / 2 + nd, 0) + nd * tan(d_ia)
d_low = box 0.1 0.1 d_za | floor | translate (G / 2 - nd) 0 0 # a needle that ends below the tunnel roof
d_zout = (drop * d_low).bbox.max[2] - nd * tan(d_ea)
assert d_zin ~ h_in_d tol:0.05
assert d_zout ~ h_out tol:0.05
assert on_grid(d_zin)
assert on_grid(d_zout)
def d_floor(x) = h_out + (G / 2 - x) * tan(d_ea)
def d_ride(x) = rides(drop, x, 0, d_floor(x) + R / cos(d_ea) + 0.1)
assert rides(drop, -G / 2 + 4, 0, h_in_d - 4 * tan(d_ia) + R + 0.1) # inlet
assert clear(drop, ball(0, 0, h_in_d + R)) # down the tube
assert clear(drop, ball(0, 0, (h_in_d + d_zb) / 2))
assert d_ride(0) # on the exit ramp
assert d_ride(G / 4)
assert d_ride(G / 2 - 2)
# ---------------------------------------------------------------- support column
def sup(u) = (box col_w col_w (u * hu) | edges =Z | fillet 2 | floor) + peg(0, 0, u * hu) - sock(0, 0)
support = sup(units)
assert support.solids == 1
assert support.bbox.z ~ (units * hu + peg_h) tol:0.01
assert support.bbox.x ~ col_w tol:0.01
# ---------------------------------------------------------------- base plate
def base_of(nx, ny) = (box (nx * G - 2 * eg) (ny * G - 2 * eg) base_t | edges =Z | fillet 3 | floor | translate ((nx - 1) * G / 2) ((ny - 1) * G / 2) 0) + (union [peg((i % nx) * G, (i // nx) * G, base_t) for i in range(nx * ny)])
base = base_of(base_nx, base_ny)
assert base.solids == 1
assert base.bbox.x ~ (base_nx * G - 2 * eg) tol:0.01
assert base.bbox.y ~ (base_ny * G - 2 * eg) tol:0.01
assert base.bbox.z ~ (base_t + peg_h) tol:0.01
# ---------------------------------------------------------------- connector fit
# A module sits on a peg: free when seated, and held sideways (pushed by the
# clearance + 0.3 mm in X or Y it hits the peg). m is given seated at z = 0.
sup1 = sup(1) | translate 0 0 (-hu)
def held(m, x, y) = clear(m, sup1 | translate x y 0) and clear(m, sup1 | translate (x + fit + 0.3) y 0) == false and clear(m, sup1 | translate x (y + fit + 0.3) 0) == false
assert held(straight, 0, 0)
assert held(straight, (cells - 1) * G, 0)
assert held(curve, 0, 0)
assert held(drop, 0, 0)
assert held(funnel, G, G)
assert held(funnel, -G, -G)
assert held(funnel, -G, G)
assert held(funnel, G, -G)
assert held(support, 0, 0) # supports stack
base1 = base_of(1, 1) | translate 0 0 (-base_t)
assert clear(support, base1) # a support on the base plate
assert clear(support, base1 | translate (fit + 0.3) 0 0) == false
assert clear(straight, base1) # a track directly on the base plate
# ---------------------------------------------------------------- module to module
# The next module is a straight, one grid step on and seated lower by the
# straight's fall: the floors meet, the parts are clear of each other and a
# marble passes the joint.
nz = h_out - h_in_s
def joint(a, b, x, y) = clear(a, b) and clear(a, ball(x, y, h_out + R + 1)) and clear(b, ball(x, y, h_out + R + 1)) and clear(b, ball(x, y, h_out + R - 1)) == false
# straight -> straight
s_next = put(straight, 0, cells, 0, nz)
assert joint(straight, s_next, sL - G / 2, 0)
assert (floor_z(s_next, sL - G / 2 + nd, 0) + nd * tan(sa)) ~ s_zout tol:0.05
# curve -> straight (leaving along +Y)
c_next = put(straight, 90, 0, 1, nz)
assert joint(curve_l, c_next, 0, G / 2)
assert (floor_z(c_next, 0, G / 2 + nd) + nd * tan(sa)) ~ c_zout tol:0.05
# straight -> curve, one unit lower
sc_next = put(curve_l, 0, cells, 0, h_out - h_in)
assert joint(straight, sc_next, sL - G / 2, 0)
assert (floor_z(sc_next, sL - G / 2 + nd, 0) + nd * tan(c_a)) ~ s_zout tol:0.05
# drop -> straight
d_next = put(straight, 0, 1, 0, nz)
assert joint(drop, d_next, G / 2, 0)
assert (floor_z(d_next, G / 2 + nd, 0) + nd * tan(sa)) ~ d_zout tol:0.05
# straight -> drop: the drop sits drop_units lower
sd_next = put(drop, 0, cells, 0, h_out - h_in_d)
assert clear(straight, sd_next)
assert (floor_z(sd_next, sL - G / 2 + nd, 0) + nd * tan(d_ia)) ~ s_zout tol:0.05
# funnel over a straight: the straight stands f_under units lower, its first
# cell under the hole; the marble falls into the channel
f_under = ceil((s_h + 1) / hu)
f_next = put(straight, 0, 0, 0, -f_under * hu)
assert clear(funnel, f_next)
assert rides(f_next, 0, 0, -f_under * hu + s_floor(0) + R / cos(sa) + 0.1)
assert clear(funnel, ball(0, 0, -f_under * hu + s_floor(0) + R / cos(sa) + 0.1))
# ---------------------------------------------------------------- example course (assembled view)
# funnel (hole over cell 0,0) -> straight (2 cells, 1 unit) along +X -> left
# curve -> drop along +Y -> straight on the base plate. Seats in units above
# the base plate, worked back from the last straight; parts are shown 0.05 mm
# apart so they stay separate solids.
k_s = straight_of(2, 1)
k_under = ceil((2 * hu + rc + rim + 1) / hu)
k_ud = 1 # drop: its outlet (1) meets the straight's inlet (2)
k_uc = k_ud + drop_units # curve
k_us = k_uc + 1 # upper straight
k_uf = k_us + k_under # funnel
k_g = 0.05
def k_z(u) = base_t + u * hu + (if u > 0 then 2 * k_g else k_g)
def k_sup(u, i, j) = sup(u) | translate (i * G) (j * G) (base_t + k_g)
def k_base() = base_of(4, 5) | translate (-G) (-G) 0
def k_tracks() = union [put(funnel, 0, 0, 0, k_z(k_uf)), put(k_s, 0, 0, 0, k_z(k_us)), put(curve_l, 0, 2, 0, k_z(k_uc)), put(drop, 90, 2, 1, k_z(k_ud)), put(k_s, 90, 2, 2, k_z(0))]
def k_stands() = union [k_sup(k_uf, -1, -1), k_sup(k_uf, 1, -1), k_sup(k_uf, -1, 1), k_sup(k_uf, 1, 1), k_sup(k_us, 0, 0), k_sup(k_us, 1, 0), k_sup(k_uc, 2, 0), k_sup(k_ud, 2, 1)]
def course() = union [k_base(), k_stands(), k_tracks()]
# marbles at the three joints and under the funnel hole, 1 mm above the floor
def k_balls() = union [ball(1.5 * G, 0, k_z(k_us) + h_out + R + 1), ball(2 * G, 0.5 * G, k_z(k_uc) + h_out + R + 1), ball(2 * G, 1.5 * G, k_z(k_ud) + h_out + R + 1), ball(0, 0, k_z(k_us) + 2 * hu + R + 1)]
def course_ok() = k_tracks().solids == 5 and k_stands().solids == 8 and course().solids == 14 and clear(k_tracks(), k_stands()) and clear(k_tracks(), k_base()) and clear(k_stands(), k_base()) and clear(course(), k_balls())
assert if piece == "course" then course_ok() else true
print "track: channel radius" rc "outer width" tw "height unit" hu "grid" G
print "inlet / outlet floor above the seat: straight" s_zin s_zout "curve" c_zin c_zout "drop" d_zin d_zout "funnel inlet" f_zin
print "slopes (deg): straight" sa "curve" c_a "drop exit" d_ea
if piece == "straight" then straight
else if piece == "curve" then curve
else if piece == "funnel" then funnel
else if piece == "drop" then drop
else if piece == "support" then support
else if piece == "base" then base
else course()