From 23b28cf9e1221cefc5e3433fa44910276d01de2a Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 25 Nov 2026 06:02:16 +0100 Subject: [PATCH] Wet friction: wet_mu in particle-body contact and the world's friction hook Solid particles take the lowest live patch's wet friction under the point (and their own particle_wet_mu); fluid particles keep their material mu. Body-body contact installs the fork's per-contact hook, which only lowers the mixed friction. destroy re-derives the wet lanes when a destroyed solid takes patches, so rederive_wet has three callers. Co-Authored-By: Claude Sonnet 5.5 --- native/Ymir.Box3D/ymir_box3d_soa.c | 9 +++ native/Ymir.Box3D/ymir_particles.c | 59 +++++++++++++++++-- native/Ymir.Box3D/ymir_particles.h | 4 ++ .../Ymir.Box3D.Soa.Tests/Box3DSoaWetTests.cs | 4 +- 4 files changed, 70 insertions(+), 6 deletions(-) diff --git a/native/Ymir.Box3D/ymir_box3d_soa.c b/native/Ymir.Box3D/ymir_box3d_soa.c index 65e514c..a5e72cd 100644 --- a/native/Ymir.Box3D/ymir_box3d_soa.c +++ b/native/Ymir.Box3D/ymir_box3d_soa.c @@ -555,6 +555,14 @@ static void ymir_soa_free(ymir_box3d_soa_world* world) free(world); } +// Per-contact friction: a body carries lane + 1 as user data. Patches change only between steps, so the read is race-free; the hook only lowers. +static float ymir_soa_contact_friction(void* user_a, void* user_b, b3Vec3 point_a, b3Vec3 point_b, float mixed, void* context) +{ + const ymir_particles* p = ((const ymir_box3d_soa_world*)context)->particles; + float f = ymir_particles_wet_mu(p, (int32_t)((uintptr_t)user_a) - 1, point_a.x, point_a.y, point_a.z, mixed); + return ymir_particles_wet_mu(p, (int32_t)((uintptr_t)user_b) - 1, point_b.x, point_b.y, point_b.z, f); +} + YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_create( int32_t capacity, int32_t joint_capacity, float gravity_x, float gravity_y, float gravity_z, int32_t fact_capacity, float hit_threshold, const ymir_particle_capacities* particle_capacities, @@ -644,6 +652,7 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_create( if (world->particles != NULL) { b3World_SetSubStepCallback(world->world_id, ymir_soa_particle_substep, world); + b3World_SetContactFrictionCallback(world->world_id, ymir_soa_contact_friction, world); } *output = world; diff --git a/native/Ymir.Box3D/ymir_particles.c b/native/Ymir.Box3D/ymir_particles.c index 089175b..b42670f 100644 --- a/native/Ymir.Box3D/ymir_particles.c +++ b/native/Ymir.Box3D/ymir_particles.c @@ -112,7 +112,7 @@ static size_t layout(ymir_particles* p, unsigned char* base) CARVE(nbr, int32_t, (int64_t)p->fluid_capacity * YMIR_FLUID_NEIGHBOURS); CARVE(nbr_start, int32_t, np); CARVE(nbr_count, int32_t, np); CARVE(wet_body, int32_t, nw); CARVE(wet_particle, int32_t, nw); CARVE(wet_x, float, nw); CARVE(wet_y, float, nw); CARVE(wet_z, float, nw); CARVE(wet_radius, float, nw); CARVE(wet_volume, float, nw); CARVE(wet_material, uint32_t, nw); CARVE(wet_live, uint32_t, nw); - CARVE(wet_generation, uint32_t, nw); CARVE(wet_removed_at, uint64_t, nw); CARVE(wet_volume_body, float, nx); + CARVE(wet_generation, uint32_t, nw); CARVE(wet_removed_at, uint64_t, nw); CARVE(wet_volume_body, float, nx); CARVE(particle_wet_mu, float, np); CARVE(wu_code, int32_t, nf); CARVE(wu_x, float, nf); CARVE(wu_y, float, nf); CARVE(wu_z, float, nf); CARVE(wu_volume, float, nf); CARVE(dist_a, int32_t, nd); CARVE(dist_b, int32_t, nd); CARVE(dist_rest, float, nd); CARVE(dist_compliance, float, nd); CARVE(dist_tension, float, nd); CARVE(dist_live, uint32_t, nd); CARVE(dist_generation, uint32_t, nd); @@ -185,6 +185,10 @@ ymir_particles* ymir_particles_create(const ymir_particle_capacities* c, int32_t { p->wet_body[w] = p->wet_particle[w] = -1; // a free patch lane holds -1 on both supports } + for (int32_t i = 0; i < p->particle_capacity; ++i) + { + p->particle_wet_mu[i] = INFINITY; // dry + } // n0 for h = 1: the Poly6 kernel summed over the simple-cubic lattice of spacing 1/2 within reach (self included); at h it is k / h^3 float k = 0.0f; for (int a = -2; a <= 2; ++a) @@ -348,9 +352,14 @@ static void kill_wet(ymir_particles* p, int32_t w, uint64_t step_count) p->wet_generation[w] += 1; } -// wet_volume_body[b] = the sum of the live patches on body b, in wet lane order. The one writer; settle and remove(WET) call it. +// wet_volume_body[b] = the sum of the live patches on body b, in wet lane order; particle_wet_mu[i] = the wet friction of the lowest live +// patch on solid particle i, +inf when dry. The one writer; settle, remove(WET) and destroy call it. static void rederive_wet(ymir_particles* p) { + for (int32_t i = 0; i < p->particle_capacity; ++i) + { + p->particle_wet_mu[i] = INFINITY; + } for (int32_t b = 0; b < p->body_capacity; ++b) { p->wet_volume_body[b] = 0.0f; @@ -361,6 +370,11 @@ static void rederive_wet(ymir_particles* p) { p->wet_volume_body[p->wet_body[w]] += p->wet_volume[w]; } + if (p->wet_live[w] && p->wet_particle[w] >= 0 && p->wet_particle[w] < p->particle_capacity && + p->particle_wet_mu[p->wet_particle[w]] == INFINITY) + { + p->particle_wet_mu[p->wet_particle[w]] = p->mat_wet_friction[p->wet_material[w]]; + } } } @@ -908,13 +922,19 @@ int32_t ymir_particles_destroy_particles(ymir_particles* p, int32_t n, const ymi p->support_particle[i] = -1; // a drop does not keep resting on a solid destroyed under it until the next step } } + int wet_killed = 0; for (int32_t w = 0; w < p->wet_count; ++w) { if (p->wet_live[w] && p->wet_particle[w] >= 0 && !p->particle_live[p->wet_particle[w]]) { kill_wet(p, w, step_count); // the liquid on a destroyed solid goes with it; a particle patch is on no body, so wet_volume_body does not change + wet_killed = 1; } } + if (wet_killed) + { + rederive_wet(p); // the dry lane reads +inf again + } return rederive_topology(p, held ? TOPO_FULL : TOPO_ISOLATED, p->queue, killed); } @@ -1778,6 +1798,29 @@ static float proxy_distance(const ymir_particle_proxy* b, v3 x, v3* normal, v3* return d; } +// The friction on body `body` at body-frame point (x, y, z): the wet friction of the lowest live patch whose disc holds the point, never above mu. +// The one patch test, for particle-body contact here and body-body contact through the world's friction hook. +float ymir_particles_wet_mu(const ymir_particles* p, int32_t body, float x, float y, float z, float mu) +{ + if (p == NULL || body < 0 || body >= p->body_capacity || p->wet_volume_body[body] == 0.0f) + { + return mu; + } + for (int32_t w = 0; w < p->wet_count; ++w) + { + if (!p->wet_live[w] || p->wet_body[w] != body) + { + continue; + } + v3 d = {x - p->wet_x[w], y - p->wet_y[w], z - p->wet_z[w]}; + if (vlen(d) <= p->wet_radius[w]) + { + return vmin(mu, p->mat_wet_friction[p->wet_material[w]]); + } + } + return mu; +} + static float proxy_reach(const ymir_particle_proxy* b) { if (b->shape == YMIR_PROXY_SPHERE) @@ -1816,6 +1859,7 @@ static void body_contacts(phase* f) { continue; } + int fluid = (p->phase[i] & YMIR_PHASE_FLUID) != 0; float mu = p->mat_friction[p->material[i]]; float r = p->radius[i]; float wi = p->inv_mass[i]; @@ -1853,7 +1897,14 @@ static void body_contacts(phase* f) float w = wi + wb; x = vadd(x, vmul(n, depth * wi / w)); proxy_push(f, b, s, vmul(n, -depth / w)); - if (mu > 0.0f) + float mu_k = mu; + if (!fluid) + { + // A solid on a wet body or wetted itself slips: the lowest wet friction there; a fluid particle keeps its own. + v3 l = qrot_inv(b->rotation, vsub(s, c)); + mu_k = vmin(ymir_particles_wet_mu(p, k, l.x, l.y, l.z, mu), p->particle_wet_mu[i]); + } + if (mu_k > 0.0f) { // Slip of the particle against the body point over this substep, tangential part. v3 rb = vsub(s, (v3){b->position[0], b->position[1], b->position[2]}); @@ -1865,7 +1916,7 @@ static void body_contacts(phase* f) if (sl > 1e-9f) { v3 t = vmul(slip, 1.0f / sl); - float move = vmin(sl, mu * depth); + float move = vmin(sl, mu_k * depth); float wt = wi + proxy_w(b, s, t); x = vsub(x, vmul(t, move * wi / wt)); proxy_push(f, b, s, vmul(t, move / wt)); diff --git a/native/Ymir.Box3D/ymir_particles.h b/native/Ymir.Box3D/ymir_particles.h index b70ac69..a2e09ba 100644 --- a/native/Ymir.Box3D/ymir_particles.h +++ b/native/Ymir.Box3D/ymir_particles.h @@ -185,6 +185,7 @@ typedef struct ymir_particles float *wet_x, *wet_y, *wet_z, *wet_radius, *wet_volume; // wet_volume m^3; wet_radius = sqrt(volume / (pi YMIR_WET_FILM)) uint32_t *wet_material, *wet_live, *wet_generation; uint64_t* wet_removed_at; + float* particle_wet_mu; // derived, per particle lane: the wet friction of the lowest live patch on that solid particle, +inf when dry (rederive_wet) float* wet_volume_body; // derived, per body lane: the sum of the live patches on it in wet lane order (rederive_wet, at command time only) int32_t* wu_code; // settle's undo log, one entry per distinct drop: a patch lane (merged: its old lanes below) or -(lane + 1) (created) float *wu_x, *wu_y, *wu_z, *wu_volume; @@ -238,6 +239,9 @@ typedef struct ymir_particles ymir_particles* ymir_particles_create(const ymir_particle_capacities* capacities, int32_t body_capacity); void ymir_particles_free(ymir_particles* p); +// The friction on body lane `body` at body-frame point (x, y, z): the wet friction of the lowest live patch whose disc holds it, never above mu. +float ymir_particles_wet_mu(const ymir_particles* p, int32_t body, float x, float y, float z, float mu); + // Commands. `step_count` is the world's completed steps: a removed lane is reused once a step has passed, lowest // lane first. Every refusal happens before anything changes; a command that would need more tethers than the // capacity is refused whole (tether capacity: up to two per particle reached, plus the pair routes). Every topology command ends in the one rederive_topology. diff --git a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaWetTests.cs b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaWetTests.cs index ca5db09..848439a 100644 --- a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaWetTests.cs +++ b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaWetTests.cs @@ -335,7 +335,7 @@ public sealed partial class Box3DSoaFluidTests public void Remove_wet_never_reaches_the_topology_derivation() { // Rederiving an unchanged graph writes the same bytes, so no lane tells the call from its absence; the rule is pinned on the source (the authority map: remove(WET) - // ends in rederive_wet, never in rederive_topology; rederive_wet's callers are settle and remove(WET)). + // ends in rederive_wet, never in rederive_topology; rederive_wet's callers are settle, remove(WET) and destroy). var source = NativeSource("ymir_particles.c").Replace("\r\n", "\n"); var at = source.IndexOf("if (type == YMIR_LANES_WET)", StringComparison.Ordinal); Assert.True(at > 0); @@ -344,7 +344,7 @@ public sealed partial class Box3DSoaFluidTests Assert.Contains("rederive_wet(p);", branch); Assert.DoesNotContain("rederive_topology", branch); Assert.DoesNotContain("TOPO_", branch); - Assert.Equal(2, System.Text.RegularExpressions.Regex.Matches(source, @"[^_]rederive_wet\(p\);").Count); // settle and remove(WET) + Assert.Equal(3, System.Text.RegularExpressions.Regex.Matches(source, @"[^_]rederive_wet\(p\);").Count); // settle, remove(WET), and destroy when a destroyed solid takes its patches (particle_wet_mu goes dry again) } // ---- determinism ----------------------------------------------------------------------------------------