code wiki / _hdl_build / nx_engine_phys_gate.nx
nx_engine_phys_gate.nx source
↩ module page · 168 lines · 7203 B
1// nx_engine_phys_gate.nx -- ★ENGINE PHYSICS RUNG 1 (the census GAP axis): a rigid body FALLS under gravity, IMPACTS
2// the generated terrain, BOUNCES with restitution, and comes to REST exactly on the surface -- integer-deterministic.
3// (First physics rung = point-mass + ground collision; placed in-gate now, promoted to an organ when object-object
4// collision/inertia arrive -- ecosystem-placement noted.) T1 it falls. T2 it rests ON the surface. T3 it really
5// BOUNCED (dynamics, not a teleport-clamp). T4 causal: no gravity -> no fall. T5 bit-deterministic trajectories.
6// T6 evidence: air | impact | rest over the streamed worldpipe terrain (textured + sun-shadowed).
7// license_tier: ORIGINAL expect_exit: 0
8import "nx_syscalls.nx"
9import "nx_png.nx"
10import "nx_trimesh.nx"
11import "nx_worldpipe.nx"
12import "nx_objload.nx"
13
14func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
15func pn(v: i64) -> i64 { let b: *u8=sys_mmap(32) as *u8; var x: i64=v; var ng: i64=0; if x<0{ng=1;x=0-x} var i: i64=31; if x==0{b[i]=48 as u8;i=i-1} while x>0{b[i]=(48+x%10) as u8;x=x/10;i=i-1} if ng==1{b[i]=45 as u8;i=i-1} sys_write(1,(b as i64+i+1) as *u8,31-i); return 0 }
16func clearfb(fb: *i64, n: i64, c: i64) -> i64 { var i: i64=0; while i<n { fb[i]=c; i=i+1 } return 0 }
17
18const BR: i64 = 292 // body radius (the bunny's half-height at scale 450)
19const GRAV: i64 = 40 // gravity per tick (units/tick^2; tick = 1/64 s)
20const REST: i64 = 340 // restitution x1024 (energy lost each bounce)
21
22// simulate a drop at (x,z) from y0. out: [0]=final y, [1]=ticks, [2]=bounces, [3]=trajectory hash, [4]=min y seen
23func sim_drop(x: i64, z: i64, y0: i64, grav: i64, out: *i64) -> i64 {
24 let ground: i64 = wp_height(x, z)
25 var y: i64 = y0
26 var v: i64 = 0
27 var bounces: i64 = 0
28 var h: i64 = 1469598103934665603
29 var miny: i64 = y0
30 var t: i64 = 0
31 while t < 500 {
32 v = v + grav
33 y = y - v/16
34 if y - BR <= ground {
35 y = ground + BR
36 if v > 120 { v = 0 - v*REST/1024; bounces = bounces + 1 } else { v = 0 }
37 }
38 if y < miny { miny = y }
39 h = (h ^ y) * 1099511628211
40 t = t + 1
41 }
42 out[0] = y
43 out[1] = t
44 out[2] = bounces
45 out[3] = h
46 out[4] = miny
47 return ground
48}
49
50func main() -> i64 {
51 hw("=== nx_engine_phys_gate -- physics rung 1: fall, bounce, rest on the generated world ===\n" as *u8)
52 var fails: i64 = 0
53 wp_init(1)
54 wp_hydro_bake()
55
56 let out: *i64 = sys_mmap(64) as *i64
57 let dropX: i64 = 900
58 let dropZ: i64 = 620
59 let ground: i64 = sim_drop(dropX, dropZ, 1400, GRAV, out)
60 let finalY: i64 = out[0]
61 let bounces: i64 = out[2]
62 let hashA: i64 = out[3]
63 hw(" drop from 1400 over ground="); pn(ground); hw(": final y="); pn(finalY); hw(" (contact "); pn(finalY-BR); hw(") bounces="); pn(bounces); hw("\n" as *u8)
64
65 // ---- T1 it falls ----
66 var t1: i64 = 0
67 if out[4] < 1400 - 200 { t1 = 1 }
68 if t1 == 1 { hw("T1 PASS gravity pulls it down\n" as *u8) } else { fails=fails+1; hw("T1 FAIL\n" as *u8) }
69
70 // ---- T2 it RESTS exactly on the surface ----
71 var off: i64 = finalY - BR - ground
72 if off < 0 { off = 0-off }
73 var t2: i64 = 0
74 if off <= 2 { t2 = 1 }
75 if t2 == 1 { hw("T2 PASS at rest ON the terrain surface (contact off by "); pn(off); hw(")\n" as *u8) }
76 else { fails=fails+1; hw("T2 FAIL off="); pn(off); hw("\n" as *u8) }
77
78 // ---- T3 real dynamics: it bounced before settling ----
79 var t3: i64 = 0
80 if bounces >= 1 { if bounces <= 8 { t3 = 1 } }
81 if t3 == 1 { hw("T3 PASS it BOUNCED "); pn(bounces); hw(" time(s) with restitution before settling (dynamics, not a clamp)\n" as *u8) }
82 else { fails=fails+1; hw("T3 FAIL\n" as *u8) }
83
84 // ---- T4 causal: no gravity -> no fall ----
85 sim_drop(dropX, dropZ, 1400, 0, out)
86 var t4: i64 = 0
87 if out[0] == 1400 { t4 = 1 }
88 if t4 == 1 { hw("T4 PASS CAUSAL: without gravity it floats exactly where it was\n" as *u8) }
89 else { fails=fails+1; hw("T4 FAIL\n" as *u8) }
90
91 // ---- T5 bit-deterministic trajectory ----
92 sim_drop(dropX, dropZ, 1400, GRAV, out)
93 var t5: i64 = 0
94 if out[3] == hashA { t5 = 1 }
95 if t5 == 1 { hw("T5 PASS the whole trajectory is BIT-DETERMINISTIC (hash-equal across runs -- replayable physics)\n" as *u8) }
96 else { fails=fails+1; hw("T5 FAIL\n" as *u8) }
97
98 // ---- T6 evidence: air | impact | rest ----
99 tm_set_spec(0)
100 tm_set_image(0 as *u8, 0, 0)
101 let PW: i64 = 360
102 let PH: i64 = 330
103 let GW: i64 = PW*3
104 let gal: *i64 = sys_mmap(GW*PH*8) as *i64
105 clearfb(gal, GW*PH, 30 + 40*256 + 58*65536)
106 var slot: i64 = 0
107 while slot < 3 {
108 var by: i64 = 1400
109 if slot == 1 { by = ground + BR + 40 }
110 if slot == 2 { by = ground + BR }
111 // terrain chunk around the drop point + the bunny at sim height
112 tm_reset()
113 let okB: i64 = obj_load("knowledge/stdassets/bunny.obj" as *u8, 1300, 214+208*256+198*65536)
114 tm_place(0, tm_nv(), dropX, by, dropZ, 450)
115 let terrFrom: i64 = tm_nv()
116 var gz: i64 = 0
117 while gz < 33 {
118 var gx: i64 = 0
119 while gx < 33 {
120 let wx: i64 = dropX + (gx-16)*110
121 let wz: i64 = dropZ + (gz-16)*110
122 var hh: i64 = wp_height(wx, wz)
123 let slope: i64 = wp_iabs(wp_height(wx+110, wz) - hh) + wp_iabs(wp_height(wx, wz+110) - hh)
124 let col: i64 = wp_surface(wx, wz, hh, slope)
125 if hh < 0-14 { hh = 0-14 }
126 let vi: i64 = tm_vert(wx, hh, wz)
127 tm_vcol(vi, col)
128 gx = gx + 1
129 }
130 gz = gz + 1
131 }
132 gz = 0
133 while gz < 32 {
134 var qx: i64 = 0
135 while qx < 32 {
136 let v0: i64 = terrFrom + gz*33 + qx
137 tm_quad(v0, v0+1, v0+34, v0+33, 0)
138 qx = qx + 1
139 }
140 gz = gz + 1
141 }
142 tm_compute_normals()
143 tm_place(0, tm_nv(), 0-dropX, 0-ground-500, 0-dropZ, 1000)
144 tm_set_sun(340, 780, 0-300)
145 tm_shadow_bake()
146 tm_set_tex(70)
147 tm_set_shadow(1)
148 let fb: *i64 = sys_mmap(PW*PH*8) as *i64
149 clearfb(fb, PW*PH, 30 + 40*256 + 58*65536)
150 trimesh_render_aa(fb, PW, PH, 620, 0-300, 4600, 430, 2)
151 tm_set_shadow(0)
152 tm_set_tex(0)
153 var y: i64 = 0
154 while y < PH {
155 var x: i64 = 0
156 while x < PW { gal[y*GW + slot*PW + x] = fb[y*PW + x]; x = x + 1 }
157 y = y + 1
158 }
159 slot = slot + 1
160 }
161 write_png(gal, GW, PH, "knowledge/nx_engine_phys.png" as *u8)
162 hw("T6 evidence -> knowledge/nx_engine_phys.png (air | impact | rest)\n" as *u8)
163
164 if fails == 0 { hw("ENGINE-PHYS-GATE GREEN -- physics rung 1: gravity, terrain impact, restitution bounces, exact rest -- bit-deterministic (replayable). Residual: rotation/inertia, object-object collision, friction, joints/ragdoll.\n" as *u8); sys_exit(0); return 0 }
165 hw("ENGINE-PHYS-GATE RED fails="); pn(fails); hw("\n" as *u8)
166 sys_exit(1)
167 return 1
168}