From e1f1449927f45fc2f5b282973c8c30aad4e6dc38 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 25 Nov 2026 21:24:11 +0100 Subject: [PATCH] Ymir SoA: remove_body, refused while named Body kind REMOVED, body_generation/body_removed_at, body slot reuse (twin of the joint slot), free_body_slots, remove_body(lane, generation): refused whole while a live attachment or sensor names the body; else wet patches go (ymir_particles_release_body, no Box3D call), joints naming it retire, Box3D destroys it, the lane tombstones. Every per-body loop skips REMOVED; attach, sensors, add_joint and parent refuse a removed lane. Box3DSoaBodyHandle returned by every Add* body helper. A removed body's shape ends a touch only by skipping it here; the next commit names it. Co-Authored-By: Claude Sonnet 5.5 --- native/Ymir.Box3D/ymir_box3d_soa.c | 152 +++++++++- native/Ymir.Box3D/ymir_particles.c | 46 +++ native/Ymir.Box3D/ymir_particles.h | 7 +- src/Ymir.Box3D/Box3DSoaWorld.cs | 59 +++- .../Box3DSoaImpulseTests.cs | 2 +- .../Box3DSoaRemoveTests.cs | 282 ++++++++++++++++++ .../Box3DSoaWetFrictionTests.cs | 2 +- .../Ymir.Box3D.Soa.Tests/Box3DSoaWetTests.cs | 4 +- 8 files changed, 526 insertions(+), 28 deletions(-) create mode 100644 tests/Ymir.Box3D.Soa.Tests/Box3DSoaRemoveTests.cs diff --git a/native/Ymir.Box3D/ymir_box3d_soa.c b/native/Ymir.Box3D/ymir_box3d_soa.c index 72e869e..140fd42 100644 --- a/native/Ymir.Box3D/ymir_box3d_soa.c +++ b/native/Ymir.Box3D/ymir_box3d_soa.c @@ -38,7 +38,8 @@ enum YMIR_SOA_DUPLICATE_ID = 2, YMIR_SOA_FULL = 3, YMIR_SOA_OUT_OF_MEMORY = 4, - YMIR_SOA_INTERNAL_ERROR = 5 + YMIR_SOA_INTERNAL_ERROR = 5, + YMIR_SOA_REFERENCED = 6 // remove_body refused: a live attachment or sensor names the body (remove those first) }; enum @@ -47,7 +48,8 @@ enum YMIR_SOA_KIND_STATIC_BOX = 1, YMIR_SOA_KIND_CAPSULE = 2, YMIR_SOA_KIND_BOX = 3, - YMIR_SOA_KIND_ANCHOR = 4 + YMIR_SOA_KIND_ANCHOR = 4, + YMIR_SOA_KIND_REMOVED = 5 // a removed body's lane (remove_body): never accepted by add_body }; enum @@ -210,6 +212,9 @@ typedef struct ymir_box3d_soa_view const float* contact_impulse; const int32_t* contact_count; const uint64_t* material_id; + // Per body lane: counts the removals of that lane. remove_body refuses a handle whose generation is not the lane's, so a stale + // removal never takes a later body in a reused lane. A removed body's lane reads kind REMOVED and every other lane 0 (stable_id 0, parent -1). + const uint32_t* body_generation; // Per joint slot: counts the destroys of that slot. A grab's handle is (lane, generation); destroy_joint refuses // a handle whose generation is not the slot's, so a stale release cannot destroy a later grab in a reused slot. const uint32_t* joint_generation; @@ -416,6 +421,8 @@ typedef struct ymir_box3d_soa_world int32_t* limb; int32_t* contact_count; uint64_t* material_id; + uint32_t* body_generation; + uint64_t* body_removed_at; // step_count when the lane was removed; reusable once a step has passed float* size; // 3 arrays of `capacity` floats, lane-major; written once in add_body float* lanes; // YMIR_SOA_BODY_FLOAT_LANES arrays of `capacity` floats, lane-major b3JointId* joints; @@ -479,7 +486,7 @@ static int ymir_soa_is_anchor(uint32_t kind) static int ymir_soa_is_dynamic(uint32_t kind) { - return kind != YMIR_SOA_KIND_STATIC_BOX && !ymir_soa_is_anchor(kind); + return kind == YMIR_SOA_KIND_SPHERE || kind == YMIR_SOA_KIND_CAPSULE || kind == YMIR_SOA_KIND_BOX; } static void ymir_soa_refresh(ymir_box3d_soa_world* world, int index) @@ -590,6 +597,8 @@ static void ymir_soa_free(ymir_box3d_soa_world* world) free(world->limb); free(world->contact_count); free(world->material_id); + free(world->body_generation); + free(world->body_removed_at); free(world->size); free(world->lanes); free(world->joints); @@ -654,6 +663,8 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_create( world->limb = (int32_t*)calloc(n, sizeof(int32_t)); world->contact_count = (int32_t*)calloc(n, sizeof(int32_t)); world->material_id = (uint64_t*)calloc(n, sizeof(uint64_t)); + world->body_generation = (uint32_t*)calloc(n, sizeof(uint32_t)); + world->body_removed_at = (uint64_t*)calloc(n, sizeof(uint64_t)); world->size = (float*)calloc(n * 3, sizeof(float)); world->lanes = (float*)calloc(n * YMIR_SOA_BODY_FLOAT_LANES, sizeof(float)); world->joints = (b3JointId*)calloc(m, sizeof(b3JointId)); @@ -683,7 +694,7 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_create( return YMIR_SOA_OUT_OF_MEMORY; } } - if (world->contact_count == NULL || world->material_id == NULL || world->begin_facts == NULL || + if (world->contact_count == NULL || world->material_id == NULL || world->body_generation == NULL || world->body_removed_at == NULL || world->begin_facts == NULL || world->end_facts == NULL || world->hit_facts == NULL || world->overload_facts == NULL || world->contact_scratch == NULL || world->bodies == NULL || world->stable_id == NULL || world->kind == NULL || world->parent == NULL || world->limb == NULL || world->size == NULL || world->lanes == NULL || world->joints == NULL || world->joint_kind == NULL || @@ -729,6 +740,35 @@ YMIR_EXPORT void YMIR_CALL ymir_box3d_soa_destroy(ymir_box3d_soa_world* world) ymir_soa_free(world); } +// The lane a new body takes: the lowest removed lane whose removing step has completed, else the next fresh lane, else -1. A removed +// lane is not reused within the step that removed it, so lanes a caller read that step stay meaningful. +static int ymir_soa_body_slot(const ymir_box3d_soa_world* world) +{ + for (int i = 0; i < world->count; ++i) + { + if (world->kind[i] == YMIR_SOA_KIND_REMOVED && world->step_count > world->body_removed_at[i]) + { + return i; + } + } + return world->count < world->capacity ? world->count : -1; +} + +// Bodies that add_body could create right now. +YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_free_body_slots(const ymir_box3d_soa_world* world) +{ + if (world == NULL) + { + return 0; + } + int32_t free_slots = world->capacity - world->count; + for (int i = 0; i < world->count; ++i) + { + free_slots += world->kind[i] == YMIR_SOA_KIND_REMOVED && world->step_count > world->body_removed_at[i]; + } + return free_slots; +} + static int32_t ymir_soa_check_body(const ymir_box3d_soa_world* world, const ymir_box3d_soa_body_desc* d) { if (d->kind > YMIR_SOA_KIND_ANCHOR || !ymir_soa_finite3(d->position) || !ymir_soa_finite3(d->velocity) || @@ -751,7 +791,7 @@ static int32_t ymir_soa_check_body(const ymir_box3d_soa_world* world, const ymir { return YMIR_SOA_INVALID_ARGUMENT; } - if (d->parent < -1 || d->parent >= world->count) + if (d->parent < -1 || d->parent >= world->count || (d->parent >= 0 && world->kind[d->parent] == YMIR_SOA_KIND_REMOVED)) { return YMIR_SOA_INVALID_ARGUMENT; } @@ -782,13 +822,13 @@ static int32_t ymir_soa_check_body(const ymir_box3d_soa_world* world, const ymir } break; } - if (world->count >= world->capacity) + if (ymir_soa_body_slot(world) < 0) { return YMIR_SOA_FULL; } for (int i = 0; i < world->count; ++i) { - if (world->stable_id[i] == d->stable_id) + if (world->kind[i] != YMIR_SOA_KIND_REMOVED && world->stable_id[i] == d->stable_id) { return YMIR_SOA_DUPLICATE_ID; } @@ -810,6 +850,7 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_add_body( return status; } + int slot = ymir_soa_body_slot(world); b3BodyDef body_def = b3DefaultBodyDef(); body_def.type = ymir_soa_is_anchor(d->kind) ? b3_kinematicBody : ymir_soa_is_dynamic(d->kind) ? b3_dynamicBody @@ -818,7 +859,7 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_add_body( body_def.rotation = (b3Quat){{d->rotation[0], d->rotation[1], d->rotation[2]}, d->rotation[3]}; body_def.linearVelocity = (b3Vec3){d->velocity[0], d->velocity[1], d->velocity[2]}; body_def.enableSleep = false; - body_def.userData = (void*)((uintptr_t)world->count + 1u); // lane + 1: a shape resolves to its lane + body_def.userData = (void*)((uintptr_t)slot + 1u); // lane + 1: a shape resolves to its lane b3BodyId body = b3CreateBody(world->world_id, &body_def); if (B3_IS_NULL(body)) { @@ -862,12 +903,17 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_add_body( } } - int i = world->count++; + int i = slot; + if (i == world->count) + { + world->count++; + } world->bodies[i] = body; world->stable_id[i] = d->stable_id; world->kind[i] = d->kind; world->parent[i] = d->parent; world->limb[i] = d->limb; + world->contact_count[i] = 0; world->material_id[i] = ymir_soa_is_anchor(d->kind) ? 0 : d->material_id; /* Publish only the components the kind uses; unused ones read 0 whatever the caller passed. */ int used = ymir_soa_is_anchor(d->kind) ? 0 : d->kind == YMIR_SOA_KIND_SPHERE ? 1 : d->kind == YMIR_SOA_KIND_CAPSULE ? 2 : 3; @@ -929,7 +975,8 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_add_joint( ymir_box3d_soa_world* world, const ymir_box3d_soa_joint_desc* d, int32_t* index) { if (world == NULL || d == NULL || d->kind > YMIR_SOA_JOINT_MOTOR || d->body_a < 0 || d->body_a >= world->count || - d->body_b < 0 || d->body_b >= world->count || d->body_a == d->body_b || !ymir_soa_finite3(d->anchor) || + d->body_b < 0 || d->body_b >= world->count || d->body_a == d->body_b || + world->kind[d->body_a] == YMIR_SOA_KIND_REMOVED || world->kind[d->body_b] == YMIR_SOA_KIND_REMOVED || !ymir_soa_finite3(d->anchor) || !ymir_soa_finite3(d->axis) || !ymir_soa_finite(d->lower) || !ymir_soa_finite(d->upper) || !ymir_soa_finite(d->cone_angle) || d->cone_angle < 0.0f || d->cone_angle > B3_PI || !ymir_soa_finite(d->linear_hertz) || !ymir_soa_finite(d->linear_damping) || !ymir_soa_finite(d->max_force) || @@ -1076,6 +1123,53 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_set_joint_thresholds( return YMIR_SOA_OK; } +// Removes a body named by its lane and the lane's generation read when it was added: the one body-ending path. Refused, nothing +// changed, for a lane that is not a live body, a stale generation, or REFERENCED while a live attachment or sensor names the body. +// Then: its wet patches go, every joint naming it is retired (ascending lane), Box3D destroys the body, and the lane becomes a +// tombstone (kind REMOVED, every other lane 0, generation + 1) that add_body may reuse once a step has completed. +YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_remove_body(ymir_box3d_soa_world* world, int32_t lane, uint32_t generation) +{ + if (world == NULL || lane < 0 || lane >= world->count || world->kind[lane] == YMIR_SOA_KIND_REMOVED || + world->body_generation[lane] != generation) + { + return YMIR_SOA_INVALID_ARGUMENT; + } + if (world->particles != NULL) + { + int32_t status = ymir_particles_release_body(world->particles, lane, world->step_count, NULL, NULL); + if (status != YMIR_PARTICLES_OK) + { + return status; + } + } + for (int j = 0; j < world->joint_count; ++j) + { + if (world->joint_kind[j] != YMIR_SOA_JOINT_REMOVED && (world->joint_body_a[j] == lane || world->joint_body_b[j] == lane)) + { + ymir_soa_retire_joint(world, j); + } + } + b3DestroyBody(world->bodies[lane]); + world->bodies[lane] = (b3BodyId){0}; + world->kind[lane] = YMIR_SOA_KIND_REMOVED; + world->stable_id[lane] = 0; + world->parent[lane] = -1; + world->limb[lane] = 0; + world->contact_count[lane] = 0; + world->material_id[lane] = 0; + world->body_removed_at[lane] = world->step_count; + world->body_generation[lane] += 1; + for (int k = 0; k < 3; ++k) + { + world->size[(size_t)k * (size_t)world->capacity + (size_t)lane] = 0.0f; + } + for (int k = 0; k < YMIR_SOA_BODY_FLOAT_LANES; ++k) + { + ymir_soa_lane(world, k)[lane] = 0.0f; + } + return YMIR_SOA_OK; +} + // Applies the caller's force and torque lanes to the dynamic bodies, then clears them: a lane holds one // step's intent, so a stale force is never applied twice. A non-finite value refuses the whole step // before any body is touched. @@ -1087,7 +1181,7 @@ static int32_t ymir_soa_apply_write_lanes(ymir_box3d_soa_world* world) lane[k] = ymir_soa_lane(world, YMIR_SOA_STATE_LANES + k); for (int i = 0; i < world->count; ++i) { - if (!ymir_soa_finite(lane[k][i])) + if (world->kind[i] != YMIR_SOA_KIND_REMOVED && !ymir_soa_finite(lane[k][i])) { return YMIR_SOA_INVALID_ARGUMENT; } @@ -1199,6 +1293,10 @@ static void ymir_soa_collect_facts(ymir_box3d_soa_world* world) for (int i = 0; i < events.endCount; ++i) { const b3ContactEndTouchEvent* e = events.endEvents + i; + if (!b3Shape_IsValid(e->shapeIdA) || !b3Shape_IsValid(e->shapeIdB)) + { + continue; // a removed body's shape: attributed by the retired table + } ymir_soa_touch_fact(world, world->end_facts, &f->end_count, &f->end_dropped, e->shapeIdA, e->shapeIdB); } for (int i = 0; i < events.hitCount; ++i) @@ -1230,6 +1328,10 @@ static void ymir_soa_collect_contact_lanes(ymir_box3d_soa_world* world) float* lane = ymir_soa_lane(world, YMIR_SOA_CONTACT_IMPULSE_LANE); for (int i = 0; i < world->count; ++i) { + if (world->kind[i] == YMIR_SOA_KIND_REMOVED) + { + continue; // its lanes stay 0 + } int n = b3Body_GetContactData(world->bodies[i], world->contact_scratch, world->capacity); float impulse = 0.0f; for (int c = 0; c < n; ++c) @@ -1253,6 +1355,12 @@ static void ymir_soa_gather_proxies(ymir_box3d_soa_world* world) { ymir_particle_proxy* b = pt->proxies + i; uint32_t kind = world->kind[i]; + if (kind == YMIR_SOA_KIND_REMOVED) + { + memset(b, 0, sizeof(*b)); + b->shape = YMIR_PROXY_NONE; // no shape: settle refuses it as a support, particles never touch it + continue; + } b3BodyId body = world->bodies[i]; b3Pos p = b3Body_GetPosition(body); b3Quat q = b3Body_GetRotation(body); @@ -1300,6 +1408,10 @@ static void ymir_soa_particle_substep(b3WorldId world_id, int sub_step, float h, b3Pos c; b3Quat q; b3Vec3 v, w; + if (world->kind[i] == YMIR_SOA_KIND_REMOVED) + { + continue; // its proxy stays zero + } if (!b3Body_GetSubStepState(world->bodies[i], &c, &q, &v, &w)) { b->inv_mass = 0.0f; @@ -1371,7 +1483,10 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_step(ymir_box3d_soa_world* world, f ymir_soa_collect_contact_lanes(world); for (int i = 0; i < world->count; ++i) { - ymir_soa_refresh(world, i); + if (world->kind[i] != YMIR_SOA_KIND_REMOVED) + { + ymir_soa_refresh(world, i); + } } for (int j = 0; j < world->joint_count; ++j) { @@ -1532,6 +1647,11 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_attach( { return YMIR_SOA_INVALID_ARGUMENT; } + if (d != NULL && d->kind == YMIR_ATTACH_BODY && d->body >= 0 && d->body < world->count && + world->kind[d->body] == YMIR_SOA_KIND_REMOVED) + { + return YMIR_SOA_INVALID_ARGUMENT; + } return ymir_particles_attach(world->particles, d, world->count, world->step_count, out); } @@ -1571,6 +1691,13 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_create_sensors(ymir_box3d_soa_world { return YMIR_SOA_INVALID_ARGUMENT; } + for (int32_t k = 0; d != NULL && k < n; ++k) + { + if (d[k].body >= 0 && d[k].body < world->count && world->kind[d[k].body] == YMIR_SOA_KIND_REMOVED) + { + return YMIR_SOA_INVALID_ARGUMENT; + } + } return ymir_particles_create_sensors(world->particles, n, d, world->count, world->step_count, out); } @@ -1665,6 +1792,7 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_get_view(const ymir_box3d_soa_world .contact_impulse = base + YMIR_SOA_CONTACT_IMPULSE_LANE * n, .contact_count = world->contact_count, .material_id = world->material_id, + .body_generation = world->body_generation, .joint_generation = world->joint_generation, .particle_impulse_x = base + (YMIR_SOA_PARTICLE_IMPULSE_LANE + 0) * n, .particle_impulse_y = base + (YMIR_SOA_PARTICLE_IMPULSE_LANE + 1) * n, diff --git a/native/Ymir.Box3D/ymir_particles.c b/native/Ymir.Box3D/ymir_particles.c index 6109a4a..ee1c972 100644 --- a/native/Ymir.Box3D/ymir_particles.c +++ b/native/Ymir.Box3D/ymir_particles.c @@ -1238,6 +1238,52 @@ int32_t ymir_particles_remove(ymir_particles* p, uint32_t type, int32_t n, const return rederive_topology(p, TOPO_FULL, NULL, 0); } +int32_t ymir_particles_release_body(ymir_particles* p, int32_t body, uint64_t step_count, uint32_t* patches, float* volume) +{ + if (p == NULL || body < 0 || body >= p->body_capacity) + { + return YMIR_PARTICLES_INVALID_ARGUMENT; + } + for (int32_t a = 0; a < p->attachment_count; ++a) + { + if (p->att_kind[a] == YMIR_ATTACH_BODY && p->att_body[a] == body) + { + return YMIR_PARTICLES_REFERENCED; + } + } + for (int32_t s = 0; s < p->sensor_count; ++s) + { + if (p->sensor_live[s] && p->sensor_body[s] == body) + { + return YMIR_PARTICLES_REFERENCED; + } + } + uint32_t count = 0; + float total = 0.0f; + for (int32_t w = 0; w < p->wet_count; ++w) + { + if (p->wet_live[w] && p->wet_body[w] == body) + { + count += 1; + total += p->wet_volume[w]; + kill_wet(p, w, step_count); + } + } + if (count > 0) + { + rederive_wet(p); + } + if (patches != NULL) + { + *patches = count; + } + if (volume != NULL) + { + *volume = total; + } + return YMIR_PARTICLES_OK; +} + int32_t ymir_particles_set_material(ymir_particles* p, int32_t index, const ymir_material_desc* d) { if (p == NULL || d == NULL || index < 0 || index >= p->material_capacity || !isfinite(d->friction) || d->friction < 0.0f || diff --git a/native/Ymir.Box3D/ymir_particles.h b/native/Ymir.Box3D/ymir_particles.h index ffc199f..e3da207 100644 --- a/native/Ymir.Box3D/ymir_particles.h +++ b/native/Ymir.Box3D/ymir_particles.h @@ -29,7 +29,8 @@ enum YMIR_PARTICLES_INVALID_ARGUMENT = 1, YMIR_PARTICLES_DUPLICATE_ID = 2, YMIR_PARTICLES_FULL = 3, - YMIR_PARTICLES_OUT_OF_MEMORY = 4 + YMIR_PARTICLES_OUT_OF_MEMORY = 4, + YMIR_PARTICLES_REFERENCED = 6 // a body that a live attachment or sensor names cannot be released }; // Phase word: bits 0-19 are the group; the four flags below are the only other bits. @@ -283,6 +284,10 @@ int32_t ymir_particles_settle(ymir_particles* p, int32_t n, const ymir_handle* h // 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. int32_t ymir_particles_create_sensors(ymir_particles* p, int32_t n, const ymir_sensor_desc* d, int32_t body_count, uint64_t step_count, ymir_handle* out); +// Frees what the particle layer holds on body lane `body` so the body can be removed: REFERENCED, changing nothing, while a live body +// attachment or a live sensor names it (the caller removes those first); otherwise every live wet patch on it is removed (one rederive_wet) and +// `patches` and `volume` (either may be NULL) receive their count and summed volume (m^3). No rigid-body call. +int32_t ymir_particles_release_body(ymir_particles* p, int32_t body, uint64_t step_count, uint32_t* patches, float* volume); int32_t ymir_particles_set_material(ymir_particles* p, int32_t index, const ymir_material_desc* d); int32_t ymir_particles_free_slots(const ymir_particles* p, uint32_t lane_set, uint64_t step_count); diff --git a/src/Ymir.Box3D/Box3DSoaWorld.cs b/src/Ymir.Box3D/Box3DSoaWorld.cs index 048a91d..1342126 100644 --- a/src/Ymir.Box3D/Box3DSoaWorld.cs +++ b/src/Ymir.Box3D/Box3DSoaWorld.cs @@ -10,7 +10,9 @@ public enum Box3DSoaKind : uint Capsule = 2, Box = 3, /// A kinematic body with no shape: it never collides or is hit, and only moves it. - Anchor = 4 + Anchor = 4, + /// The kind lane of a removed body (a tombstone, from RemoveBody): every other lane reads 0 and the parent -1. + Removed = 5 } public enum Box3DSoaJointKind : uint @@ -223,6 +225,15 @@ public readonly record struct Box3DSoaJointHandle(int Lane, uint Generation) public static implicit operator int(Box3DSoaJointHandle handle) => handle.Lane; } +/// +/// A body's identity: its lane and the lane's generation when it was added. Converts to the lane for reading lanes and for every +/// parameter that names a body; takes the whole handle and refuses one from an earlier occupant of the lane. +/// +public readonly record struct Box3DSoaBodyHandle(int Lane, uint Generation) +{ + public static implicit operator int(Box3DSoaBodyHandle handle) => handle.Lane; +} + /// The closest hit of a ray. false is a miss and every other field is then default. public readonly record struct Box3DSoaRayHit( bool Hit, int Lane, ulong StableId, (float X, float Y, float Z) Point, (float X, float Y, float Z) Normal, float Fraction); @@ -305,6 +316,7 @@ internal unsafe struct Box3DSoaViewNative public float* ContactImpulse; public int* ContactCount; public ulong* MaterialId; + public uint* BodyGeneration; public uint* JointGeneration; public float* ParticleImpulseX; public float* ParticleImpulseY; @@ -555,6 +567,14 @@ internal static unsafe partial class Box3DSoaNative [UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])] internal static partial int ymir_box3d_soa_destroy_joint(nint world, int joint, uint generation); + [LibraryImport(Library)] + [UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])] + internal static partial int ymir_box3d_soa_remove_body(nint world, int lane, uint generation); + + [LibraryImport(Library)] + [UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])] + internal static partial int ymir_box3d_soa_free_body_slots(nint world); + [LibraryImport(Library)] [UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])] internal static partial int ymir_box3d_soa_set_joint_thresholds(nint world, int joint, uint generation, float forceThreshold, float torqueThreshold); @@ -697,6 +717,10 @@ public readonly unsafe struct Box3DSoaView public ReadOnlySpan ContactCount => Lane(_v.ContactCount, Count); /// The material id the body was added with (0 by default). public ReadOnlySpan MaterialId => Lane(_v.MaterialId, Count); + /// Per body lane, the number of times it has been removed. A body's handle is its lane and the generation read when it was added; + /// refuses a handle from an earlier occupant of the lane. is the high-water mark of lanes + /// ever used: a lane with kind is free (reused once a step has completed). + public ReadOnlySpan BodyGeneration => Lane(_v.BodyGeneration, Count); /// Per joint slot, the number of times it has been destroyed. A joint's handle is its lane and the generation read /// when it was added; refuses a handle from an earlier occupant of the slot. @@ -963,7 +987,7 @@ public sealed class Box3DSoaWorld : IDisposable } /// Adds a dynamic sphere; returns its lane index. - public int AddSphere( + public Box3DSoaBodyHandle AddSphere( ulong stableId, (float X, float Y, float Z) position, float radius, float density, (float X, float Y, float Z) velocity = default, float friction = 0.6f, float restitution = 0f, int parent = -1, int limb = 0, float rollingResistance = 0f, ulong materialId = 0) => @@ -971,7 +995,7 @@ public sealed class Box3DSoaWorld : IDisposable velocity, friction, restitution, parent, limb, rollingResistance, materialId)); /// Adds a static axis-aligned box centred at . - public int AddStaticBox( + public Box3DSoaBodyHandle AddStaticBox( ulong stableId, (float X, float Y, float Z) position, (float X, float Y, float Z) halfExtents, float friction = 0.6f, float restitution = 0f, ulong materialId = 0) => AddBody(Describe(Box3DSoaKind.StaticBox, stableId, position, default, halfExtents, 0f, @@ -982,7 +1006,7 @@ public sealed class Box3DSoaWorld : IDisposable /// the body's local Y, swept by . is a unit quaternion /// x y z w; the default (all zero) means identity. /// - public int AddCapsule( + public Box3DSoaBodyHandle AddCapsule( ulong stableId, (float X, float Y, float Z) position, float radius, float halfLength, float density, (float X, float Y, float Z, float W) rotation = default, float friction = 0.6f, float restitution = 0f, int parent = -1, int limb = 0, float rollingResistance = 0f, ulong materialId = 0) => @@ -990,7 +1014,7 @@ public sealed class Box3DSoaWorld : IDisposable default, friction, restitution, parent, limb, rollingResistance, materialId)); /// Adds a dynamic box with the given half extents; rotation as for . - public int AddBox( + public Box3DSoaBodyHandle AddBox( ulong stableId, (float X, float Y, float Z) position, (float X, float Y, float Z) halfExtents, float density, (float X, float Y, float Z, float W) rotation = default, float friction = 0.6f, float restitution = 0f, int parent = -1, int limb = 0, ulong materialId = 0) => @@ -1087,18 +1111,30 @@ public sealed class Box3DSoaWorld : IDisposable } } - /// Bodies that can still be added. Bodies are never removed here; keep one anchor per cursor and reuse it. + /// Bodies AddSphere and its siblings could create right now: unused capacity plus lanes removed in an earlier step. public int FreeBodySlots { get { - var v = View; - return v.Capacity - v.Count; + ObjectDisposedException.ThrowIf(_handle == 0, this); + return Box3DSoaNative.ymir_box3d_soa_free_body_slots(_handle); } } + /// + /// Removes a body: its wet patches go, every joint naming it is retired (their handles go stale), and its lane becomes a tombstone + /// (kind , every other lane 0, generation + 1) that a later Add* reuses once a has completed. + /// A touch it was in ends by name in the next step's facts. Throws, changing nothing, for a lane that is not a live body, a stale handle, or + /// while a live attachment or liquid sensor names the body (remove those first). Removing an anchor ends its grabs the same way. + /// + public void RemoveBody(Box3DSoaBodyHandle body) + { + ObjectDisposedException.ThrowIf(_handle == 0, this); + Check(Box3DSoaNative.ymir_box3d_soa_remove_body(_handle, body.Lane, body.Generation)); + } + /// Adds a kinematic anchor with no shape (rotation as for ); returns its lane index. - public int AddAnchor(ulong stableId, (float X, float Y, float Z) position, (float X, float Y, float Z, float W) rotation = default) => + public Box3DSoaBodyHandle AddAnchor(ulong stableId, (float X, float Y, float Z) position, (float X, float Y, float Z, float W) rotation = default) => AddBody(Describe(Box3DSoaKind.Anchor, stableId, position, rotation, default, 0f, default, 0f, 0f, -1, 0)); /// @@ -1335,11 +1371,12 @@ public sealed class Box3DSoaWorld : IDisposable }; } - private int AddBody(Box3DSoaBodyDesc desc) + private unsafe Box3DSoaBodyHandle AddBody(Box3DSoaBodyDesc desc) { ObjectDisposedException.ThrowIf(_handle == 0, this); Check(Box3DSoaNative.ymir_box3d_soa_add_body(_handle, desc, out var index)); - return index; + Check(Box3DSoaNative.ymir_box3d_soa_get_view(_handle, out var v)); + return new Box3DSoaBodyHandle(index, v.BodyGeneration[index]); } public void Step(float timeStep, int subStepCount = 4) diff --git a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaImpulseTests.cs b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaImpulseTests.cs index f76f3eb..de0a529 100644 --- a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaImpulseTests.cs +++ b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaImpulseTests.cs @@ -36,7 +36,7 @@ public sealed class Box3DSoaImpulseTests var fore = world.AddCapsule(3, (0.78f, 0.2f, 0f), 0.04f, 0.14f, 1000f, rotation: QuarterTurnZ); world.AddJoint(Box3DSoaJointKind.Spherical, torso, upper, (0.2f, 0.2f, 0f), (0f, 0f, 1f)); world.AddJoint(Box3DSoaJointKind.Revolute, upper, fore, (0.6f, 0.2f, 0f), (0f, 0f, 1f)); - var arm = new[] { (torso, 0.35f * 0.5f * 0.2f * 680f), (upper, CapsuleMass(0.045f, 0.15f, 1000f)), (fore, CapsuleMass(0.04f, 0.14f, 1000f)) }; + var arm = new (int Lane, float Mass)[] { (torso, 0.35f * 0.5f * 0.2f * 680f), (upper, CapsuleMass(0.045f, 0.15f, 1000f)), (fore, CapsuleMass(0.04f, 0.14f, 1000f)) }; return (world, arm); } diff --git a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaRemoveTests.cs b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaRemoveTests.cs new file mode 100644 index 0000000..9b2ce7a --- /dev/null +++ b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaRemoveTests.cs @@ -0,0 +1,282 @@ +using Ymir.Box3D; + +namespace Ymir.Box3D.Soa.Tests; + +/// Body removal: docs/soa-world.md, Release. RemoveBody is the one body-ending path; it is refused while a liquid sensor or attachment names the body. +public sealed class Box3DSoaRemoveTests +{ + private const float Dt = 1f / 60f; + private const float R = 0.0125f; + + private static Box3DSoaWorld World(int bodies = 8, int joints = 4, int fluid = 16, int wets = 8, int sensors = 4) => + new(bodies, gravityY: -10f, jointCapacity: joints, + particles: new Box3DSoaParticleCapacities(16, 16, 16, 4, 16, 2, fluid, wets, sensors)); + + private static void Run(Box3DSoaWorld world, int steps) + { + for (var i = 0; i < steps; i++) world.Step(Dt); + } + + /// The state a refusal must leave alone: every body and joint lane that RemoveBody writes. + private static string Snapshot(Box3DSoaWorld world) + { + var v = world.View; + return string.Join(",", v.Kind.ToArray()) + "|" + string.Join(",", v.BodyGeneration.ToArray()) + "|" + string.Join(",", v.StableId.ToArray()) + + "|" + string.Join(",", v.JointKind.ToArray()) + "|" + string.Join(",", v.JointGeneration.ToArray()) + "|" + string.Join(",", v.WetLive.ToArray()) + + "|" + string.Join(",", v.SensorLive.ToArray()) + "|" + string.Join(",", v.AttachmentKind.ToArray()) + "|" + world.StepCount; + } + + [Fact] + public void Removing_a_body_retires_its_joints_and_zeroes_its_lane() + { + using var world = World(); + var pin = world.AddStaticBox(1, (0f, 5f, 0f), (0.1f, 0.1f, 0.1f)); + var upper = world.AddCapsule(2, (0f, 4.5f, 0f), 0.1f, 0.4f, 1000f, parent: pin, limb: 3, materialId: 9); + var lower = world.AddCapsule(3, (0f, 3.5f, 0f), 0.1f, 0.4f, 1000f, parent: upper); + var elsewhere = world.AddSphere(4, (3f, 5f, 0f), 0.1f, 1000f); + var j1 = world.AddJoint(Box3DSoaJointKind.Spherical, pin, upper, (0f, 5f, 0f), (0f, 0f, 1f)); + var j2 = world.AddJoint(Box3DSoaJointKind.Revolute, upper, lower, (0f, 4f, 0f), (0f, 0f, 1f)); + var j3 = world.AddJoint(Box3DSoaJointKind.Weld, pin, elsewhere, (1f, 5f, 0f), (0f, 0f, 1f)); + Run(world, 10); + var view = world.View; + Assert.Equal(upper.Generation, view.BodyGeneration[upper]); + + world.RemoveBody(upper); + Assert.Equal((uint)Box3DSoaKind.Removed, view.Kind[upper]); + Assert.Equal(upper.Generation + 1, view.BodyGeneration[upper]); + Assert.Equal(0ul, view.StableId[upper]); + Assert.Equal(-1, view.Parent[upper]); + Assert.Equal(0, view.Limb[upper]); + Assert.Equal(0ul, view.MaterialId[upper]); + Assert.Equal(0, view.ContactCount[upper]); + Assert.Equal(0f, view.PositionX[upper] + view.PositionY[upper] + view.PositionZ[upper]); + Assert.Equal(0f, view.RotationX[upper] + view.RotationW[upper] + view.VelocityY[upper]); + Assert.Equal(0f, view.Size0[upper] + view.Size1[upper] + view.ContactImpulse[upper]); + foreach (var joint in new[] { j1, j2 }) + { + Assert.Equal((uint)Box3DSoaJointKind.Removed, view.JointKind[joint]); + Assert.Equal(joint.Generation + 1, view.JointGeneration[joint]); + Assert.Equal(-1, view.JointBodyA[joint]); + Assert.Throws(() => world.DestroyJoint(joint)); // the old handles are stale + } + Assert.Equal((uint)Box3DSoaJointKind.Weld, view.JointKind[j3]); // a joint that does not name it stays + + Assert.Throws(() => world.RemoveBody(upper)); // already gone + Assert.Throws(() => world.RemoveBody(new Box3DSoaBodyHandle(lower.Lane, lower.Generation + 1))); // stale + Assert.Throws(() => world.RemoveBody(new Box3DSoaBodyHandle(8, 0))); // out of range + Assert.Throws(() => world.RemoveBody(new Box3DSoaBodyHandle(-1, 0))); + + Run(world, 30); // the world steps on, the removed lane stays a tombstone, the orphan falls free + Assert.Equal((uint)Box3DSoaKind.Removed, view.Kind[upper]); + Assert.Equal(0f, view.PositionY[upper]); + Assert.True(view.PositionY[lower] < 3.5f - 0.5f, "the child of a removed body is free"); + Assert.Equal(lower.Generation, view.BodyGeneration[lower]); + } + + [Fact] + public void Removal_is_refused_while_an_attachment_or_a_sensor_names_the_body() + { + using var world = World(); + var floor = world.AddStaticBox(1, (0f, -0.5f, 0f), (2f, 0.5f, 2f)); + var box = world.AddBox(2, (0f, 0.1f, 0f), (0.1f, 0.1f, 0.1f), 500f); + var other = world.AddSphere(3, (1f, 0.1f, 0f), 0.1f, 500f); + var joint = world.AddJoint(Box3DSoaJointKind.Weld, box, other, (0.5f, 0.1f, 0f), (0f, 0f, 1f)); + var p = new Box3DSoaParticleHandle[1]; + world.CreateParticles([new((0f, 0.1f, 0f), 0.02f, R)], p); + var attachment = world.Attach(Box3DSoaAttachKind.Body, p[0], box, (0f, 0f, 0f)); + var sensors = new Box3DSoaConstraintHandle[1]; + world.CreateSensors([(box, (0f, 0f, 0f), 0.2f)], sensors); + Run(world, 5); + var before = Snapshot(world); + + Assert.Throws(() => world.RemoveBody(box)); // both name it + Assert.Equal(before, Snapshot(world)); + world.Remove(attachment); + Assert.Throws(() => world.RemoveBody(box)); // the sensor still does + Assert.Equal((uint)Box3DSoaKind.Box, world.View.Kind[box]); + Assert.Equal((uint)Box3DSoaJointKind.Weld, world.View.JointKind[joint]); // a refusal retired nothing + world.Remove(sensors[0]); + world.RemoveBody(box); + + var removed = box.Lane; + Assert.Equal((uint)Box3DSoaKind.Removed, world.View.Kind[removed]); + Assert.Throws(() => world.Attach(Box3DSoaAttachKind.Body, p[0], removed, (0f, 0f, 0f))); + Assert.Throws(() => world.CreateSensors([(removed, (0f, 0f, 0f), 0.2f)])); + Assert.Throws(() => world.AddJoint(Box3DSoaJointKind.Weld, floor, removed, (0f, 0f, 0f), (0f, 0f, 1f))); + Assert.Throws(() => world.AddSphere(10, (2f, 1f, 0f), 0.1f, 500f, parent: removed)); + Assert.Equal(0, world.View.AttachmentCount == 0 ? 0 : world.View.AttachmentKind.ToArray().Count(k => k == (uint)Box3DSoaAttachKind.Body)); + } + + [Fact] + public void A_removed_anchor_ends_its_grab() + { + using var world = World(); + var anchor = world.AddAnchor(1, (0f, 5f, 0f)); + var sphere = world.AddSphere(2, (0f, 5f, 0f), 0.1f, 1000f); + var grab = world.AddMotorJoint(anchor, sphere, (0f, 5f, 0f), (5f, 0.7f, 5000f), (0f, 0f, 0f)); + Run(world, 30); + var view = world.View; + Assert.Equal(5f, view.PositionY[sphere], 0.3f); // held + + world.RemoveBody(anchor); + Assert.Equal((uint)Box3DSoaJointKind.Removed, view.JointKind[grab]); + Assert.Throws(() => world.DestroyJoint(grab)); // the grab handle is stale + Run(world, 60); + Assert.True(view.PositionY[sphere] < 3f, "the grabbed body falls"); + } + + [Fact] + public void A_drop_resting_on_a_removed_body_cannot_settle() + { + using var world = World(); + var floor = world.AddStaticBox(1, (0f, -0.5f, 0f), (2f, 0.5f, 2f), friction: 0.3f); + var p = new Box3DSoaParticleHandle[1]; + world.CreateParticles([new((0f, R, 0f), 0.015625f, R, Box3DSoaPhase.Of(0, fluid: true), 0u, null, default)], p); + Run(world, 20); + var view = world.View; + Assert.Equal(floor.Lane, view.SupportBody[0]); + world.RemoveBody(floor); + Assert.Equal(floor.Lane, view.SupportBody[0]); // last step's fact: it may name the dead lane until the next substep + var before = Snapshot(world); + Assert.Throws(() => world.Settle(p)); + Assert.Equal(before, Snapshot(world)); + Assert.Equal(1u, view.ParticleLive[0]); + } + + [Fact] + public void Wet_patches_go_with_the_body() + { + using var world = World(fluid: 16, wets: 16); + var a = world.AddStaticBox(1, (-1f, -0.5f, 0f), (0.5f, 0.5f, 0.5f), friction: 0.3f); + var b = world.AddStaticBox(2, (1f, -0.5f, 0f), (0.5f, 0.5f, 0.5f), friction: 0.3f); + var at = new[] { -1.3f, -1f, -0.7f, 0.9f, 1.2f }; + var phase = Box3DSoaPhase.Of(0, fluid: true); + world.CreateParticles(at.Select(x => new Box3DSoaParticleDesc((x, R, 0f), 0.015625f, R, phase, 0u, null, default)).ToArray()); + Run(world, 20); + var view = world.View; + Assert.Equal(new[] { a.Lane, a.Lane, a.Lane, b.Lane, b.Lane }, Enumerable.Range(0, 5).Select(i => view.SupportBody[i]).ToArray()); + world.Settle(Enumerable.Range(0, 5).Select(i => new Box3DSoaParticleHandle(i, view.ParticleGeneration[i])).ToArray()); + view = world.View; + var onA = Enumerable.Range(0, view.WetCount).Where(w => view.WetLive[w] != 0 && view.WetBody[w] == a.Lane).ToArray(); + var onB = Enumerable.Range(0, view.WetCount).Where(w => view.WetLive[w] != 0 && view.WetBody[w] == b.Lane).ToArray(); + Assert.Equal(3, onA.Length); + Assert.Equal(2, onB.Length); + var volumeB = view.WetVolume[b.Lane]; + var generationsB = onB.Select(w => view.WetGeneration[w]).ToArray(); + Assert.True(view.WetVolume[a.Lane] > 0f && volumeB > 0f); + var generationsA = onA.Select(w => view.WetGeneration[w]).ToArray(); + + world.RemoveBody(a); + for (var k = 0; k < onA.Length; k++) + { + Assert.Equal(0u, view.WetLive[onA[k]]); + Assert.Equal(generationsA[k] + 1, view.WetGeneration[onA[k]]); + } + Assert.Equal(0f, view.WetVolume[a.Lane]); + Assert.Equal(volumeB, view.WetVolume[b.Lane]); // the other body's patches are untouched + for (var k = 0; k < onB.Length; k++) + { + Assert.Equal(1u, view.WetLive[onB[k]]); + Assert.Equal(generationsB[k], view.WetGeneration[onB[k]]); + } + } + + [Fact] + public void Lanes_survive_the_removal_of_others() + { + using var world = World(bodies: 8, joints: 0); + var h = Enumerable.Range(0, 6).Select(i => world.AddSphere((ulong)(i + 1), (i * 0.5f, 5f + i, 0f), 0.1f, 1000f, velocity: (0.1f * i, 0f, 0f))).ToArray(); + Run(world, 10); + var view = world.View; + var keep = new[] { 0, 1, 2, 4, 5 }; + var posBefore = keep.Select(i => (view.PositionX[h[i]], view.PositionY[h[i]], view.VelocityX[h[i]])).ToArray(); + world.RemoveBody(h[3]); + foreach (var i in keep) + { + Assert.Equal((uint)Box3DSoaKind.Sphere, view.Kind[h[i]]); + Assert.Equal((ulong)(i + 1), view.StableId[h[i]]); + Assert.Equal(h[i].Generation, view.BodyGeneration[h[i]]); + } + Assert.Equal(posBefore, keep.Select(i => (view.PositionX[h[i]], view.PositionY[h[i]], view.VelocityX[h[i]])).ToArray()); // continuity: nothing moved + Run(world, 5); + Assert.Equal(6, view.Count); // the high-water mark + Assert.All(keep, i => Assert.True(view.PositionY[h[i]] < 5f + i, "the others keep falling")); + } + + [Fact] + public void A_removed_lane_is_reused_one_step_later_with_a_new_generation() + { + using var world = World(bodies: 3, joints: 0); + var a = world.AddSphere(1, (0f, 5f, 0f), 0.1f, 1000f); + var b = world.AddSphere(2, (1f, 5f, 0f), 0.1f, 1000f); + var c = world.AddSphere(3, (2f, 5f, 0f), 0.1f, 1000f); + Assert.Equal(0, world.FreeBodySlots); + Assert.Throws(() => world.AddSphere(4, (3f, 5f, 0f), 0.1f, 1000f)); // full + world.RemoveBody(b); + Assert.Equal(0, world.FreeBodySlots); // not within the removing step + Assert.Throws(() => world.AddSphere(4, (3f, 5f, 0f), 0.1f, 1000f)); + world.Step(Dt); + Assert.Equal(1, world.FreeBodySlots); + var d = world.AddSphere(2, (3f, 5f, 0f), 0.1f, 1000f); // the removed stable id may be reused + Assert.Equal(b.Lane, d.Lane); + Assert.Equal(b.Generation + 1, d.Generation); + var view = world.View; + Assert.Equal((uint)Box3DSoaKind.Sphere, view.Kind[d]); + Assert.Equal(3f, view.PositionX[d], 1e-5f); + Assert.Equal(3, view.Count); + Assert.Throws(() => world.RemoveBody(b)); // the old handle is stale against the new occupant + Assert.Equal((uint)Box3DSoaKind.Sphere, view.Kind[d]); + Assert.Throws(() => world.AddSphere(2, (4f, 5f, 0f), 0.1f, 1000f)); // a live stable id still collides (and the world is full) + world.RemoveBody(a); + world.Step(Dt); + Assert.Equal(a.Lane, world.AddSphere(7, (5f, 5f, 0f), 0.1f, 1000f).Lane); // lowest free lane first + _ = c; + } + + private static byte[] Replay() + { + using var world = World(bodies: 8, joints: 4); + world.AddStaticBox(1, (0f, -0.5f, 0f), (4f, 0.5f, 4f)); + var a = world.AddCapsule(2, (0f, 1f, 0f), 0.1f, 0.3f, 800f); + var b = world.AddSphere(3, (0.3f, 2f, 0f), 0.1f, 800f); + var c = world.AddBox(4, (-0.3f, 3f, 0f), (0.1f, 0.1f, 0.1f), 800f); + var j1 = world.AddJoint(Box3DSoaJointKind.Spherical, a, b, (0.1f, 1.5f, 0f), (0f, 0f, 1f), forceThreshold: 50f); + var j2 = world.AddJoint(Box3DSoaJointKind.Weld, a, c, (-0.1f, 1.5f, 0f), (0f, 0f, 1f)); + var view = world.View; + var o = new MemoryStream(); + void B(ReadOnlySpan lane) where T : unmanaged => o.Write(System.Runtime.InteropServices.MemoryMarshal.AsBytes(lane)); + for (var i = 0; i < 300; i++) + { + if (i == 60) world.RemoveBody(b); + if (i == 120) world.SetJointThresholds(j2, 5f, float.PositiveInfinity); + world.Step(Dt); + B(view.PositionX); B(view.PositionY); B(view.RotationW); B(view.VelocityY); B(view.Kind); B(view.BodyGeneration); B(view.StableId); + B(view.JointKind); B(view.JointGeneration); B(view.JointConstraintForceY); B(view.JointConstraintTorqueZ); + B(world.Facts.Overloads); + } + Assert.Equal((uint)Box3DSoaJointKind.Removed, view.JointKind[j1]); + return o.ToArray(); + } + + [Fact] + public void A_world_with_removals_and_thresholds_repeats_bit_for_bit() + { + var first = Replay(); + Assert.True(first.AsSpan().SequenceEqual(Replay())); + Assert.Contains(first, x => x != 0); + } + + [Fact] + public void Removing_a_body_and_the_step_after_allocate_nothing_managed() + { + using var world = World(bodies: 8, joints: 2); + var a = world.AddSphere(1, (0f, 5f, 0f), 0.1f, 1000f); + var b = world.AddSphere(2, (1f, 5f, 0f), 0.1f, 1000f); + world.AddJoint(Box3DSoaJointKind.Weld, a, b, (0.5f, 5f, 0f), (0f, 0f, 1f)); + Run(world, 5); + var before = GC.GetAllocatedBytesForCurrentThread(); + world.RemoveBody(a); + world.Step(Dt); + Assert.Equal(0L, GC.GetAllocatedBytesForCurrentThread() - before); + } +} diff --git a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaWetFrictionTests.cs b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaWetFrictionTests.cs index 919528d..4c59684 100644 --- a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaWetFrictionTests.cs +++ b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaWetFrictionTests.cs @@ -25,7 +25,7 @@ public sealed partial class Box3DSoaFluidTests world.CreateParticles([Fluid((patchAt.X, DropRadius, patchAt.Z), FluidMass * scale * scale * scale, DropRadius, material: 1u)]); for (var tick = 0; tick < 20; tick++) world.Step(Dt, Sub); if (patch) world.Settle([Handle(world, 0)]); else world.DestroyParticles([Handle(world, 0)]); - var lanes = boxes.Select((at, i) => world.AddBox(10ul + (ulong)i, (at.X, 0.1f, at.Z), (0.1f, 0.1f, 0.1f), BoxMass / 0.008f, friction: 0.6f)).ToArray(); + var lanes = boxes.Select((at, i) => (int)world.AddBox(10ul + (ulong)i, (at.X, 0.1f, at.Z), (0.1f, 0.1f, 0.1f), BoxMass / 0.008f, friction: 0.6f)).ToArray(); return (world, lanes); } diff --git a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaWetTests.cs b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaWetTests.cs index 3874f43..d93e701 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, remove(WET) and destroy). + // ends in rederive_wet, never in rederive_topology; rederive_wet's callers are settle, remove(WET), release_body 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(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) + Assert.Equal(4, System.Text.RegularExpressions.Regex.Matches(source, @"[^_]rederive_wet\(p\);").Count); // settle, remove(WET), release_body (a removed body takes its patches), and destroy when a destroyed solid takes its patches (particle_wet_mu goes dry again) } // ---- determinism ----------------------------------------------------------------------------------------