1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
# -*- coding: utf-8 -*-
"""BLOQUES - the harmonic stack that overhangs the table without limit.
Blender designs (table, floor, wooden blocks) and exports to glTF; Godot
(Jolt) decides what holds and what falls. There are three experiments, each
in its own lane of the table (different Y) so they do not touch; when
rendering, the current one is brought to the front and the others hidden.
A: the "obvious" staircase, each block shifted half a length -> falls
B: 5 harmonic blocks (1/2, 1/4, 1/6, 1/8, 1/10) -> holds
C: 16 harmonic blocks; at beat 8 one more is put on top -> all fall
The harmonic shifts are scaled by SHIFT_SCALE = 0.95: that way the centre of
mass of each sub-stack stays (1-C)/2 = 2.5 % of a length inside the edge
holding it, on every level. Without that margin, the solver decides at random.
"""
import math, os, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import bpy
from base import *
NAME_KEY = "bloques"
L, H, P = 1.0, 0.16, 0.50 # block length, height, depth
SHIFT_SCALE = 0.95
C_TOWER = float(os.environ.get("T_C", SHIFT_SCALE)) # the tall tower needs more margin with the solver
Z_FLOOR = -3.5 # 70 cm table with 20 cm blocks
GRAVITY = 49.0 # 9.81 / 0.2: real 20 cm blocks
LANE = {"a": 0.0, "b": 3.0, "c": 6.0, "x": 6.0}
N_B, N_C = 5, 16
DX_EXTRA = float(os.environ.get("T_DX", -0.45)) # where the extra block rests
def block_stack(n, c=SHIFT_SCALE):
"""Centre x of a harmonic stack of n blocks (index 0 = the top one)."""
xs = [0.0] * n
x = -L / 2 # the table acts as block n+1
for k in range(n, 0, -1):
x += c * L / (2 * k)
xs[k - 1] = x
return xs
def z_de(n, k):
"""Height of the centre of block k (0 = top) in a stack of n."""
return (n - 1 - k) * H + H / 2
def build_scene():
scene_setup(samples=int(env("SAMPLES", "MUESTRAS", 24)))
studio_world(force=0.55)
M = {
"bloque": madera("bloque", light_c="#E2B880", dark="#B07A45", scale_to=5.0, rough=0.5),
"arriba": material("arriba", "ambar", rough=0.35),
"mesa": madera("mesa", light_c="#6B4426", dark="#2E1A0C", scale_to=2.5, rough=0.35,
grain=(0.4, 1, 1)),
"piso": madera("piso", light_c="#3A3F4B", dark="#1E222B", scale_to=1.5, rough=0.6,
grain=(0.3, 1, 1)),
"invisible": material("inv", "gris"),
}
phys = []
table = box_obj("box_mesa", (6.0, 9.0, 0.3), (-3.0, 3.0, -0.15), M["invisible"])
floor_obj = box_obj("box_piso", (30.0, 30.0, 0.2), (0.0, 3.0, Z_FLOOR - 0.1), M["invisible"])
table.hide_render = floor_obj.hide_render = True
phys += [table, floor_obj]
# what shows of the table and the floor: only the front lane
box_obj("mesa_v", (6.0, 2.4, 0.3), (-3.0, 0.4, -0.15), M["mesa"])
m_leg = metal("pata", "#2A2F3A", rough=0.4)
box_obj("pata", (0.14, 0.14, -Z_FLOOR - 0.3), (-0.35, -0.55, (Z_FLOOR - 0.3) / 2), m_leg)
box_obj("pata2", (0.14, 0.14, -Z_FLOOR - 0.3), (-0.35, 1.3, (Z_FLOOR - 0.3) / 2), m_leg)
box_obj("piso_v", (14.0, 5.0, 0.2), (1.0, 1.0, Z_FLOOR - 0.1), M["piso"])
bl = {}
# A: "obvious" staircase
for k in range(5):
x = -0.25 + 0.5 * (4 - k)
bl[f"a{k}"] = box_obj(f"block_a{k}", (L, P, H), (x, LANE["a"], z_de(5, k)), M["bloque"])
# B: harmonic stack of 5
for k, x in enumerate(block_stack(N_B)):
bl[f"b{k}"] = box_obj(f"block_b{k}", (L, P, H), (x, LANE["b"], z_de(N_B, k)),
M["arriba"] if k == 0 else M["bloque"])
# C: harmonic stack of 16 and the extra block, waiting on top
xc = block_stack(N_C, C_TOWER)
for k, x in enumerate(xc):
bl[f"c{k:02d}"] = box_obj(f"block_c{k:02d}", (L, P, H), (x, LANE["c"], z_de(N_C, k)),
M["arriba"] if k == 0 else M["bloque"])
bl["x"] = box_obj("block_x", (L, P, H), (xc[0] + DX_EXTRA, LANE["x"], z_de(N_C, 0) + H + 0.005),
M["bloque"])
phys += list(bl.values())
return dict(phys=phys, bl=bl, M=M)
def events(T):
t = lambda f: (f - 1) / FPS
fa = T.span(1)[0] + int(0.55 * (T.span(1)[1] - T.span(1)[0]))
fx = T.span(8)[0] + int(0.70 * (T.span(8)[1] - T.span(8)[0]))
return {"a": fa, "b": T.span(4)[0], "c": T.span(7)[0], "x": fx}, [
{"t": t(fa), "action": "release", "prefix": "block_a"},
{"t": t(T.span(4)[0]), "action": "release", "prefix": "block_b"},
{"t": t(T.span(7)[0]), "action": "release", "prefix": "block_c"},
{"t": t(fx), "action": "release", "prefix": "block_x"},
]
def simulate(T, obj):
_, ev = events(T)
cfg = {
"duration": T.n_frames / FPS + 0.2,
"gravity": GRAVITY,
"scale": float(env("T_SCALE", None, 5.0)),
"hz": int(os.environ.get("T_HZ", 1920)),
"frozen": ["block_"],
"rules": {
"block_": {"friction": 0.55, "bounce": 0.08, "density": 600},
"box_": {"friction": 0.6, "bounce": 0.05},
},
"events": ev,
}
run_godot(export_physics(NAME_KEY, obj["phys"], cfg))
def main():
T = Timeline(NAME_KEY)
obj = build_scene()
if env("MODE", "MODO") == "sim":
simulate(T, obj)
return
idx, D = load_sim(NAME_KEY)
bl, M = obj["bl"], obj["M"]
release_at, _ = events(T)
xc = block_stack(N_C, C_TOWER)
# report: what is still standing
for g, n in (("a", 5), ("b", N_B), ("c", N_C)):
nm_key = [k for k in bl if k.startswith(g) and k != "x"]
zmin = min(D[-1, idx[bl[k].name], 2] for k in nm_key)
zmin_before = min(D[min(len(D) - 1, release_at["x"] - 5), idx[bl[k].name], 2] for k in nm_key)
print(f"[{NAME_KEY}] stack {g}: minimum z at the end {zmin:+.2f} "
f"(before the extra block {zmin_before:+.2f})")
print(f"[{NAME_KEY}] B: the top one starts at x={block_stack(N_B)[0] - L / 2:+.3f} "
f"(overhangs {block_stack(N_B)[0] + L / 2:.3f}); C overhangs {xc[0] + L / 2:.3f}")
# --- drawings -------------------------------------------------------------
m_edge = material("borde", "celeste", emit=2.5)
edge = curve_poly("borde", [[(0, -0.30, z0), (0, -0.30, z0 + 0.09)]
for z0 in [0.02 + 0.16 * i for i in range(18)]],
thickness_px=0.012, mat=m_edge)
label_list = []
xb = block_stack(N_B)
for k in range(N_B):
num = txt_m(f"1/{2 * (k + 1)}", size_u=0.15, color="ambar")
# shift of this block relative to the one below, to its right
num.location = (xb[k] + L / 2 + 0.16, -0.30, z_de(N_B, k))
label_list.append(num)
air = curve_poly("aire", [[(0, -0.3, 0.9), (xb[0] - L / 2, -0.3, 0.9)]],
thickness_px=0.02, mat=material("aire", "rosa", emit=3.0))
air_t = txt_m("en el aire", size_u=0.16, color="rosa")
air_t.location = (0.45, -0.3, 1.08)
# centres of mass of each sub-stack, over the edge that holds it
cms = []
for k in range(1, N_B + 1):
x_cm = sum(xb[:k]) / k
e = sphere(f"cm{k}", 0.045, material(f"mcm{k}", "ambar", emit=3.0))
e.location = (x_cm, -0.30, z_de(N_B, k - 1) - H / 2)
cms.append(e)
count = txt_m("16 bloques", size_u=0.22, color="blanco")
onto = txt_m("sobresale 1,6 bloques", size_u=0.18, color="ambar")
ruler = curve_poly("regla", [[(0, -0.3, 2.78), (xc[0] + L / 2, -0.3, 2.78)]],
thickness_px=0.018, mat=material("regla", "ambar", emit=3.0))
lens = 50.0
cam = camera_obj((0, -8, 1), (0, 0, 0), lens=lens)
cam.data.sensor_fit = 'VERTICAL'
cam.data.sensor_height = 36.0
light_obj("key", 'AREA', (-3.0, -4.0, 5.0), 1400, "blanco", size_u=4.0, sight=(0.5, 0, 0.8))
light_obj("fill", 'AREA', (4.0, -3.0, 0.5), 350, "blanco", size_u=4.0, sight=(0.5, 0, 0.5))
light_obj("rim", 'AREA', (1.0, 4.0, 4.0), 600, "blanco", size_u=3.0, sight=(0.5, 0, 1.0))
|