SoA particles r2: chains couple to bodies inside Box3D's substep

Delete the post-step coupling (carry, cancel/restore, hard_impulse, k_body and
end_force_scale, add_link_load and tension_diff, ymir_soa_apply_particle_impulses,
ymir_particles_step) and replace it with begin, substep and end. The substep runs in
Box3D's new substep callback (extern/box3d moves to the fork's substep-callback commit),
after the biased solve and before positions integrate: each proxy takes the body's pose
at the substep's start and its solved velocity, every correction is lambda / h^2 and
returns to the body as a velocity change there, and Box3D integrates the body to where
the constraint put it. Particle-body contacts run last in each substep. Link tension is
read by momentum balance from the structural impulses past each link.

The deletion and its replacement share one commit: the step cannot build without both.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-23 04:18:58 +01:00
commit 66179d68b4
5 changed files with 236 additions and 166 deletions

2
extern/box3d vendored

@ -1 +1 @@
Subproject commit 6cfeeff232c0752d2d1f7b162d6f78bf105e8819
Subproject commit fb719c49c07f521eb748b2000e2e2c955b4bd219

View file

@ -335,9 +335,10 @@ typedef struct ymir_box3d_soa_world
b3ContactData* contact_scratch; // `capacity` entries: a body has one shape, so at most capacity - 1 contacts
b3Vec3 gravity;
ymir_particles* particles; // NULL when created without particle capacity: the step then runs exactly as ABI 8
float* carry; // 3 per body: the particle phase's deferred impulse, applied before the next Box3D step
} ymir_box3d_soa_world;
static void ymir_soa_particle_substep(b3WorldId world_id, int sub_step, float h, void* context);
static float* ymir_soa_lane(const ymir_box3d_soa_world* world, int lane)
{
return world->lanes + (size_t)lane * (size_t)world->capacity;
@ -490,7 +491,6 @@ static void ymir_soa_free(ymir_box3d_soa_world* world)
free(world->hit_facts);
free(world->contact_scratch);
ymir_particles_free(world->particles);
free(world->carry);
free(world);
}
@ -548,8 +548,7 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_create(
if (particle_capacity > 0 || chain_capacity > 0 || attachment_capacity > 0)
{
world->particles = ymir_particles_create(particle_capacity, chain_capacity, attachment_capacity, capacity);
world->carry = (float*)calloc(n * 3, sizeof(float));
if (world->particles == NULL || world->carry == NULL)
if (world->particles == NULL)
{
ymir_soa_free(world);
return YMIR_SOA_OUT_OF_MEMORY;
@ -578,6 +577,10 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_create(
ymir_soa_free(world);
return YMIR_SOA_INTERNAL_ERROR;
}
if (world->particles != NULL)
{
b3World_SetSubStepCallback(world->world_id, ymir_soa_particle_substep, world);
}
*output = world;
return YMIR_SOA_OK;
@ -1064,9 +1067,10 @@ static void ymir_soa_collect_contact_lanes(ymir_box3d_soa_world* world)
}
}
// The particle phase's view of the bodies, after Box3D's step: pose, velocity, shape and the real inverse mass and
// world inverse inertia of each dynamic body (0 for static, kinematic and anchor bodies). Every shape is centred on
// the body origin, so the origin is the centre of mass.
// The particle phase's view of the bodies, before Box3D's step: shape, size, and the real inverse mass and world
// inverse inertia of each dynamic body (0 for static, kinematic and anchor bodies), with the step-start pose and
// velocity (the substep callback refreshes pose and velocity every substep). Every shape is centred on the body
// origin, so the origin is the centre of mass.
static void ymir_soa_gather_proxies(ymir_box3d_soa_world* world)
{
ymir_particles* pt = world->particles;
@ -1103,32 +1107,48 @@ static void ymir_soa_gather_proxies(ymir_box3d_soa_world* world)
}
}
// What the particle phase delivered to each dynamic body, as the impulse Box3D applies now (a velocity change, never
// a teleport) and the lane a consumer reads.
static void ymir_soa_apply_particle_impulses(ymir_box3d_soa_world* world)
// Box3D's substep callback: each proxy takes the body's pose at the substep's start and its velocity after the
// biased solve; the particle substep corrects both sides; each dynamic body's correction goes back as an impulse
// before Box3D integrates its position, so the body ends the substep where the constraint put it.
static void ymir_soa_particle_substep(b3WorldId world_id, int sub_step, float h, void* context)
{
const ymir_particles* pt = world->particles;
(void)world_id;
(void)sub_step;
ymir_box3d_soa_world* world = (ymir_box3d_soa_world*)context;
ymir_particles* pt = world->particles;
if (pt->live_chains == 0)
{
return;
}
for (int i = 0; i < world->count; ++i)
{
ymir_particle_proxy* b = pt->proxies + i;
b3Pos c;
b3Quat q;
b3Vec3 v, w;
if (!b3Body_GetSubStepState(world->bodies[i], &c, &q, &v, &w))
{
b->inv_mass = 0.0f;
}
b->position[0] = c.x; b->position[1] = c.y; b->position[2] = c.z;
b->rotation[0] = q.v.x; b->rotation[1] = q.v.y; b->rotation[2] = q.v.z; b->rotation[3] = q.s;
b->velocity[0] = v.x; b->velocity[1] = v.y; b->velocity[2] = v.z;
b->angular_velocity[0] = w.x; b->angular_velocity[1] = w.y; b->angular_velocity[2] = w.z;
}
ymir_particles_substep(pt, h, world->gravity.x, world->gravity.y, world->gravity.z);
for (int i = 0; i < world->count; ++i)
{
const ymir_particle_proxy* b = pt->proxies + i;
for (int k = 0; k < 3; ++k)
{
ymir_soa_lane(world, YMIR_SOA_PARTICLE_IMPULSE_LANE + k)[i] = b->impulse[k] + b->carry[k];
world->carry[3 * i + k] = b->carry[k];
}
if (!ymir_soa_is_dynamic(world->kind[i]))
if (b->inv_mass == 0.0f)
{
continue;
}
b3Vec3 linear = {b->impulse[0], b->impulse[1], b->impulse[2]};
b3Vec3 angular = {b->angular_impulse[0], b->angular_impulse[1], b->angular_impulse[2]};
if (linear.x != 0.0f || linear.y != 0.0f || linear.z != 0.0f)
b3Vec3 linear = {b->sub_impulse[0], b->sub_impulse[1], b->sub_impulse[2]};
b3Vec3 angular = {b->sub_angular[0], b->sub_angular[1], b->sub_angular[2]};
if (linear.x != 0.0f || linear.y != 0.0f || linear.z != 0.0f || angular.x != 0.0f || angular.y != 0.0f ||
angular.z != 0.0f)
{
b3Body_ApplyLinearImpulseToCenter(world->bodies[i], linear, true);
}
if (angular.x != 0.0f || angular.y != 0.0f || angular.z != 0.0f)
{
b3Body_ApplyAngularImpulse(world->bodies[i], angular, true);
b3Body_ApplySubStepImpulse(world->bodies[i], linear, angular);
}
}
}
@ -1144,22 +1164,27 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_step(ymir_box3d_soa_world* world, f
{
return status;
}
if (world->carry != NULL)
int particles_live = 0;
if (world->particles != NULL)
{
// The impulse the last particle phase deferred: the support a tied body needs before gravity integrates.
ymir_soa_gather_proxies(world);
particles_live = ymir_particles_begin(world->particles);
}
b3World_Step(world->world_id, time_step, sub_step_count); // the particle substeps run inside it
if (world->particles != NULL)
{
ymir_particles_end(world->particles, sub_step_count);
for (int i = 0; i < world->count; ++i)
{
b3Vec3 c = {world->carry[3 * i], world->carry[3 * i + 1], world->carry[3 * i + 2]};
if (c.x != 0.0f || c.y != 0.0f || c.z != 0.0f)
const ymir_particle_proxy* b = world->particles->proxies + i;
for (int k = 0; k < 3; ++k)
{
b3Body_ApplyLinearImpulseToCenter(world->bodies[i], c, true);
world->carry[3 * i] = world->carry[3 * i + 1] = world->carry[3 * i + 2] = 0.0f;
ymir_soa_lane(world, YMIR_SOA_PARTICLE_IMPULSE_LANE + k)[i] = particles_live ? b->impulse[k] : 0.0f;
}
}
}
b3World_Step(world->world_id, time_step, sub_step_count);
// An anchor moves only on a target: the one just applied has been reached, so it holds still until the
// next target. Otherwise its velocity would carry it on past the pose (and fling what it holds).
// next target. Otherwise its velocity would run it on past the pose (and fling what it holds).
for (int i = 0; i < world->count; ++i)
{
if (ymir_soa_is_anchor(world->kind[i]))
@ -1168,14 +1193,6 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_step(ymir_box3d_soa_world* world, f
b3Body_SetAngularVelocity(world->bodies[i], b3Vec3_zero);
}
}
if (world->particles != NULL)
{
// Box3D has stepped: the particles now move against the bodies' new poses and hand back what they pushed.
ymir_soa_gather_proxies(world);
ymir_particles_step(
world->particles, time_step, sub_step_count, world->gravity.x, world->gravity.y, world->gravity.z);
ymir_soa_apply_particle_impulses(world);
}
ymir_soa_collect_facts(world);
ymir_soa_collect_contact_lanes(world);
for (int i = 0; i < world->count; ++i)

View file

@ -92,7 +92,7 @@ static size_t layout(ymir_particles* p, unsigned char* base)
CARVE(pp_x, float, np); CARVE(pp_y, float, np); CARVE(pp_z, float, np);
CARVE(dp_x, float, np); CARVE(dp_y, float, np); CARVE(dp_z, float, np);
CARVE(rest_link, float, np); CARVE(rest_bend, float, np); CARVE(rest_pos, float, np);
CARVE(tension_acc, float, np); CARVE(tension_diff, float, np + 1);
CARVE(tension_acc, float, np); CARVE(struct_x, float, np); CARVE(struct_y, float, np); CARVE(struct_z, float, np);
CARVE(skip_body, int32_t, np); CARVE(cell_x, int32_t, np); CARVE(cell_y, int32_t, np); CARVE(cell_z, int32_t, np);
CARVE(cell_start, int32_t, table + 1); CARVE(cell_fill, int32_t, table); CARVE(sorted, int32_t, np);
CARVE(mark, uint8_t, np);
@ -517,9 +517,8 @@ int32_t ymir_particles_detach(ymir_particles* p, int32_t attachment, uint32_t ge
typedef struct phase
{
ymir_particles* p;
float dt, h, inv_dt;
float k_particle; // force of a correction made once per substep: lambda / h^2
float k_body; // force of a correction that moves a body once per tick: summed over the substeps / dt^2
float h, inv_h;
float k; // force of a correction: lambda / h^2
} phase;
// Generalised inverse mass of a proxy along unit `n` at world point `at`.
@ -534,9 +533,9 @@ static float proxy_w(const ymir_particle_proxy* b, v3 at, v3 n)
return b->inv_mass + vdot(rn, mat_mul(b->inv_inertia, rn));
}
// Applies a position-level impulse `imp` (kg m) at `at` to a proxy: moves its pose and books the velocity-level
// impulse (imp / dt) the tick will hand back to Box3D.
static void proxy_push(const phase* f, ymir_particle_proxy* b, v3 at, v3 imp, int hard)
// Applies a position-level impulse `imp` (kg m) at `at` to a proxy: moves its predicted pose and books the
// velocity-level impulse (imp / h) the caller applies to the body before Box3D integrates its position.
static void proxy_push(const phase* f, ymir_particle_proxy* b, v3 at, v3 imp)
{
if (b->inv_mass == 0.0f)
{
@ -565,13 +564,12 @@ static void proxy_push(const phase* f, ymir_particle_proxy* b, v3 at, v3 imp, in
{
q[k] *= inv;
}
float* acc = hard ? b->hard_impulse : b->impulse;
acc[0] += imp.x * f->inv_dt;
acc[1] += imp.y * f->inv_dt;
acc[2] += imp.z * f->inv_dt;
b->angular_impulse[0] += torque.x * f->inv_dt;
b->angular_impulse[1] += torque.y * f->inv_dt;
b->angular_impulse[2] += torque.z * f->inv_dt;
b->sub_impulse[0] += imp.x * f->inv_h;
b->sub_impulse[1] += imp.y * f->inv_h;
b->sub_impulse[2] += imp.z * f->inv_h;
b->sub_angular[0] += torque.x * f->inv_h;
b->sub_angular[1] += torque.y * f->inv_h;
b->sub_angular[2] += torque.z * f->inv_h;
}
// The world point an attachment holds, and the inverse mass of what it holds it to along `n`.
@ -608,7 +606,7 @@ static void end_push(const phase* f, int32_t a, v3 at, v3 imp)
ymir_particles* p = f->p;
if (p->att_kind[a] == YMIR_ATTACH_PARTICLE_BODY)
{
proxy_push(f, p->proxies + p->att_body[a], at, imp, 1);
proxy_push(f, p->proxies + p->att_body[a], at, imp);
}
else if (p->att_kind[a] == YMIR_ATTACH_PARTICLE_PARTICLE)
{
@ -617,16 +615,6 @@ static void end_push(const phase* f, int32_t a, v3 at, v3 imp)
}
}
// A correction that moves a dynamic body happens once per tick, not once per substep, so its force is the sum
// of the substeps' lambdas over dt^2; every other correction is lambda / h^2 each substep. Both are written as
// per-substep terms that the step averages.
static float end_force_scale(const phase* f, int32_t a)
{
const ymir_particles* p = f->p;
int dynamic_body = p->att_kind[a] == YMIR_ATTACH_PARTICLE_BODY && p->proxies[p->att_body[a]].inv_mass > 0.0f;
return dynamic_body ? f->k_body : f->k_particle;
}
// Adds a force (N) the attachment `a` exerts on the chain to its sensor lane.
static void add_force(ymir_particles* p, int32_t a, v3 force)
{
@ -635,13 +623,14 @@ static void add_force(ymir_particles* p, int32_t a, v3 force)
p->att_force_z[a] += force.z;
}
// Books a load of `force` N carried along the chain between particles `from` and `to` (same chain): every link
// between them transmits it, so a tether that carries the load past the links is still read by the links.
static void add_link_load(ymir_particles* p, int32_t from, int32_t to, float force)
// Books a structural position impulse (kg m) the chain delivers to its particle `i`: links, bends and tethers.
// Every booking has an equal and opposite one, so the sum over the particles past a link is the load the link
// carries (momentum balance), however the solver routed it.
static void add_struct(ymir_particles* p, int32_t i, v3 imp)
{
int32_t lo = from < to ? from : to, hi = from < to ? to : from;
p->tension_diff[lo] += force;
p->tension_diff[hi] -= force;
p->struct_x[i] += imp.x;
p->struct_y[i] += imp.y;
p->struct_z[i] += imp.z;
}
// ---- grid and particle-particle contact ----------------------------------------------------------------------
@ -864,7 +853,7 @@ static void body_contacts(phase* f)
float wb = proxy_w(b, s, n);
float w = wi + wb;
x = vadd(x, vmul(n, depth * wi / w));
proxy_push(f, b, s, vmul(n, -depth / w), 0);
proxy_push(f, b, s, vmul(n, -depth / w));
if (mu > 0.0f)
{
// Slip of the particle against the body point over this substep, tangential part.
@ -880,7 +869,7 @@ static void body_contacts(phase* f)
float move = vmin(sl, mu * 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), 0);
proxy_push(f, b, s, vmul(t, move / wt));
}
}
put(p->pp_x, p->pp_y, p->pp_z, i, x);
@ -917,7 +906,7 @@ static void solve_attachments(phase* f)
float dl = -len / den;
put(p->pp_x, p->pp_y, p->pp_z, i, vadd(P(p, p->pp_x, p->pp_y, p->pp_z, i), vmul(n, p->inv_mass[i] * dl)));
end_push(f, a, at, vmul(n, -dl));
add_force(p, a, vmul(n, dl * end_force_scale(f, a)));
add_force(p, a, vmul(n, dl * f->k));
}
}
@ -935,6 +924,8 @@ static float distance_constraint(ymir_particles* p, int32_t i, int32_t j, float
float dl = -(len - rest) / (wi + wj + alpha);
put(p->pp_x, p->pp_y, p->pp_z, i, vsub(P(p, p->pp_x, p->pp_y, p->pp_z, i), vmul(n, wi * dl)));
put(p->pp_x, p->pp_y, p->pp_z, j, vadd(P(p, p->pp_x, p->pp_y, p->pp_z, j), vmul(n, wj * dl)));
add_struct(p, i, vmul(n, -dl));
add_struct(p, j, vmul(n, dl));
return dl;
}
@ -953,8 +944,7 @@ static void solve_chains(phase* f)
int32_t first = p->chain_first[c], last = first + p->chain_size[c];
for (int32_t i = first; i + 1 < last; ++i)
{
float dl = distance_constraint(p, i, i + 1, p->rest_link[i], p->chain_stretch[c] / hh);
p->tension_acc[i] += -dl * f->k_particle;
distance_constraint(p, i, i + 1, p->rest_link[i], p->chain_stretch[c] / hh);
}
for (int32_t i = first; i + 2 < last; ++i)
{
@ -969,8 +959,8 @@ static int tetherable(const ymir_particles* p, int32_t a)
}
// Long-range attachments: a unilateral distance bound at the chain's rest path length from each attachment point
// to every particle of its chain, and between the attachment points of one chain. They carry load past the links,
// so the load they carry is booked on the links and on the attachments (the sensors read the load, not the path).
// to every particle of its chain, and between the attachment points of one chain. They bear load past the links,
// so their structural impulses are booked on the particles they pull and the sensors read the load, not the path.
static void solve_tethers(phase* f)
{
ymir_particles* p = f->p;
@ -1005,9 +995,9 @@ static void solve_tethers(phase* f)
float dl = -(len - bound) / den;
put(p->pp_x, p->pp_y, p->pp_z, j, vadd(P(p, p->pp_x, p->pp_y, p->pp_z, j), vmul(n, p->inv_mass[j] * dl)));
end_push(f, a, at, vmul(n, -dl));
float k = end_force_scale(f, a);
add_force(p, a, vmul(n, dl * k)); // the force on particle j, toward the attachment
add_link_load(p, i, j, -dl * k);
add_force(p, a, vmul(n, dl * f->k)); // the force on the chain, toward the attachment
add_struct(p, j, vmul(n, dl)); // the chain between i and j pulls j toward i
add_struct(p, i, vmul(n, -dl)); // and i toward j
}
}
for (int32_t a = 0; a < p->attachment_count; ++a)
@ -1038,34 +1028,29 @@ static void solve_tethers(phase* f)
continue;
}
float dl = -(len - bound) / den;
float k = end_force_scale(f, a) == f->k_body || end_force_scale(f, b) == f->k_body ? f->k_body : f->k_particle;
end_push(f, a, pa, vmul(n, dl));
end_push(f, b, pb, vmul(n, -dl));
add_force(p, a, vmul(n, -dl * k)); // each point pulls the chain away from the other
add_force(p, b, vmul(n, dl * k));
add_link_load(p, p->att_particle[a], p->att_particle[b], -dl * k);
add_force(p, a, vmul(n, -dl * f->k)); // each point pulls the chain away from the other
add_force(p, b, vmul(n, dl * f->k));
add_struct(p, p->att_particle[a], vmul(n, dl)); // the chain between pulls each end toward the other
add_struct(p, p->att_particle[b], vmul(n, -dl));
}
}
}
// ---- the step ------------------------------------------------------------------------------------------------
void ymir_particles_step(ymir_particles* p, float dt, int32_t substeps, float gx, float gy, float gz)
int32_t ymir_particles_begin(ymir_particles* p)
{
if (p == NULL || p->live_chains == 0)
{
return;
return 0;
}
phase f = {p, dt, dt / (float)substeps, 1.0f / dt, 0.0f, 0.0f};
f.k_particle = 1.0f / (f.h * f.h);
f.k_body = 1.0f / ((float)substeps * f.h * f.h);
v3 g = {gx, gy, gz};
for (int32_t k = 0; k < p->body_count; ++k)
{
memset(p->proxies[k].impulse, 0, sizeof(p->proxies[k].impulse));
memset(p->proxies[k].hard_impulse, 0, sizeof(p->proxies[k].hard_impulse));
memset(p->proxies[k].carry, 0, sizeof(p->proxies[k].carry));
memset(p->proxies[k].angular_impulse, 0, sizeof(p->proxies[k].angular_impulse));
ymir_particle_proxy* b = p->proxies + k;
memset(b->impulse, 0, sizeof(b->impulse));
memset(b->angular_impulse, 0, sizeof(b->angular_impulse));
}
for (int32_t i = 0; i < p->particle_count; ++i)
{
@ -1080,77 +1065,120 @@ void ymir_particles_step(ymir_particles* p, float dt, int32_t substeps, float gx
p->skip_body[p->att_particle[a]] = p->att_body[a];
}
}
return 1;
}
for (int32_t s = 0; s < substeps; ++s)
// One substep of `h` against the proxies' poses at its start and velocities after the rigid solve: proxies are
// predicted like particles, every constraint moves both sides by inverse mass, and each proxy's correction returns
// as sub_impulse (its correction over h) for the caller to apply before the rigid positions integrate. Position,
// velocity and momentum stay one state: XPBD's velocity update, done by the rigid integrator.
void ymir_particles_substep(ymir_particles* p, float h, float gx, float gy, float gz)
{
if (p == NULL || p->live_chains == 0)
{
for (int32_t i = 0; i < p->particle_count; ++i)
{
if (p->chain[i] < 0)
{
continue;
}
v3 x = P(p, p->pos_x, p->pos_y, p->pos_z, i);
if (p->inv_mass[i] > 0.0f)
{
v3 v = vadd(P(p, p->vel_x, p->vel_y, p->vel_z, i), vmul(g, f.h));
put(p->vel_x, p->vel_y, p->vel_z, i, v);
x = vadd(x, vmul(v, f.h));
}
put(p->pp_x, p->pp_y, p->pp_z, i, x);
}
build_grid(p);
particle_contacts(p);
body_contacts(&f);
solve_attachments(&f);
solve_chains(&f);
solve_tethers(&f);
for (int32_t c = 0; c < p->chain_count; ++c) // the tethers' load, spread over the links it bypassed
{
if (p->chain_kind[c] != YMIR_CHAIN_LIVE)
{
continue;
}
float running = 0.0f;
for (int32_t i = p->chain_first[c]; i < p->chain_first[c] + p->chain_size[c]; ++i)
{
running += p->tension_diff[i];
p->tension_acc[i] += running;
p->tension_diff[i] = 0.0f;
}
}
for (int32_t i = 0; i < p->particle_count; ++i)
{
if (p->chain[i] < 0)
{
continue;
}
v3 x = P(p, p->pos_x, p->pos_y, p->pos_z, i), y = P(p, p->pp_x, p->pp_y, p->pp_z, i);
if (p->inv_mass[i] > 0.0f)
{
put(p->vel_x, p->vel_y, p->vel_z, i, vmul(vsub(y, x), 1.0f / f.h));
}
put(p->pos_x, p->pos_y, p->pos_z, i, y);
}
return;
}
// A body tied to the chain (attachment, tether) cannot be corrected by a post-step impulse alone: a velocity
// from the whole position error rings at six ticks and never settles, and holding a resting body needs the
// support before Box3D integrates gravity, not after. So the hard impulse splits along its direction: the
// velocity the body carries into the constraint is cancelled now (it reads still after the step), and the
// rest, the position error to close, returns as `carry`, applied before the next Box3D step.
phase f = {p, h, 1.0f / h, 1.0f / (h * h)};
v3 g = {gx, gy, gz};
for (int32_t k = 0; k < p->body_count; ++k)
{
ymir_particle_proxy* b = p->proxies + k;
v3 hard = {b->hard_impulse[0], b->hard_impulse[1], b->hard_impulse[2]};
float hl = vlen(hard);
if (b->inv_mass == 0.0f || !(hl > 0.0f))
memset(b->sub_impulse, 0, sizeof(b->sub_impulse));
memset(b->sub_angular, 0, sizeof(b->sub_angular));
// predict the pose the rigid integrator reaches with this velocity
b->position[0] += h * b->velocity[0];
b->position[1] += h * b->velocity[1];
b->position[2] += h * b->velocity[2];
v3 w = vmul((v3){b->angular_velocity[0], b->angular_velocity[1], b->angular_velocity[2]}, h);
float* q = b->rotation;
float dq[4] = {
0.5f * (w.x * q[3] + w.y * q[2] - w.z * q[1]),
0.5f * (w.y * q[3] + w.z * q[0] - w.x * q[2]),
0.5f * (w.z * q[3] + w.x * q[1] - w.y * q[0]),
0.5f * (-w.x * q[0] - w.y * q[1] - w.z * q[2])};
float n2 = 0.0f;
for (int c = 0; c < 4; ++c)
{
q[c] += dq[c];
n2 += q[c] * q[c];
}
float inv = 1.0f / sqrtf(n2);
for (int c = 0; c < 4; ++c)
{
q[c] *= inv;
}
}
for (int32_t i = 0; i < p->particle_count; ++i)
{
if (p->chain[i] < 0)
{
continue;
}
v3 j = vmul(hard, 1.0f / hl);
float cancel = vmax(-vdot((v3){b->velocity[0], b->velocity[1], b->velocity[2]}, j), 0.0f) / b->inv_mass;
float restore = hl;
b->impulse[0] += j.x * cancel; b->impulse[1] += j.y * cancel; b->impulse[2] += j.z * cancel;
b->carry[0] = j.x * restore; b->carry[1] = j.y * restore; b->carry[2] = j.z * restore;
v3 x = P(p, p->pos_x, p->pos_y, p->pos_z, i);
if (p->inv_mass[i] > 0.0f)
{
v3 v = vadd(P(p, p->vel_x, p->vel_y, p->vel_z, i), vmul(g, h));
put(p->vel_x, p->vel_y, p->vel_z, i, v);
x = vadd(x, vmul(v, h));
}
put(p->pp_x, p->pp_y, p->pp_z, i, x);
p->struct_x[i] = p->struct_y[i] = p->struct_z[i] = 0.0f;
}
build_grid(p);
particle_contacts(p);
solve_attachments(&f);
solve_chains(&f);
solve_tethers(&f);
body_contacts(&f);
// Link tension by momentum balance: what the structure delivered to the particles past the link, along it.
for (int32_t c = 0; c < p->chain_count; ++c)
{
if (p->chain_kind[c] != YMIR_CHAIN_LIVE)
{
continue;
}
int32_t first = p->chain_first[c], last = first + p->chain_size[c];
v3 past = {0.0f, 0.0f, 0.0f};
for (int32_t i = last - 2; i >= first; --i)
{
past = vadd(past, P(p, p->struct_x, p->struct_y, p->struct_z, i + 1));
v3 d = vsub(P(p, p->pp_x, p->pp_y, p->pp_z, i + 1), P(p, p->pp_x, p->pp_y, p->pp_z, i));
float len = vlen(d);
if (len > 1e-9f)
{
p->tension_acc[i] += -vdot(past, d) / len * f.k;
}
}
}
for (int32_t i = 0; i < p->particle_count; ++i)
{
if (p->chain[i] < 0)
{
continue;
}
v3 x = P(p, p->pos_x, p->pos_y, p->pos_z, i), y = P(p, p->pp_x, p->pp_y, p->pp_z, i);
if (p->inv_mass[i] > 0.0f)
{
put(p->vel_x, p->vel_y, p->vel_z, i, vmul(vsub(y, x), f.inv_h));
}
put(p->pos_x, p->pos_y, p->pos_z, i, y);
}
for (int32_t k = 0; k < p->body_count; ++k)
{
ymir_particle_proxy* b = p->proxies + k;
for (int c = 0; c < 3; ++c)
{
b->impulse[c] += b->sub_impulse[c];
b->angular_impulse[c] += b->sub_angular[c];
}
}
}
void ymir_particles_end(ymir_particles* p, int32_t substeps)
{
if (p == NULL || p->live_chains == 0)
{
return;
}
float mean = 1.0f / (float)substeps;
for (int32_t i = 0; i < p->particle_count; ++i)

View file

@ -85,10 +85,10 @@ typedef struct ymir_particle_proxy
float angular_velocity[3];
float inv_mass; // 0 for static, kinematic and anchor bodies
float inv_inertia[6]; // world inverse inertia, symmetric: xx xy xz yy yz zz
float impulse[3];
float hard_impulse[3]; // the part from attachments and tethers: finalised into `impulse` now and `carry` next step
float carry[3]; // N s, applied to the body before the next Box3D step
float impulse[3]; // N s, world: the tick's total, the sum of every substep's
float angular_impulse[3];
float sub_impulse[3]; // N s: this substep's, applied by the caller before the body's positions integrate
float sub_angular[3];
} ymir_particle_proxy;
typedef struct ymir_particles
@ -125,7 +125,7 @@ typedef struct ymir_particles
// solver scratch, allocated once at create
float *pp_x, *pp_y, *pp_z, *dp_x, *dp_y, *dp_z;
float *rest_link, *rest_bend, *rest_pos, *tension_acc, *tension_diff;
float *rest_link, *rest_bend, *rest_pos, *tension_acc, *struct_x, *struct_y, *struct_z;
int32_t *skip_body, *cell_x, *cell_y, *cell_z, *cell_start, *cell_fill, *sorted;
uint32_t table_mask;
uint8_t* mark;
@ -147,8 +147,11 @@ int32_t ymir_particles_free_particle_slots(const ymir_particles* p, uint64_t ste
int32_t ymir_particles_free_chain_slots(const ymir_particles* p, uint64_t step_count);
int32_t ymir_particles_free_attachment_slots(const ymir_particles* p, uint64_t step_count);
// One particle phase for a tick of `dt` seconds in `substeps` substeps, after the caller filled
// proxies[0 .. body_count). Returns nothing: it cannot fail once the commands were accepted.
void ymir_particles_step(ymir_particles* p, float dt, int32_t substeps, float gx, float gy, float gz);
// The particle phase runs inside the rigid solver's substeps: begin once per tick (after the caller filled the
// proxies' shape, size, inverse mass and inertia), substep once per rigid substep (after the caller refreshed each
// proxy's pose and velocity; it then applies each proxy's sub_impulse before positions integrate), end once.
int32_t ymir_particles_begin(ymir_particles* p);
void ymir_particles_substep(ymir_particles* p, float h, float gx, float gy, float gz);
void ymir_particles_end(ymir_particles* p, int32_t substeps);
#endif

View file

@ -480,6 +480,8 @@ public sealed class Box3DSoaParticleSourceTests
private static string Body(string source, string signature)
{
var start = source.IndexOf(signature, StringComparison.Ordinal);
while (start >= 0 && source[(start + signature.Length)..].TrimStart().StartsWith(';')) // a prototype, not the definition
start = source.IndexOf(signature, start + 1, StringComparison.Ordinal);
Assert.True(start >= 0, signature);
var open = source.IndexOf("\n{", start, StringComparison.Ordinal);
var end = source.IndexOf("\n}", open, StringComparison.Ordinal);
@ -499,18 +501,38 @@ public sealed class Box3DSoaParticleSourceTests
Assert.Empty(System.Text.RegularExpressions.Regex.Matches(c, @"\bomp\b|#pragma|pthread|_Atomic|atomic_"));
}
private static int Calls(string name) =>
Directory.GetFiles(Native(), "*.c").Where(f => !f.EndsWith("ymir_particles.c")).Concat(Directory.GetFiles(Native(), "*.h").Where(f => !f.EndsWith("ymir_particles.h")))
.Sum(f => System.Text.RegularExpressions.Regex.Matches(File.ReadAllText(f), name + @"\(").Count);
private static int Count(string text, string pattern) => System.Text.RegularExpressions.Regex.Matches(text, pattern).Count;
[Fact]
public void The_particle_phase_has_one_caller_and_the_step_never_splits_or_destroys()
public void The_particle_phase_runs_in_the_substep_callback_and_never_splits_or_destroys()
{
var soa = File.ReadAllText(Path.Combine(Native(), "ymir_box3d_soa.c"));
var c = File.ReadAllText(Path.Combine(Native(), "ymir_particles.c"));
var calls = Directory.GetFiles(Native(), "*.c").Where(f => !f.EndsWith("ymir_particles.c")).Concat(Directory.GetFiles(Native(), "*.h").Where(f => !f.EndsWith("ymir_particles.h")))
.Sum(f => System.Text.RegularExpressions.Regex.Matches(File.ReadAllText(f), @"ymir_particles_step\(").Count);
Assert.Equal(1, calls);
// begin and end once per tick, in the step; the substep once, in the callback Box3D runs inside its own substeps
Assert.Equal(1, Calls("ymir_particles_begin"));
Assert.Equal(1, Calls("ymir_particles_end"));
Assert.Equal(1, Calls("ymir_particles_substep"));
var step = Body(soa, "YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_step(");
Assert.Contains("ymir_particles_step(", step);
var phase = Body(c, "void ymir_particles_step(");
foreach (var text in new[] { step, phase })
Assert.Empty(System.Text.RegularExpressions.Regex.Matches(text, @"split_chain|destroy_chain|detach"));
var callback = Body(soa, "static void ymir_soa_particle_substep(b3WorldId world_id, int sub_step, float h, void* context)");
Assert.Contains("ymir_particles_begin(", step);
Assert.Contains("ymir_particles_end(", step);
Assert.Contains("ymir_particles_substep(", callback);
Assert.Equal(1, Count(soa, @"b3World_SetSubStepCallback\("));
// a body is touched only through the callback's two fork accessors: never pushed after the step, never teleported
Assert.Equal(0, Count(step + callback, @"b3Body_ApplyLinearImpulse|b3Body_ApplyAngularImpulse|b3Body_SetTransform"));
Assert.Equal(1, Count(soa, @"b3Body_GetSubStepState\("));
Assert.Equal(1, Count(soa, @"b3Body_ApplySubStepImpulse\("));
Assert.Contains("b3Body_GetSubStepState(", callback);
Assert.Contains("b3Body_ApplySubStepImpulse(", callback);
var phases = new[] { Body(c, "int32_t ymir_particles_begin("), Body(c, "void ymir_particles_substep("), Body(c, "void ymir_particles_end(") };
foreach (var text in phases.Append(step).Append(callback))
Assert.Equal(0, Count(text, @"split_chain|destroy_chain|detach"));
// the retired owners stay gone
Assert.Equal(0, Count(c, @"carry|hard_impulse|cancel|k_body|end_force_scale|tension_diff|add_link_load|ymir_particles_step"));
Assert.Equal(0, Count(soa, @"carry|ymir_particles_step|ymir_soa_apply_particle_impulses"));
}
}