diff --git a/native/Ymir.Box3D/ymir_particles.c b/native/Ymir.Box3D/ymir_particles.c index 7cfc63e..047dca1 100644 --- a/native/Ymir.Box3D/ymir_particles.c +++ b/native/Ymir.Box3D/ymir_particles.c @@ -2870,6 +2870,39 @@ int32_t ymir_particles_settle(ymir_particles* p, int32_t n, const ymir_handle* h into = nearest; // no lane left: the nearest patch on this support takes it } } + int foreign = 0; + if (into < 0) + { + // No lane and no patch on this support: the pool of the nearest patch of this material, by world distance, grows by the drop's volume + // (its centre stays where it is: the drop's point is in another support's frame). Only when the material has no live patch at all is it FULL. + v3 at = P(p, p->pos_x, p->pos_y, p->pos_z, i); + float best = 0.0f; + for (int32_t w = 0; w < p->wet_count; ++w) + { + if (!p->wet_live[w] || p->wet_material[w] != material) + { + continue; + } + v3 c = {p->wet_x[w], p->wet_y[w], p->wet_z[w]}; + v3 world; + if (p->wet_body[w] >= 0) + { + const ymir_particle_proxy* b = p->proxies + p->wet_body[w]; + world = vadd((v3){b->position[0], b->position[1], b->position[2]}, qrot(b->rotation, c)); + } + else + { + world = vadd(P(p, p->pos_x, p->pos_y, p->pos_z, p->wet_particle[w]), c); + } + float d = vlen(vsub(at, world)); + if (into < 0 || d < best) + { + into = w; + best = d; + } + } + foreign = into >= 0; + } if (into < 0) { status = YMIR_PARTICLES_FULL; @@ -2896,6 +2929,10 @@ int32_t ymir_particles_settle(ymir_particles* p, int32_t n, const ymir_handle* h p->wet_count = into + 1; } } + else if (foreign) + { + p->wet_volume[into] += volume; + } else { float total = p->wet_volume[into] + volume; diff --git a/native/Ymir.Box3D/ymir_particles.h b/native/Ymir.Box3D/ymir_particles.h index 37982d8..ce6e958 100644 --- a/native/Ymir.Box3D/ymir_particles.h +++ b/native/Ymir.Box3D/ymir_particles.h @@ -301,8 +301,8 @@ int32_t ymir_particles_remove(ymir_particles* p, uint32_t type, int32_t n, const int32_t ymir_particles_set_pinned(ymir_particles* p, int32_t n, const ymir_handle* h, uint32_t pinned); // Settles the named fluid particles into wet patches and destroys them. Each reads its support from the last step (support_body, else // support_particle) and its contact point from the proxies the caller has just filled; a drop's volume is (2 r)^3. Refuses whole -// (INVALID_ARGUMENT) a dead or non-fluid particle or one with no support, FULL when a drop finds no lane and no patch to merge into, before any -// change. `out` (NULL or n handles) receives the patch each drop landed in. +// (INVALID_ARGUMENT) a dead or non-fluid particle or one with no support, FULL when a drop finds no lane and no live patch of its material anywhere to merge into, before any +// change. A drop that finds no lane and no patch on its support adds its volume to the nearest patch of its material in the world (the patch's centre stays). `out` (NULL or n handles) receives the patch each drop landed in. int32_t ymir_particles_settle(ymir_particles* p, int32_t n, const ymir_handle* h, uint64_t step_count, ymir_handle* out); // Creates liquid sensors in the lowest free lanes. Refuses whole (INVALID_ARGUMENT) a body outside [0, body_count), a radius that is not finite and > 0, or a // non-finite point; FULL when there are not n free lanes; nothing changes. A sensor only reads: its volume moves nothing. diff --git a/src/Ymir.Box3D/Box3DSoaWorld.cs b/src/Ymir.Box3D/Box3DSoaWorld.cs index 180cf7a..5175a1a 100644 --- a/src/Ymir.Box3D/Box3DSoaWorld.cs +++ b/src/Ymir.Box3D/Box3DSoaWorld.cs @@ -1398,9 +1398,9 @@ public sealed class Box3DSoaWorld : IDisposable /// /// Settles fluid particles into wet patches and destroys them: each drop (volume (2 r)^3) joins the lowest patch on its support whose disc - /// holds its contact point, else takes the lowest free lane, else joins the nearest patch on that support. A drop's support is its + /// holds its contact point, else takes the lowest free lane, else joins the nearest patch on that support, else adds its volume to the nearest patch of its material anywhere (that patch's centre stays). A drop's support is its /// last step's , else . Refuses the whole batch, changing - /// nothing, for a dead or non-fluid particle, one without a support, a particle named twice, or no lane and no patch to join. + /// nothing, for a dead or non-fluid particle, one without a support, a particle named twice, or no lane and no live patch of its material to join. /// (or empty) receives the patch each drop landed in. Which particles settle is the caller's policy. /// public unsafe void Settle(ReadOnlySpan particles, Span patches = default) diff --git a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaWetTests.cs b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaWetTests.cs index d93e701..971c87b 100644 --- a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaWetTests.cs +++ b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaWetTests.cs @@ -166,10 +166,15 @@ public sealed partial class Box3DSoaFluidTests Refused(floorB, flight); // no support Refused(floorB, gone[0]); // a stale handle Refused(floorB, floorB); // named twice - Refused(floorB, pedestal); // the floor drop would merge, the pedestal drop finds no lane and no patch on its support: FULL, all undone - world.Settle([floorB]); // and without the pedestal drop it merges + var centre = (world.View.WetX[0], world.View.WetY[0], world.View.WetZ[0]); + world.Settle([pedestal]); // no lane and no patch on the pedestal: it adds its volume to the floor patch, whose centre stays Assert.Equal(1, LivePatches(world.View)); Assert.Equal(2f * DropVolume, world.View.WetPatchVolume[0], 1e-9f); + Assert.Equal(centre, (world.View.WetX[0], world.View.WetY[0], world.View.WetZ[0])); + Assert.Equal(0, world.View.WetBody[0]); + world.Settle([floorB]); // and a floor drop merges as before + Assert.Equal(1, LivePatches(world.View)); + Assert.Equal(3f * DropVolume, world.View.WetPatchVolume[0], 1e-9f); } [Fact] @@ -292,14 +297,36 @@ public sealed partial class Box3DSoaFluidTests Assert.Equal(DropVolume, v.WetPatchVolume[2], 1e-9f); } + [Fact] + public void Settle_with_no_lane_and_no_patch_on_its_support_pools_into_the_nearest_patch_in_the_world() + { + // Two lanes, both taken by floor patches of material 0 at x -0.5 and x 0.9. A pedestal drop (its own support, no patch) at x 0.5 adds its volume to the nearer, lane 1, + // and moves no centre; the sum of the volumes is conserved. + using var world = FluidWorld(16, gravityY: -9.81f, wets: 2); + world.AddStaticBox(1, (0f, -0.5f, 0f), (2f, 0.5f, 2f), friction: 0.3f); + world.AddStaticBox(2, (0.5f, 0.1f, 0f), (0.1f, 0.1f, 0.1f), friction: 0.3f); + world.CreateParticles([Fluid((-0.5f, R, 0f)), Fluid((0.9f, R, 0f)), Fluid((0.5f, 0.2f + R, 0f))]); + for (var tick = 0; tick < 20; tick++) world.Step(1f / 60f, Sub); + world.Settle([Handle(world, 0), Handle(world, 1)]); + Assert.Equal(0, world.FreeWetSlots); + var v = world.View; + var before = (v.WetX[0], v.WetY[0], v.WetZ[0], v.WetX[1], v.WetY[1], v.WetZ[1]); + world.Settle([Handle(world, 2)]); + v = world.View; + Assert.Equal(DropVolume, v.WetPatchVolume[0], 1e-9f); + Assert.Equal(2f * DropVolume, v.WetPatchVolume[1], 1e-9f); + Assert.Equal(before, (v.WetX[0], v.WetY[0], v.WetZ[0], v.WetX[1], v.WetY[1], v.WetZ[1])); + Assert.Equal(2, LivePatches(v)); + } + [Fact] public void Settle_refusal_unwinds_a_patch_it_created() { - // One lane; the floor drop creates the patch, then the pedestal drop finds no lane and no patch on its support: FULL. The created patch and the count go back. + // One lane; the floor drop (material 0) creates the patch, then the pedestal drop (material 1) finds no lane and no live patch of its material anywhere: FULL. The created patch and the count go back. using var world = FluidWorld(16, gravityY: -9.81f, wets: 1); world.AddStaticBox(1, (0f, -0.5f, 0f), (2f, 0.5f, 2f), friction: 0.3f); world.AddStaticBox(2, (0.5f, 0.1f, 0f), (0.1f, 0.1f, 0.1f), friction: 0.3f); - world.CreateParticles([Fluid((-0.5f, R, 0f)), Fluid((0.5f, 0.2f + R, 0f)), Fluid((-0.45f, R, 0f))]); + world.CreateParticles([Fluid((-0.5f, R, 0f)), Fluid((0.5f, 0.2f + R, 0f), material: 1u), Fluid((-0.45f, R, 0f))]); for (var tick = 0; tick < 20; tick++) world.Step(1f / 60f, Sub); Assert.Equal(0, world.View.WetCount); var before = WorldBytes(world.View);