diff --git a/docs/soa-world.md b/docs/soa-world.md index 6a839b9..f45bfc5 100644 --- a/docs/soa-world.md +++ b/docs/soa-world.md @@ -13,8 +13,8 @@ Box3D decides every physical outcome; Ymir creates bodies, steps the world and e - Body lanes (`ymir_box3d_soa_view`): `stable_id`, `kind`, `parent` and `limb` (set at creation, not read by Box3D: the limb tree for the consumer), `size_0`, `size_1`, `size_2` (read-only shape size set at creation, never changed by `step`: sphere {radius}, capsule {radius, half_length} along local Y, box and static box - {half_x, half_y, half_z}; unused components 0; shapes are centred on the body origin, so a renderer draws - every body from kind and size), position xyz, rotation quaternion xyzw, linear and angular + {half_x, half_y, half_z}; unused components 0; dynamic and static box shapes are centred on the body origin, whereas + `StaticHeightField` (kind 6, anchored at grid `(0, 0)`) and `StaticMesh` (kind 7, local vertex coordinates) leave `size_0..2 = 0`), position xyz, rotation quaternion xyzw, linear and angular velocity xyz. One contiguous array per component, indexed by body. - Write lanes: per-body world `force` (at the centre of mass) and `torque`, xyz, mutable through the view. `step` applies them to the dynamic bodies and clears them, so a lane holds one step's intent. A @@ -265,7 +265,7 @@ Box3D decides every physical outcome; Ymir creates bodies, steps the world and e `rederive_wet`); a fluid particle keeps its material friction. Body-body contact goes through the GameCult Box3D fork's `b3World_SetContactFrictionCallback`: `b3UpdateConvexContact` calls the hook right after the mix (`sqrt(frictionA frictionB)`) with the mean of the manifold points in each body's frame (one point per manifold), both bodies' user data (lane + 1) and the mixed value, and the world's hook returns `wet_mu` on B of `wet_mu` on A of it: it only lowers. The read is race-free (patches change only between steps) and a world with no settled liquid steps byte for - byte as before. Mesh contacts are not hooked (Ymir makes none). The fork runs the hook on a recycled contact too (Box3D skips a recycled contact's update, and with it the friction mix): it mixes again and calls the hook, only when a hook is installed, so friction follows a patch settled or removed under a body at rest. The lookup is culled: `rederive_wet` indexes the live body patches by body then centre x with each body's box and largest radius (`wet_order`, `wet_body_start`, `wet_body_count`, `wet_body_box`), so a point outside the body's box is a constant check and one inside binary-searches the x window; the lowest lane still decides. The fork's own tests: `test.exe WorldTest` (the unfiltered run stops at CompoundTest, which fails at the fork's base as well). No ABI change (14). + byte as before. Box3D skips `contactFrictionFcn` for `b3_simMeshContact` (so wet friction does not apply to `StaticHeightField` or `StaticMesh` contacts). The fork runs the hook on a recycled contact too (Box3D skips a recycled contact's update, and with it the friction mix): it mixes again and calls the hook, only when a hook is installed, so friction follows a patch settled or removed under a body at rest. The lookup is culled: `rederive_wet` indexes the live body patches by body then centre x with each body's box and largest radius (`wet_order`, `wet_body_start`, `wet_body_count`, `wet_body_box`), so a point outside the body's box is a constant check and one inside binary-searches the x window; the lowest lane still decides. The fork's own tests: `test.exe WorldTest` (the unfiltered run stops at CompoundTest, which fails at the fork's base as well). No ABI change (14). Fluid holds no constraint: `create_distances` and `attach` refuse a fluid particle, so `create_bends` (over distances) can never name one; a thing that holds liquid is cloth around free fluid (a balloon), as in Flex sec. 8. Every `create_particles` and every `destroy_particles` of particles that held no distance derives only the isolated lanes @@ -307,6 +307,8 @@ no allocation across 100 steps with fact reads. `Box3DSoaImpulseTests`: `contact `Box3DSoaCouplingTests` (also the batch file): a 256-attachment sheet attached one by one and as one batch ends with equal lanes, generations and state after 120 steps; a batch with one bad desc (a fluid particle, a removed body, `STATIC` with `ONE_WAY` or `LIMB`), more descs than free lanes, or a derivation past the tether capacity changes no lane; `ONE_WAY` leaves its box at speed 0 with a 0 impulse lane while the particle holds its point; spawn cost lines for 256 attachments and for one and four Fellas. A sphere thrown at 5 m/s with a 48 x 20 g rope from rest keeps x momentum within 0.1% and gains no energy, at 5 and 70 kg; thrown up it reaches the apex momentum allows; 5 kg snapping a taut rope at 6 m/s keeps at most 5% of its energy and 70 kg is still within a tick; a 45 degree pendulum for 12 s gains no energy and loses at most 1.25 times a heavy-particle control, and a hung body keeps its spin about the vertical; removing the body attachment or destroying the rope's particles frees a held 70 kg body in the step after, with no upward impulse; `particle_impulse` equals M dv minus M g dt every tick, hung and thrown; material friction 0.6 holds a rope on a 20 degree slope and friction 0 does not; no particle is farther from a tethered point than its rest path; an attached end stays on its body point; a world whose only body is static still steps the particles (a dropped rope falls g t^2 / 2 within 2%) and rests; and a cost line with 16 ropes x 48 and one 16 x 16 sheet at 4 substeps, one body and forty (printed, not asserted). +`Box3DSoaTerrainTests`: a bowl `StaticHeightField` catching a dropped sphere (sliding into the bowl centre with zero tunneling) and box (resting flat at bowl height), a `StaticMesh` box-prism roof drop and horizontal wall drive without penetration, `CastRayClosest` returning accurate hit point, surface normal, lane, and stable id on both `StaticHeightField` and `StaticMesh`, 10 x 20 repeated add/step/remove/dispose churn cycles with live particles (verifying `BodiesRemoved` and `End` touch facts), and clean refusal without slot consumption on invalid grid/mesh inputs (all-degenerate collinear or repeated-index triangles, out-of-bounds `>= 50000f` / `> 1e5f` vertex coordinates, overflowing `min_height = -2e38f, max_height = +2e38f` or scaled extents `>= 1e5f`, and oversized `count_x * count_z > 64M`, `triangle_count > 16M`, or `vertex_count > 32M`). + ## Not done `Box3DSoaFluidTests`: the refusals (a distance or attachment naming fluid, a viscosity below 0 or not finite; a batch of fluid past the fluid capacity, refused whole); 64 fluid particles created and destroyed a tick on the 20-pin sheet leaving every tether lane byte-equal; an 8 x 8 x 16 column in a box well (max density, mean density, nothing outside); fluid thrown into a free sheet and rope at fluid : solid mass 64 : 20, 1 : 30, 30 : 1 and 100 : 1 (momentum drift, energy on every row); a 343-particle fluid block dropped 15 cm onto a floor of immovable solid particles at 4 substeps (penetration, nothing through the floor's interior bar the one particle over a lattice hole); probe scene 3, the 343-particle pour on a tilted sheet and a sloped rope at 4 substeps, printed as readings with the same block flagged solid as the control, asserting nothing; fluid pushing a free box with the impulse lane equal to its M dv; a 3 r lattice not clumping; XSPH damping a shear field; the kernel reaching a larger solid beyond two contact radii; the pour's six checkpoint hashes (two runs, a one-ulp nudge, eight concurrent worlds). Each bound states its readings and the mutant it separates from. `Fluid_cost_line` prints 512 and 2000 fluid particles on the sheet scene at viscosity 0 and 0.01. Overflow is asserted 0 in the column, the floor and the pour. diff --git a/native/Ymir.Box3D/ymir_box3d_soa.c b/native/Ymir.Box3D/ymir_box3d_soa.c index 560d431..dbd614d 100644 --- a/native/Ymir.Box3D/ymir_box3d_soa.c +++ b/native/Ymir.Box3D/ymir_box3d_soa.c @@ -1128,7 +1128,7 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_add_static_heightfield( float min_height, float max_height, float friction, float restitution, uint64_t material_id, int32_t* index) { if (world == NULL || heights == NULL || count_x < 2 || count_z < 2 || - (int64_t)count_x * (int64_t)count_z > (int64_t)INT32_MAX / 2 || !ymir_soa_finite(origin_x) || + (int64_t)count_x * (int64_t)count_z > 64LL * 1024 * 1024 || !ymir_soa_finite(origin_x) || !ymir_soa_finite(origin_y) || !ymir_soa_finite(origin_z) || !ymir_soa_finite(scale_x) || !ymir_soa_finite(scale_y) || !ymir_soa_finite(scale_z) || scale_x <= 0.0f || scale_y <= 0.0f || scale_z <= 0.0f || !ymir_soa_finite(min_height) || !ymir_soa_finite(max_height) || @@ -1137,6 +1137,18 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_add_static_heightfield( { return YMIR_SOA_INVALID_ARGUMENT; } + float height_range = max_height - min_height; + float extent_x = scale_x * (float)(count_x - 1); + float extent_z = scale_z * (float)(count_z - 1); + float scaled_min_y = scale_y * min_height; + float scaled_max_y = scale_y * max_height; + float scaled_range_y = scale_y * height_range; + if (!ymir_soa_finite(height_range) || !ymir_soa_finite(extent_x) || !ymir_soa_finite(extent_z) || + !ymir_soa_finite(scaled_min_y) || !ymir_soa_finite(scaled_max_y) || !ymir_soa_finite(scaled_range_y) || + fabsf(extent_x) >= 1e5f || fabsf(extent_z) >= 1e5f || fabsf(scaled_range_y) >= 1e5f) + { + return YMIR_SOA_INVALID_ARGUMENT; + } int32_t height_count = count_x * count_z; for (int32_t k = 0; k < height_count; ++k) { @@ -1209,7 +1221,7 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_add_static_mesh( float friction, float restitution, uint64_t material_id, int32_t* index) { if (world == NULL || vertices_xyz == NULL || triangle_indices == NULL || vertex_count < 3 || triangle_count <= 0 || - (int64_t)vertex_count * 3 > (int64_t)INT32_MAX || (int64_t)triangle_count * 3 > (int64_t)INT32_MAX || + vertex_count > 32 * 1024 * 1024 || triangle_count > 16 * 1024 * 1024 || !ymir_soa_finite(origin_x) || !ymir_soa_finite(origin_y) || !ymir_soa_finite(origin_z) || !ymir_soa_finite(friction) || !ymir_soa_finite(restitution) || friction < 0.0f || restitution < 0.0f) { @@ -1218,7 +1230,8 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_add_static_mesh( int32_t float_count = vertex_count * 3; for (int32_t k = 0; k < float_count; ++k) { - if (!ymir_soa_finite(vertices_xyz[k])) + float v = vertices_xyz[k]; + if (!ymir_soa_finite(v) || fabsf(v) >= 50000.0f) { return YMIR_SOA_INVALID_ARGUMENT; } @@ -1231,6 +1244,40 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_add_static_mesh( return YMIR_SOA_INVALID_ARGUMENT; } } + int has_valid_triangle = 0; + for (int32_t t = 0; t < triangle_count; ++t) + { + int32_t i0 = triangle_indices[(size_t)t * 3 + 0]; + int32_t i1 = triangle_indices[(size_t)t * 3 + 1]; + int32_t i2 = triangle_indices[(size_t)t * 3 + 2]; + if (i0 == i1 || i1 == i2 || i2 == i0) + { + continue; + } + const float* p0 = &vertices_xyz[(size_t)i0 * 3]; + const float* p1 = &vertices_xyz[(size_t)i1 * 3]; + const float* p2 = &vertices_xyz[(size_t)i2 * 3]; + float e1x = p1[0] - p0[0]; + float e1y = p1[1] - p0[1]; + float e1z = p1[2] - p0[2]; + float e2x = p2[0] - p0[0]; + float e2y = p2[1] - p0[1]; + float e2z = p2[2] - p0[2]; + float cx = e1y * e2z - e1z * e2y; + float cy = e1z * e2x - e1x * e2z; + float cz = e1x * e2y - e1y * e2x; + float len_sq = cx * cx + cy * cy + cz * cz; + if (ymir_soa_finite(cx) && ymir_soa_finite(cy) && ymir_soa_finite(cz) && + ymir_soa_finite(len_sq) && len_sq > 1e-10f) + { + has_valid_triangle = 1; + break; + } + } + if (!has_valid_triangle) + { + return YMIR_SOA_INVALID_ARGUMENT; + } int32_t status = ymir_soa_check_static_slot(world, stable_id); if (status != YMIR_SOA_OK) { diff --git a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaTerrainTests.cs b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaTerrainTests.cs index 9900ecb..aa31020 100644 --- a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaTerrainTests.cs +++ b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaTerrainTests.cs @@ -237,7 +237,7 @@ public sealed class Box3DSoaTerrainTests } [Fact] - public void Invalid_heightfield_and_mesh_inputs_are_refused_cleanly() + public unsafe void Invalid_heightfield_and_mesh_inputs_are_refused_cleanly() { using var world = new Box3DSoaWorld(capacity: 2); var heights = new float[4]; @@ -253,6 +253,17 @@ public sealed class Box3DSoaTerrainTests world.AddStaticHeightField(1, (0f, 0f, 0f), [0f, float.NaN, 0f, 0f], 2, 2, (1f, 1f, 1f), -1f, 1f)); Assert.Throws(() => world.AddStaticHeightField(1, (0f, 0f, 0f), new float[3], 2, 2, (1f, 1f, 1f), -1f, 1f)); + // Height range and scaled-bound float overflow / >= 1e5f limits. + Assert.Throws(() => + world.AddStaticHeightField(1, (0f, 0f, 0f), heights, 2, 2, (1f, 1f, 1f), -2e38f, 2e38f)); + Assert.Throws(() => + world.AddStaticHeightField(1, (0f, 0f, 0f), heights, 2, 2, (1e5f, 1f, 1f), -1f, 1f)); + Assert.Throws(() => + world.AddStaticHeightField(1, (0f, 0f, 0f), heights, 2, 2, (1f, 1f, 1e5f), -1f, 1f)); + Assert.Throws(() => + world.AddStaticHeightField(1, (0f, 0f, 0f), heights, 2, 2, (1f, 1e4f, 1f), -10f, 10f)); + Assert.Throws(() => + world.AddStaticHeightField(1, (0f, 0f, 0f), heights, 2, 2, (1f, 1e20f, 1f), 1e20f, 1e20f)); Assert.Throws(() => world.AddStaticMesh(2, (0f, 0f, 0f), vertices.AsSpan(0, 2), [0, 1, 0])); @@ -262,6 +273,31 @@ public sealed class Box3DSoaTerrainTests world.AddStaticMesh(2, (0f, 0f, 0f), [Vector3.Zero, new(float.NaN, 0f, 0f), Vector3.UnitZ], [0, 1, 2])); Assert.Throws(() => world.AddStaticMesh(2, (0f, 0f, 0f), vertices, [0, 1])); + // Degenerate meshes (collinear or repeated-index triangles) and out-of-bounds vertex coordinates must be refused before b3CreateMesh. + Assert.Throws(() => + world.AddStaticMesh(2, (0f, 0f, 0f), [Vector3.Zero, Vector3.UnitX, new(2f, 0f, 0f)], [0, 1, 2])); + Assert.Throws(() => + world.AddStaticMesh(2, (0f, 0f, 0f), [Vector3.Zero, Vector3.UnitX, Vector3.UnitZ], [0, 1, 0, 1, 2, 2])); + Assert.Throws(() => + world.AddStaticMesh(2, (0f, 0f, 0f), [Vector3.Zero, new(1.5e5f, 0f, 0f), Vector3.UnitZ], [0, 1, 2])); + Assert.Throws(() => + world.AddStaticMesh(2, (0f, 0f, 0f), [Vector3.Zero, new(50000f, 0f, 0f), Vector3.UnitZ], [0, 1, 2])); + + // Native cap refusals (YMIR_SOA_INVALID_ARGUMENT == 1) without allocating 200+ MB in tests. + var nativeHandle = (nint)typeof(Box3DSoaWorld) + .GetField("_handle", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)! + .GetValue(world)!; + fixed (float* hPtr = heights) + fixed (Vector3* vPtr = vertices) + fixed (int* iPtr = indices) + { + Assert.Equal(1, Box3DSoaNative.ymir_box3d_soa_add_static_heightfield( + nativeHandle, 3, 0f, 0f, 0f, hPtr, 8192, 8193, 1f, 1f, 1f, -1f, 1f, 0.6f, 0f, 0, out _)); + Assert.Equal(1, Box3DSoaNative.ymir_box3d_soa_add_static_mesh( + nativeHandle, 4, 0f, 0f, 0f, (float*)vPtr, vertices.Length, iPtr, 16 * 1024 * 1024 + 1, 0.6f, 0f, 0, out _)); + Assert.Equal(1, Box3DSoaNative.ymir_box3d_soa_add_static_mesh( + nativeHandle, 5, 0f, 0f, 0f, (float*)vPtr, 32 * 1024 * 1024 + 1, iPtr, 1, 0.6f, 0f, 0, out _)); + } // Valid additions still fit because refusals consumed no slots. world.AddStaticHeightField(10, (0f, 0f, 0f), heights, 2, 2, (1f, 1f, 1f), -1f, 1f);