Map: flesh on the bones (tissue stack, dormant pinning, live budget) and the cost probes behind it

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-25 20:46:12 +01:00
commit ae49a7de8c
9 changed files with 1603 additions and 0 deletions

115
docs/flesh-map.md Normal file
View file

@ -0,0 +1,115 @@
# Flesh on the bones: map
Pass `flesh-bones-map` (Imagination, 2026-11-25). Pinned to Lodur fbc3501, Ymir d82da6d, BadDaySimulator 3d6e735.
Rests on rulings bds-particle-tissues, bds-flesh-at-rest, bds-body-skeleton-particle-flesh-rough, bds-skeleton-19-for-mvp,
bds-cut-location, bds-gut-length, bds-gut-rope-tension, bend-limit-stiffness, ymir-sheet-sieve-at-budget-substeps,
particles-are-constraint-graphs, and on the ideas of pass bds-body-flesh-map (2026-11-23), which this map measures and corrects.
## Target shape
Fella is 19 Ymir rigid bones (the plan's parts). Each part's flesh is one skin sheet and, on the six muscled parts, one muscle
sheet: Ymir particles, distances and bends, each particle attached to its own bone at its rest point. Sheets of neighbouring
parts are stitched across each joint by distances. At rest a part's flesh is dormant: in no Ymir loop at all, drawn from the
bone pose. Lodur's binding wakes one part when that part is disturbed and pins it again when it settles. A wound removes the
sheet edges it crosses; a sever at a joint removes that joint's stitches with the joint. Fat and bone are thicknesses in the
part's tissue stack, not particles. Internal tubes and organs stay meshes until released (follow-ups lodur-organ-release-rope,
lodur-exposed-tube-chains).
### Tissue layers per part (plan data)
| Part (label, side) | Skin sheet | Muscle sheet | Stack only |
| --- | --- | --- | --- |
| Pelvis, Torso | yes | yes | fat, bone; organs contained |
| Head | yes | no (scalp over skull) | fat, bone; brain contained |
| UpperArm, LowerArm, Thigh, Shin (L, R) | yes | yes | fat, bone; vessels, tendons contained |
| Foot, Palm, Thumb, Fingers (L, R) | yes | no | bone, tendons contained |
Authority: the BodyPlan owns each part's tissue stack (layer kind, thickness, and for a sheet layer: spacing, edge and
attachment compliance, tensile strength). Lodur.Core derives the sheet geometry (rest points in the part frame, edges, bends,
joint stitches) from the part's shape and stack, deterministically, at build. Derived geometry is never stored: a stored copy
would be a cache that could disagree with the stack. The plan lives in BodyPlans.Humanoid until lodur-body-plan-cultcache.
## Authority map
- Owner of flesh meaning (which part, which layer, awake or dormant, wound, sever): Lodur's binding (Lodur.Body.Ymir), from
plan data and physiology insults.
- Owner of flesh physics (positions, constraint solve, tension and attachment-force sensors, components): Ymir.
- Inputs to the binding's wake rule: contact impulse lanes on the part's bone, insults on the part, wounds, severs,
evisceration; for re-pinning, attachment force times compliance (the deviation; no new lane).
- Outputs: Ymir commands (create in batches, set pinned, remove distances, destroy particles); Tear insults from DistanceTension.
- Derived: sheet geometry (from the stack), render embedding weights (BDS client, at load), components and tethers (Ymir),
the active lists of the pin cut (Ymir, at command time), the per-particle held-body list and limb skip (Ymir, at command time).
- Forbidden writers: Ymir never wakes, pins, tears or splits on its own; BDS never commands flesh except through the session;
no per-tick writes of dormant particles.
- Demotions: "pinned = inverse mass 0 in every constraint" (follow-up ymir-particle-pinning as admitted) is no longer the
mechanism; dormant particles leave the loops. held_by's scan over every attachment is no longer an owner; the held list is
derived at command time.
## Body facts (probes)
Probe sources: `<scratchpad>/imag-flesh/fella.c`, `sieve.c`, and a copy of Ymir d82da6d `ymir_particles.c` with opt-in probe
patches (pinned skip flags, a body group filter in place of held_by, flesh out of the solid contact grid, phase timers; the
unpatched paths are byte-for-byte the shipped code). gcc 13.2 -O2 -ffp-contract=off, one core, 120 Hz x 4 substeps. Bones are
kinematic proxies driven by forward kinematics of BodyPlans.Humanoid (sways at 3 rad/s); Box3D's own step is not included.
P1 Counts per Fella, sampler at skin 10 cm / muscle 14 cm with a ring floor of 4: 397 skin + 264 muscle = 661 particles,
3,123 distances (383 joint stitches), 434 bends, 661 attachments. At 8 / 10 cm with a ring floor of 6: 619 + 414 = 1,033,
4,742 distances (527 stitches), 736 bends. Torso skin 110 and pelvis 54 at 8 cm; hands 76 together.
P2 Per-tick cost, median of 5 interleaved runs of 600 ticks (`matrix2.sh`, 10/14 cm config):
| Scene | Live particles | ms/tick |
| --- | --- | --- |
| A 1 Fella pinned as mass 0 in the solve (shipped code) | 0 | 4.918 |
| B 1 Fella pinned by skip flags | 0 | 0.255 |
| H 4 Fellas pinned by skip flags | 0 | 1.023 |
| D 1 live forearm, rest dormant (absent) | 40 | 0.204 |
| E 1 Fella live, flesh out of particle contact, limb skip | 661 | 1.036 |
| F E plus a released 8 m gut (300 particles, self-colliding) | 961 | 2.359 |
| G E with flesh in the solid contact walk | 661 | 5.441 |
| I E with held_by's attachment scan instead of the limb skip | 661 | 5.280 |
| J 4 Fellas' bones and capacity, 1 Fella live, rest absent | 661 | 2.271 |
| K J plus the gut | 961 | 4.262 |
Phase split (fella_prof): a live Fella in the contact walk spends 4.4 of 5.4 ms in particle_contacts walking pairs of its
own group that never collide. With nothing live, 0.6 ms of 4 Fellas' 1.2 ms is grid_sort clearing a bucket table sized 2x
particle capacity (32,768 buckets). body_contacts is live particles x every body: 0.49 ms for 232 particles x 77 bodies.
P3 Command cost (one call each, 4 Fellas + gut, 8/10 cm): remove 24 skin edges 0.59-0.65 ms, remove 32 elbow stitches
0.61-0.66 ms (one outlier 10.3 ms); 1 Fella + gut at 10/14 cm: 0.12-0.47 and 0.11-0.17 ms. Every remove, attach and
create_distances runs rederive_topology over the whole world. Waking a part (restoring inverse mass) 2-10 us.
P4 Spawn with one attach per call (the only attach Ymir has): 1 Fella 147 ms, 4 Fellas 2.3 s (quadratic in world size).
P5 Joint rings: with held_by, neighbouring bones of the same Fella push flesh at the joints: max live deviation from the
attachment point 0.064 m against 0.023 m with the limb skip (8/10 cm, all live).
P6 Sieve (`sieve.c`): a skin sheet at 8 cm (36 particles, r 0.036, 96 edges) attached along a static capsule (r 0.07,
half 0.18, horizontal) under a 7x7x7 fluid pour or a 4x4x4 solid block, 3 s at 4 substeps:
| Load, rate | Worst edge stretch | Deep tick x pairs | Load inside the bone | Skin mean axis distance |
| --- | --- | --- | --- | --- |
| fluid 60 Hz | 0.0204 | 35 | 0 | 0.0700 |
| fluid 120 Hz | 0.0221 | 2 | 0 | 0.0700 |
| solid 60 Hz | 0.0275 | 2 | 0 | 0.0700 |
| solid 120 Hz | 0.0321 | 0 | 0 | 0.0700 |
| mutant: attachments off (compliance 1e3), fluid 120 Hz | 0.0566 | 0 | 0 | 0.4016 |
The body-backed skin does not sieve: it holds its place within 0.1 mm and stretches 2-3%. 96 of 343 fluid particles reach the
bone surface between skin particles, which is where the drawn skin is; none enters the bone. The corner-pinned tarp's sieve
is a limit of loose cloth, not of BDS skin.
## Cut order
1. cut-ymir-skin-sieve-test (Ymir, test and doc). Beside it: 2. cut-ymir-particle-limb-contact (Ymir: held list derived at
command time, LIMB attachment flag skips the attached body's limb tree).
3. ymir-particle-pinning, revised: dormant particles out of every loop through command-time active lists; pinned particles
bordering live ones are kinematic per substep; one-way attachment flag; wake writes position and velocity from the target.
4. ymir-attach-batch (spawn) and, after question ymir-flesh-particle-contact, ymir-flesh-no-particle-contact.
5. lodur-tissue-stack (plan data and the deterministic sheet builder; can start any time).
6. lodur-body-sheets-graph (binding: build dormant, wake per part, re-pin, wounds and severs as commands), after lodur-physiology.
7. bds-flesh-render-embedding; then lodur-organ-release-rope and lodur-exposed-tube-chains.
Budget reading: at rest flesh must cost nothing per tick (P2 B, H); one hurt part costs about 0.2 ms; a whole live Fella about
1 ms alone and 2.3 ms in a 4-Fella world; an eviscerated gut adds 1.3 to 2 ms. The rope scene already takes about 2 ms, so a
whole-body event on top of it overruns: question bds-flesh-live-budget.

View file

@ -0,0 +1,18 @@
# Flesh cost probes
Probes behind `docs/flesh-map.md` and the Nibu research notes
`lodur:research:flesh-bones-map.probe-flesh-cost`, `.probe-flesh-commands` and
`.probe-skin-sieve`.
- `ymir_particles.probe.patch`: the probe's changes to Ymir's
`native/Ymir.Box3D/ymir_particles.{c,h}` at GameCult/Ymir d82da6d (pin
skipping by flag, a no-contact phase switch). Probe only; not a Ymir change.
- `fella.c`: one or more Fellas of skin and muscle sheets on rigid bones,
stepped at 120 Hz x 4 substeps. `matrix.sh` / `matrix2.sh` run the
configurations interleaved, five repetitions; `matrix*.out` are the runs the
notes cite.
- `sieve.c`: a skin sheet attached along a bone under fluid and solid load at
4 substeps.
Build against a patched copy of the two Ymir files with the same flags Ymir
uses (`-O2 -std=c17 -ffp-contract=off`).

502
docs/probes/flesh/fella.c Normal file
View file

@ -0,0 +1,502 @@
// Probe: Fella flesh on Ymir's particle solver (ymir_particles.c at d82da6d, scratch copy with two opt-in patches:
// probe_pinned excludes pinned particles from grid and constraints; probe_body_group replaces held_by with a group filter).
// Bones are kinematic proxies animated by FK (one-way coupling). 120 Hz x 4 substeps.
// usage: fella F skin_m muscle_m live pinmode filter gut ticks [cut]
// live: 0 none, 1 left forearm of each Fella, 2 all of Fella 0, 3 everything
// pinmode: 0 pinned = mass 0 in the solve (shipped code), 1 pinned excluded (patch)
// filter: 0 held_by (shipped), 1 body group filter (patch)
#include "ymir_particles.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
extern uint8_t* probe_pinned;
extern int32_t* probe_body_group;
extern double probe_t[8];
extern int probe_nocontact;
typedef struct { float x, y, z; } v3;
typedef struct { float x, y, z, w; } q4;
static v3 V(float x, float y, float z) { return (v3){x, y, z}; }
static v3 add(v3 a, v3 b) { return V(a.x + b.x, a.y + b.y, a.z + b.z); }
static v3 sub(v3 a, v3 b) { return V(a.x - b.x, a.y - b.y, a.z - b.z); }
static v3 mul(v3 a, float s) { return V(a.x * s, a.y * s, a.z * s); }
static float dot(v3 a, v3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
static float len(v3 a) { return sqrtf(dot(a, a)); }
static v3 cross(v3 a, v3 b) { return V(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x); }
static q4 qmul(q4 a, q4 b)
{
return (q4){a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y, a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x,
a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w, a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z};
}
static v3 qrot(q4 q, v3 v)
{
v3 u = V(q.x, q.y, q.z);
v3 t = mul(cross(u, v), 2.0f);
return add(add(v, mul(t, q.w)), cross(u, t));
}
static q4 qaxis(v3 a, float ang) { float s = sinf(ang / 2); return (q4){a.x * s, a.y * s, a.z * s, cosf(ang / 2)}; }
// The 19-part humanoid of Lodur BodyPlans.Humanoid (fbc3501), rotations taken as identity.
enum { BOX, CAP, SPH };
typedef struct { int shape; v3 size, pos; } part;
typedef struct { int parent, child; v3 anchor, axis; float amp, phase; } joint;
static part P[19];
static joint J[18];
static int np = 0, nj = 0;
static int addp(int sh, v3 sz, v3 pos) { P[np] = (part){sh, sz, pos}; return np++; }
static void addj(int pa, int ch, v3 an, v3 ax, float amp) { J[nj] = (joint){pa, ch, an, ax, amp, 0.7f * nj}; nj++; }
static int LOWERARM_L;
static void humanoid(void)
{
int pelvis = addp(BOX, V(.15f, .08f, .09f), V(0, 1.11f, 0));
int torso = addp(BOX, V(.15f, .22f, .09f), V(0, 1.41f, 0));
int head = addp(SPH, V(.11f, 0, 0), V(0, 1.74f, 0));
int ua[2], la[2], th[2], sh[2], ft[2], pa[2], tb[2], fg[2];
for (int k = 0; k < 2; k++)
{
float sx = k == 0 ? 1.f : -1.f;
ua[k] = addp(CAP, V(.045f, .11f, 0), V(sx * .21f, 1.395f, 0));
la[k] = addp(CAP, V(.04f, .10f, 0), V(sx * .21f, 1.10f, 0));
th[k] = addp(CAP, V(.07f, .18f, 0), V(sx * .09f, .79f, 0));
sh[k] = addp(CAP, V(.06f, .17f, 0), V(sx * .09f, .31f, 0));
ft[k] = addp(BOX, V(.05f, .04f, .12f), V(sx * .09f, .04f, .05f));
}
for (int k = 0; k < 2; k++)
{
float sx = k == 0 ? 1.f : -1.f;
pa[k] = addp(BOX, V(.014f, .045f, .040f), V(sx * .21f, .930f, 0));
tb[k] = addp(CAP, V(.011f, .03f, 0), V(sx * .206f, .93f, .055f));
fg[k] = addp(BOX, V(.011f, .0425f, .038f), V(sx * .21f - sx * .0145f, .845f, 0));
}
LOWERARM_L = la[0];
v3 up = V(0, 1, 0), ac = V(1, 0, 0);
addj(pelvis, torso, V(0, 1.19f, 0), ac, .15f);
addj(torso, head, V(0, 1.63f, 0), ac, .2f);
for (int k = 0; k < 2; k++)
{
float sx = k == 0 ? 1.f : -1.f;
addj(torso, ua[k], V(sx * .21f, 1.55f, 0), ac, .6f);
addj(ua[k], la[k], V(sx * .21f, 1.24f, 0), ac, .7f);
addj(pelvis, th[k], V(sx * .09f, 1.04f, 0), ac, .5f);
addj(th[k], sh[k], V(sx * .09f, .54f, 0), ac, .6f);
addj(sh[k], ft[k], V(sx * .09f, .08f, 0), ac, .3f);
}
for (int k = 0; k < 2; k++)
{
float sx = k == 0 ? 1.f : -1.f;
addj(la[k], pa[k], V(sx * .21f, .975f, 0), ac, .4f);
addj(pa[k], tb[k], V(sx * .206f, .955f, .03f), ac, .3f);
addj(pa[k], fg[k], V(sx * .21f, .885f, 0), ac, .5f);
}
(void)up;
}
// signed distance to a part's shape in its local frame
static float sdf(const part* p, v3 x)
{
if (p->shape == SPH) return len(x) - p->size.x;
if (p->shape == CAP)
{
float y = x.y < -p->size.y ? -p->size.y : x.y > p->size.y ? p->size.y : x.y;
return len(V(x.x, x.y - y, x.z)) - p->size.x;
}
v3 q = V(fabsf(x.x) - p->size.x, fabsf(x.y) - p->size.y, fabsf(x.z) - p->size.z);
v3 m = V(q.x > 0 ? q.x : 0, q.y > 0 ? q.y : 0, q.z > 0 ? q.z : 0);
float in = fmaxf(q.x, fmaxf(q.y, q.z));
return len(m) + (in < 0 ? in : 0);
}
// surface samples of a part at spacing s, inset d (local frame)
static int sample(const part* p, float s, float d, v3* out)
{
int n = 0;
if (p->shape == SPH || p->shape == CAP)
{
float r = p->size.x - d;
if (r < 0.005f) r = 0.005f;
float half = p->shape == CAP ? p->size.y : 0;
int rings = (int)ceilf(2 * half / s);
for (int i = 0; i <= rings && half > 0; i++)
{
float y = -half + 2 * half * i / (rings ? rings : 1);
int m = (int)fmaxf(getenv("MINRING") ? atoi(getenv("MINRING")) : 6, roundf(2 * 3.14159f * r / s));
for (int k = 0; k < m; k++)
{
float a = 2 * 3.14159f * (k + 0.5f * (i & 1)) / m;
out[n++] = V(r * cosf(a), y, r * sinf(a));
}
}
// caps (whole sphere for SPH): latitude rings
int lat = (int)fmaxf(2, roundf(3.14159f * r / 2 / s));
for (int c = 0; c < 2; c++)
for (int i = (half > 0 ? 1 : 0); i <= lat; i++)
{
if (half == 0 && c == 1 && i == 0) continue;
float phi = 3.14159f / 2 * i / lat; // 0 at equator
float rr = r * cosf(phi), yy = r * sinf(phi) * (c ? -1 : 1);
int m = i == lat ? 1 : (int)fmaxf(3, roundf(2 * 3.14159f * rr / s));
for (int k = 0; k < m; k++)
{
float a = 2 * 3.14159f * (k + 0.5f * (i & 1)) / m;
out[n++] = V(rr * cosf(a), (c ? -half : half) + yy, rr * sinf(a));
}
}
return n;
}
v3 h = V(fmaxf(p->size.x - d, .004f), fmaxf(p->size.y - d, .004f), fmaxf(p->size.z - d, .004f));
for (int ax = 0; ax < 3; ax++)
for (int sg = -1; sg <= 1; sg += 2)
{
float hu = ax == 0 ? h.y : h.x, hv = ax == 2 ? h.y : h.z;
int nu = (int)fmaxf(1, ceilf(2 * hu / s)), nv = (int)fmaxf(1, ceilf(2 * hv / s));
for (int i = 0; i <= nu; i++)
for (int k = 0; k <= nv; k++)
{
// skip the shared edges of faces already done
float u = -hu + 2 * hu * i / nu, v = -hv + 2 * hv * k / nv;
v3 x = ax == 0 ? V(sg * h.x, u, v) : ax == 1 ? V(u, sg * h.y, v) : V(u, v, sg * h.z);
int dup = 0;
for (int q = 0; q < n; q++) if (len(sub(out[q], x)) < 0.3f * s) { dup = 1; break; }
if (!dup) out[n++] = x;
}
}
return n;
}
// ---- world ----
static ymir_particles* W;
static int F; // Fellas
static uint64_t stepc = 1;
typedef struct { int fella, part, layer; v3 local; } pinfo;
static pinfo* PI;
static float* pmass;
static int nparticles = 0;
static ymir_handle* PH;
static int ndist = 0, nbend = 0, natt = 0, nstitch = 0;
static ymir_handle* DH; static int* Dlayer; static int* Dpart; static int* Dfella;
static int nfloor;
static v3 bonepos[64][19]; static q4 bonerot[64][19];
static void pose(int f, float t, v3* pos, q4* rot)
{
v3 off = V(1.2f * f, 0, 0);
q4 R[19]; v3 X[19];
R[0] = qaxis(V(0, 1, 0), 0.3f * sinf(1.3f * t));
X[0] = add(add(P[0].pos, off), V(0, 0.02f * sinf(6 * t), 0));
for (int j = 0; j < nj; j++)
{
joint* J0 = &J[j];
int a = J0->parent, c = J0->child;
q4 rj = qaxis(J0->axis, J0->amp * sinf(3.0f * t + J0->phase + f));
R[c] = qmul(R[a], rj);
v3 an = add(X[a], qrot(R[a], sub(J0->anchor, P[a].pos)));
X[c] = add(an, qrot(R[c], sub(P[c].pos, J0->anchor)));
}
for (int i = 0; i < 19; i++) { pos[i] = X[i]; rot[i] = R[i]; }
}
static void fill_proxies(float t, float h)
{
for (int f = 0; f < F; f++)
{
v3 x0[19], x1[19]; q4 r0[19], r1[19];
pose(f, t, x0, r0);
pose(f, t + h, x1, r1);
for (int i = 0; i < 19; i++)
{
ymir_particle_proxy* b = W->proxies + f * 19 + i;
memset(b, 0, sizeof *b);
b->shape = P[i].shape == SPH ? YMIR_PROXY_SPHERE : P[i].shape == CAP ? YMIR_PROXY_CAPSULE : YMIR_PROXY_BOX;
b->size[0] = P[i].size.x; b->size[1] = P[i].size.y; b->size[2] = P[i].size.z;
b->position[0] = x0[i].x; b->position[1] = x0[i].y; b->position[2] = x0[i].z;
b->rotation[0] = r0[i].x; b->rotation[1] = r0[i].y; b->rotation[2] = r0[i].z; b->rotation[3] = r0[i].w;
v3 v = mul(sub(x1[i], x0[i]), 1 / h);
b->velocity[0] = v.x; b->velocity[1] = v.y; b->velocity[2] = v.z;
q4 inv0 = {-r0[i].x, -r0[i].y, -r0[i].z, r0[i].w};
q4 dq = qmul(r1[i], inv0);
if (dq.w < 0) { dq.x = -dq.x; dq.y = -dq.y; dq.z = -dq.z; dq.w = -dq.w; }
b->angular_velocity[0] = 2 * dq.x / h; b->angular_velocity[1] = 2 * dq.y / h; b->angular_velocity[2] = 2 * dq.z / h;
bonepos[f][i] = x0[i]; bonerot[f][i] = r0[i];
}
}
ymir_particle_proxy* fl = W->proxies + F * 19;
memset(fl, 0, sizeof *fl);
fl->shape = YMIR_PROXY_BOX; fl->size[0] = 50; fl->size[1] = 0.5f; fl->size[2] = 50; fl->position[1] = -0.5f; fl->rotation[3] = 1;
W->body_count = F * 19 + 1;
}
static double now_ms(void)
{
LARGE_INTEGER c, fq; QueryPerformanceCounter(&c); QueryPerformanceFrequency(&fq);
return 1000.0 * (double)c.QuadPart / (double)fq.QuadPart;
}
int main(int argc, char** argv)
{
F = argc > 1 ? atoi(argv[1]) : 1;
float s = argc > 2 ? (float)atof(argv[2]) : 0.08f, sm = argc > 3 ? (float)atof(argv[3]) : 0.10f;
int live = argc > 4 ? atoi(argv[4]) : 0, pinmode = argc > 5 ? atoi(argv[5]) : 0, filter = argc > 6 ? atoi(argv[6]) : 0;
int gut = argc > 7 ? atoi(argv[7]) : 0, ticks = argc > 8 ? atoi(argv[8]) : 600, cut = argc > 9 ? atoi(argv[9]) : 0;
humanoid();
int cap = 4000 * F + 400;
ymir_particle_capacities c = {cap, cap * 6, cap * 4, cap * 2, cap * 2, 4, 0, 0, 0};
W = ymir_particles_create(&c, F * 19 + 1);
PI = calloc(cap, sizeof *PI); pmass = calloc(cap, sizeof *pmass); PH = calloc(cap, sizeof *PH);
DH = calloc(cap * 6, sizeof *DH); Dlayer = calloc(cap * 6, sizeof(int)); Dpart = calloc(cap * 6, sizeof(int)); Dfella = calloc(cap * 6, sizeof(int));
uint8_t* pinned = calloc(cap, 1);
int32_t* bgroup = calloc(F * 19 + 1, sizeof(int32_t));
for (int f = 0; f < F; f++) for (int i = 0; i < 19; i++) bgroup[f * 19 + i] = f + 1;
bgroup[F * 19] = -1;
fill_proxies(0, 1.f / 480);
double build0 = now_ms();
v3* tmp = malloc(sizeof(v3) * 4000);
int per_layer[2] = {0, 0};
for (int f = 0; f < F; f++)
{
int first[2][19], cnt[2][19];
for (int layer = 0; layer < 2; layer++)
{
float sp = layer == 0 ? s : sm, d = layer == 0 ? 0 : 0.012f;
float mass = layer == 0 ? 0.012f * (sp * sp / 0.0064f) : 0.05f, rad = 0.45f * sp;
for (int i = 0; i < 19; i++)
{
first[layer][i] = nparticles; cnt[layer][i] = 0;
if (layer == 1 && (P[i].shape == BOX && P[i].size.x < 0.02f)) continue; // no muscle sheet in palm/fingers
if (layer == 1 && P[i].size.x < 0.02f) continue;
int n = sample(&P[i], sp, d, tmp);
for (int k = 0; k < n; k++)
{
v3 w = add(P[i].pos, tmp[k]);
int inside = 0;
for (int o = 0; o < 19; o++) if (o != i && sdf(&P[o], sub(w, P[o].pos)) < -0.25f * sp) { inside = 1; break; }
if (inside) continue;
v3 wp = add(bonepos[f][i], qrot(bonerot[f][i], tmp[k]));
int live_p = live == 3 || (live == 2 && f == 0) || (live == 1 && i == LOWERARM_L);
if (!live_p && getenv("ABSENT")) continue;
ymir_particle_desc pd = {{wp.x, wp.y, wp.z}, {w.x + 1.2f * f, w.y, w.z}, {0, 0, 0}, mass, rad, (uint32_t)(f + 1), 0};
pd.mass = live_p ? mass : 0.0f;
if (ymir_particles_create_particles(W, 1, &pd, stepc, &PH[nparticles]) != 0) { printf("create failed\n"); return 1; }
PI[nparticles] = (pinfo){f, i, layer, tmp[k]};
pmass[nparticles] = mass;
pinned[PH[nparticles].lane] = !live_p;
nparticles++; cnt[layer][i]++; if (f == 0) per_layer[layer]++;
}
}
}
if (f == 0 && getenv("PARTS")) { for (int i = 0; i < 19; i++) printf("part %d skin %d muscle %d;", i, cnt[0][i], cnt[1][i]); }
// edges within a part and layer: 6 nearest within 1.6 spacing; bends over near-opposite pairs
for (int layer = 0; layer < 2; layer++)
{
float sp = layer == 0 ? s : sm;
for (int i = 0; i < 19; i++)
{
int a0 = first[layer][i], n = cnt[layer][i];
int* nb = calloc((size_t)n * 6, sizeof(int)); int* nbe = calloc((size_t)n * 6, sizeof(int)); int* nn = calloc(n, sizeof(int));
for (int a = 0; a < n; a++)
{
int best[6]; float bd[6]; int nb6 = 0;
for (int b = 0; b < n; b++)
{
if (b == a) continue;
float dd = len(sub(PI[a0 + a].local, PI[a0 + b].local));
if (dd > 1.6f * sp) continue;
int q;
if (nb6 < 6) q = nb6++;
else { if (bd[5] <= dd) continue; q = 5; }
best[q] = b; bd[q] = dd;
for (int z = q; z > 0 && bd[z] < bd[z - 1]; z--) { float t = bd[z]; bd[z] = bd[z - 1]; bd[z - 1] = t; int u = best[z]; best[z] = best[z - 1]; best[z - 1] = u; }
}
for (int q = 0; q < nb6; q++)
{
int b = best[q];
if (b < a) { int seen = 0; for (int z = 0; z < nn[b]; z++) if (nb[b * 6 + z] == a) seen = 1; if (seen) continue; }
ymir_distance_desc dd = {PH[a0 + a], PH[a0 + b], -1.0f, layer == 0 ? 2e-4f : 1e-4f};
{ int rc = ymir_particles_create_distances(W, 1, &dd, stepc, &DH[ndist]); if (rc) { printf("dist failed rc %d layer %d part %d a %d b %d n %d lanes %d %d\n", rc, layer, i, a, b, n, PH[a0+a].lane, PH[a0+b].lane); return 1; } }
Dlayer[ndist] = layer; Dpart[ndist] = i; Dfella[ndist] = f;
if (nn[a] < 6) { nb[a * 6 + nn[a]] = b; nbe[a * 6 + nn[a]] = ndist; nn[a]++; }
if (nn[b] < 6) { nb[b * 6 + nn[b]] = a; nbe[b * 6 + nn[b]] = ndist; nn[b]++; }
ndist++;
}
}
for (int b = 0; b < n; b++)
for (int x = 0; x < nn[b]; x++)
for (int y = x + 1; y < nn[b]; y++)
{
v3 u = sub(PI[a0 + nb[b * 6 + x]].local, PI[a0 + b].local), v = sub(PI[a0 + nb[b * 6 + y]].local, PI[a0 + b].local);
if (dot(u, v) / (len(u) * len(v)) > -0.85f) continue;
ymir_bend_desc bd = {DH[nbe[b * 6 + x]], DH[nbe[b * 6 + y]], 1e-3f, -1.0f};
ymir_handle hb;
if (ymir_particles_create_bends(W, 1, &bd, stepc, &hb) == 0) nbend++;
}
free(nb); free(nbe); free(nn);
}
}
// stitches across each joint (skin and muscle): child particles within 1.5 sp of the anchor to the nearest parent particle within 2 sp
for (int layer = 0; layer < 2; layer++)
{
float sp = layer == 0 ? s : sm;
for (int j = 0; j < nj; j++)
{
int pa = J[j].parent, ch = J[j].child;
for (int a = first[layer][ch]; a < first[layer][ch] + cnt[layer][ch]; a++)
{
v3 wa = add(P[ch].pos, PI[a].local);
if (len(sub(wa, J[j].anchor)) > 1.5f * sp + P[ch].size.x) continue;
int best = -1; float bd = 2.0f * sp;
for (int b = first[layer][pa]; b < first[layer][pa] + cnt[layer][pa]; b++)
{
float dd = len(sub(wa, add(P[pa].pos, PI[b].local)));
if (dd < bd) { bd = dd; best = b; }
}
if (best < 0) continue;
ymir_distance_desc dd = {PH[a], PH[best], -1.0f, 2e-4f};
if (ymir_particles_create_distances(W, 1, &dd, stepc, &DH[ndist]) != 0) { printf("stitch failed\n"); return 1; }
Dlayer[ndist] = 2 + layer; Dpart[ndist] = ch; Dfella[ndist] = f; ndist++; nstitch++;
}
}
}
// skin to nearest muscle of the same part
for (int i = 0; i < 19; i++)
for (int a = first[0][i]; a < first[0][i] + cnt[0][i]; a++)
{
int best = -1; float bd = 1e9f;
for (int b = first[1][i]; b < first[1][i] + cnt[1][i]; b++) { float dd = len(sub(PI[a].local, PI[b].local)); if (dd < bd) { bd = dd; best = b; } }
if (best < 0) continue;
ymir_distance_desc dd = {PH[a], PH[best], -1.0f, 5e-4f};
if (ymir_particles_create_distances(W, 1, &dd, stepc, &DH[ndist]) != 0) { printf("sm failed\n"); return 1; }
Dlayer[ndist] = 4; Dpart[ndist] = i; Dfella[ndist] = f; ndist++;
}
}
// attachments: every flesh particle to its bone point
for (int k = 0; k < nparticles; k++)
{
ymir_attachment_desc ad = {YMIR_ATTACH_BODY, 0, PH[k], PI[k].fella * 19 + PI[k].part, {PI[k].local.x, PI[k].local.y, PI[k].local.z}, PI[k].layer == 0 ? 1e-3f : 1e-4f, 0};
ymir_handle ha;
if (ymir_particles_attach(W, &ad, W->body_count, stepc, &ha) != 0) { printf("attach failed\n"); return 1; }
natt++;
}
int flesh = nparticles;
double build_ms = now_ms() - build0;
// gut: a released real-length rope of 300 particles hanging from the pelvis of Fella 0
int gut0 = nparticles, ngut = gut ? 300 : 0;
ymir_handle* GE = calloc(400, sizeof *GE);
for (int k = 0; k < ngut; k++)
{
float a = k * 0.18f, r = 0.05f + 0.0012f * k;
v3 x = V(r * cosf(a), 0.9f, 0.25f + r * sinf(a));
ymir_particle_desc pd = {{x.x, x.y, x.z}, {0.0267f * k, 0, 0}, {0, 0, 0}, 0.0267f * 0.0003f * 1050.0f / 0.0267f * 0.0267f, 0.0125f, 99u | YMIR_PHASE_SELF_COLLIDE | YMIR_PHASE_SELF_FILTER, 0};
pd.mass = 0.008f;
ymir_particles_create_particles(W, 1, &pd, stepc, &PH[nparticles]);
PI[nparticles] = (pinfo){0, -1, 9, x}; nparticles++;
}
int ngutd = 0;
for (int k = 1; k < ngut; k++)
{
ymir_distance_desc dd = {PH[gut0 + k - 1], PH[gut0 + k], 0.0267f, 0};
ymir_particles_create_distances(W, 1, &dd, stepc, &GE[k - 1]); ngutd++;
}
for (int k = 1; k + 1 < ngut; k++) { ymir_bend_desc bd = {GE[k - 1], GE[k], 1e-2f, -1}; ymir_handle hb; ymir_particles_create_bends(W, 1, &bd, stepc, &hb); }
if (ngut)
{
for (int e = 0; e < 2; e++)
{
int k = e ? ngut - 1 : 0;
ymir_attachment_desc ad = {YMIR_ATTACH_BODY, YMIR_ATTACH_FLAG_TETHER, PH[gut0 + k], 0, {0.02f * (e ? 1 : -1), -0.02f, 0.09f}, 0, 0};
ymir_handle ha; ymir_particles_attach(W, &ad, W->body_count, stepc, &ha);
}
}
if (pinmode) probe_pinned = pinned;
if (getenv("NOCONTACT")) probe_nocontact = 1;
if (filter) probe_body_group = bgroup;
if (filter && ngut) bgroup[0] = 0; // the gut (group 99) collides with every bone; nothing to do
// per-Fella counts
int fd = 0, fs = 0;
for (int e = 0; e < ndist; e++) if (Dfella[e] == 0) { fd++; if (Dlayer[e] >= 2 && Dlayer[e] <= 3) fs++; }
float h = 1.0f / 480.0f;
double* tk = malloc(sizeof(double) * (ticks + 240));
float t = 0;
int warm = 120;
double cut_ms = -1, sever_ms = -1, wake_ms = -1;
float maxdev = 0;
for (int n = 0; n < warm + ticks; n++)
{
// pinned particles are written from their bone at the tick's start (Ymir's pin write, one per tick)
double t0 = now_ms();
fill_proxies(t, h);
for (int k = 0; k < flesh; k++)
{
int l = PH[k].lane;
if (!pinned[l]) continue;
v3 w = add(bonepos[PI[k].fella][PI[k].part], qrot(bonerot[PI[k].fella][PI[k].part], PI[k].local));
W->pos_x[l] = w.x; W->pos_y[l] = w.y; W->pos_z[l] = w.z;
}
ymir_particles_begin(W);
for (int sstep = 0; sstep < 4; sstep++)
{
if (sstep) fill_proxies(t + sstep * h, h);
ymir_particles_substep(W, h, 0, -9.81f, 0);
}
ymir_particles_end(W, 4);
double t1 = now_ms();
t += 4 * h;
stepc++;
if (n >= warm) tk[n - warm] = t1 - t0;
if (cut && n == warm + ticks / 2)
{
// wound: remove the skin edges of Fella 0's left forearm within 6 cm of its front point; then sever at the left elbow (stitches)
ymir_handle hs[64]; int m = 0;
for (int e = 0; e < ndist && m < 64; e++)
if (Dfella[e] == 0 && Dpart[e] == LOWERARM_L && Dlayer[e] == 0)
{
int a = W->dist_a[DH[e].lane];
v3 x = V(W->pos_x[a], W->pos_y[a], W->pos_z[a]);
v3 c0 = add(bonepos[0][LOWERARM_L], qrot(bonerot[0][LOWERARM_L], V(0, 0, .04f)));
if (len(sub(x, c0)) < 0.06f) hs[m++] = DH[e];
}
double c0 = now_ms();
ymir_particles_remove(W, YMIR_LANES_DISTANCE, m, hs, stepc);
cut_ms = now_ms() - c0;
printf("cut: %d skin edges\n", m);
m = 0;
for (int e = 0; e < ndist && m < 64; e++) if (Dfella[e] == 0 && Dpart[e] == LOWERARM_L && (Dlayer[e] == 2 || Dlayer[e] == 3)) hs[m++] = DH[e];
c0 = now_ms();
ymir_particles_remove(W, YMIR_LANES_DISTANCE, m, hs, stepc);
sever_ms = now_ms() - c0;
printf("sever: %d stitches; forearm skin component %d, upper arm skin component %d\n", m,
W->component[PH[0].lane], 0);
// wake Fella 0's thigh flesh: pinned -> live (mass back, velocity of the bone point)
c0 = now_ms();
for (int k = 0; k < flesh; k++)
if (PI[k].fella == 0 && PI[k].part == 7)
{
int l = PH[k].lane;
if (!pinned[l]) continue;
pinned[l] = 0; W->inv_mass[l] = 1.0f / pmass[k];
}
wake_ms = now_ms() - c0;
}
if (n == warm + ticks - 1)
for (int k = 0; k < flesh; k++)
{
int l = PH[k].lane;
if (pinned[l]) continue;
v3 w = add(bonepos[PI[k].fella][PI[k].part], qrot(bonerot[PI[k].fella][PI[k].part], PI[k].local));
float dv = len(sub(V(W->pos_x[l], W->pos_y[l], W->pos_z[l]), w));
if (dv > maxdev) maxdev = dv;
}
}
// median and mean
int nt = ticks;
for (int a = 1; a < nt; a++) { double v = tk[a]; int b = a - 1; while (b >= 0 && tk[b] > v) { tk[b + 1] = tk[b]; b--; } tk[b + 1] = v; }
double mean = 0; for (int a = 0; a < nt; a++) mean += tk[a]; mean /= nt;
int livec = 0; for (int k = 0; k < nparticles; k++) livec += !pinned[PH[k].lane] || k >= flesh;
printf("FELLA F=%d skin=%.3f muscle=%.3f live=%d pinmode=%d filter=%d gut=%d | per Fella: skin %d muscle %d particles, %d distances (%d stitches), bends total %d, attachments total %d | live particles %d | ms/tick median %.3f mean %.3f p95 %.3f max %.3f | max live deviation %.3f m",
F, s, sm, live, pinmode, filter, gut, per_layer[0], per_layer[1], fd, fs, nbend, natt, livec, tk[nt / 2], mean, tk[(int)(nt * 0.95)], tk[nt - 1], maxdev);
printf(" | phases ms/tick grid %.3f pcontact %.3f att %.3f dist %.3f bend %.3f tether %.3f body %.3f", probe_t[0]/(warm+ticks), probe_t[1]/(warm+ticks), probe_t[2]/(warm+ticks), probe_t[3]/(warm+ticks), probe_t[4]/(warm+ticks), probe_t[5]/(warm+ticks), probe_t[6]/(warm+ticks));
if (cut) printf(" | cut %.3f ms sever %.3f ms wake %.4f ms", cut_ms, sever_ms, wake_ms);
printf("\n");
printf("BUILD %.1f ms;", build_ms); return 0;
}

View file

@ -0,0 +1,50 @@
1 ship_1pin median 5.345
1 ship_4pin median 21.693
1 pin_1 median 0.242
1 pin_4 median 1.167
1 nc_4_forearms median 1.358
1 nc_4_one_live median 3.038
1 nc_4_one_live_gut median 3.572
1 c_4_one_live median 6.990
1 nc_4_all_live median 7.416
1 nc_1_live median 1.127
2 ship_1pin median 4.701
2 ship_4pin median 20.071
2 pin_1 median 0.245
2 pin_4 median 1.026
2 nc_4_forearms median 1.397
2 nc_4_one_live median 2.712
2 nc_4_one_live_gut median 3.827
2 c_4_one_live median 6.973
2 nc_4_all_live median 7.499
2 nc_1_live median 1.135
3 ship_1pin median 5.612
3 ship_4pin median 24.192
3 pin_1 median 0.248
3 pin_4 median 1.268
3 nc_4_forearms median 1.667
3 nc_4_one_live median 3.134
3 nc_4_one_live_gut median 4.969
3 c_4_one_live median 8.860
3 nc_4_all_live median 9.238
3 nc_1_live median 1.132
4 ship_1pin median 5.626
4 ship_4pin median 23.291
4 pin_1 median 0.249
4 pin_4 median 1.271
4 nc_4_forearms median 1.809
4 nc_4_one_live median 3.429
4 nc_4_one_live_gut median 4.777
4 c_4_one_live median 8.777
4 nc_4_all_live median 8.990
4 nc_1_live median 1.128
5 ship_1pin median 4.790
5 ship_4pin median 23.049
5 pin_1 median 0.247
5 pin_4 median 1.282
5 nc_4_forearms median 1.684
5 nc_4_one_live median 3.326
5 nc_4_one_live_gut median 4.850
5 c_4_one_live median 8.675
5 nc_4_all_live median 9.965
5 nc_1_live median 1.578

View file

@ -0,0 +1,18 @@
#!/bin/sh
# name|env|args
CONF='ship_1pin||1 0.08 0.10 0 0 0 0 600
ship_4pin||4 0.08 0.10 0 0 0 0 600
pin_1||1 0.08 0.10 0 1 1 0 600
pin_4||4 0.08 0.10 0 1 1 0 600
nc_4_forearms|NOCONTACT=1|4 0.08 0.10 1 1 1 0 600
nc_4_one_live|NOCONTACT=1|4 0.08 0.10 2 1 1 0 600
nc_4_one_live_gut|NOCONTACT=1|4 0.08 0.10 2 1 1 1 600
c_4_one_live|CONTACT=1|4 0.08 0.10 2 1 1 0 600
nc_4_all_live|NOCONTACT=1|4 0.08 0.10 3 1 1 0 600
nc_1_live|NOCONTACT=1|1 0.08 0.10 3 1 1 0 600'
for rep in 1 2 3 4 5; do
echo "$CONF" | while IFS='|' read name env args; do
if [ -n "$env" ]; then out=$(env $env ./fella.exe $args); else out=$(./fella.exe $args); fi
echo "$rep $name $(echo "$out" | grep -o 'median [0-9.]*' )"
done
done

View file

@ -0,0 +1,50 @@
1 A_ship_pinned_1 median 4.271 live particles 0
1 B_skipflag_pinned_1 median 0.252 live particles 0
1 D_forearm_live_1 median 0.204 live particles 40
1 E_fella_live_1 median 1.025 live particles 661
1 F_fella_live_gut_1 median 2.244 live particles 961
1 G_fella_live_contact_1 median 5.117 live particles 661
1 I_fella_live_heldby_1 median 6.433 live particles 661
1 H_skipflag_pinned_4 median 1.021 live particles 0
1 J_4_one_live_absent median 2.266 live particles 661
1 K_4_one_live_gut_absent median 4.334 live particles 961
2 A_ship_pinned_1 median 4.918 live particles 0
2 B_skipflag_pinned_1 median 0.253 live particles 0
2 D_forearm_live_1 median 0.205 live particles 40
2 E_fella_live_1 median 1.036 live particles 661
2 F_fella_live_gut_1 median 2.428 live particles 961
2 G_fella_live_contact_1 median 12.285 live particles 661
2 I_fella_live_heldby_1 median 5.280 live particles 661
2 H_skipflag_pinned_4 median 1.014 live particles 0
2 J_4_one_live_absent median 2.270 live particles 661
2 K_4_one_live_gut_absent median 4.166 live particles 961
3 A_ship_pinned_1 median 5.341 live particles 0
3 B_skipflag_pinned_1 median 0.256 live particles 0
3 D_forearm_live_1 median 0.204 live particles 40
3 E_fella_live_1 median 1.046 live particles 661
3 F_fella_live_gut_1 median 2.349 live particles 961
3 G_fella_live_contact_1 median 5.441 live particles 661
3 I_fella_live_heldby_1 median 5.310 live particles 661
3 H_skipflag_pinned_4 median 1.046 live particles 0
3 J_4_one_live_absent median 2.317 live particles 661
3 K_4_one_live_gut_absent median 4.218 live particles 961
4 A_ship_pinned_1 median 5.124 live particles 0
4 B_skipflag_pinned_1 median 0.255 live particles 0
4 D_forearm_live_1 median 0.204 live particles 40
4 E_fella_live_1 median 1.028 live particles 661
4 F_fella_live_gut_1 median 2.359 live particles 961
4 G_fella_live_contact_1 median 5.683 live particles 661
4 I_fella_live_heldby_1 median 5.232 live particles 661
4 H_skipflag_pinned_4 median 1.023 live particles 0
4 J_4_one_live_absent median 2.271 live particles 661
4 K_4_one_live_gut_absent median 4.319 live particles 961
5 A_ship_pinned_1 median 4.507 live particles 0
5 B_skipflag_pinned_1 median 0.257 live particles 0
5 D_forearm_live_1 median 0.205 live particles 40
5 E_fella_live_1 median 1.050 live particles 661
5 F_fella_live_gut_1 median 2.369 live particles 961
5 G_fella_live_contact_1 median 5.268 live particles 661
5 I_fella_live_heldby_1 median 5.052 live particles 661
5 H_skipflag_pinned_4 median 1.039 live particles 0
5 J_4_one_live_absent median 2.284 live particles 661
5 K_4_one_live_gut_absent median 4.262 live particles 961

View file

@ -0,0 +1,17 @@
#!/bin/sh
CONF='A_ship_pinned_1|MINRING=4|1 0.10 0.14 0 0 0 0 600
B_skipflag_pinned_1|MINRING=4|1 0.10 0.14 0 1 1 0 600
D_forearm_live_1|MINRING=4 NOCONTACT=1 ABSENT=1|1 0.10 0.14 1 1 1 0 600
E_fella_live_1|MINRING=4 NOCONTACT=1|1 0.10 0.14 3 1 1 0 600
F_fella_live_gut_1|MINRING=4 NOCONTACT=1|1 0.10 0.14 3 1 1 1 600
G_fella_live_contact_1|MINRING=4|1 0.10 0.14 3 1 1 0 600
I_fella_live_heldby_1|MINRING=4 NOCONTACT=1|1 0.10 0.14 3 1 0 0 600
H_skipflag_pinned_4|MINRING=4|4 0.10 0.14 0 1 1 0 600
J_4_one_live_absent|MINRING=4 NOCONTACT=1 ABSENT=1|4 0.10 0.14 2 1 1 0 600
K_4_one_live_gut_absent|MINRING=4 NOCONTACT=1 ABSENT=1|4 0.10 0.14 2 1 1 1 600'
for rep in 1 2 3 4 5; do
echo "$CONF" | while IFS='|' read name env args; do
out=$(env $env ./fella.exe $args)
echo "$rep $name $(echo "$out" | grep -o 'median [0-9.]*') $(echo "$out" | grep -o 'live particles [0-9]*')"
done
done

142
docs/probes/flesh/sieve.c Normal file
View file

@ -0,0 +1,142 @@
// Sieve probe: a skin sheet attached along its length to a horizontal static capsule (a BDS thigh: r 0.07, half 0.18) at spacing s,
// under a fluid pour (7x7x7, r 0.0125, 15.6 g) and a dropped solid block (4x4x4 solid particles, r 0.02) at 4 substeps, 60 Hz as the
// Ymir pour test and 120 Hz as BDS. Readings: worst edge stretch, deep overlaps (pairs within half a contact distance), fluid and
// solid particles that reach the bone surface through the skin (centre within r_cap + 1.5 r of the axis segment), and fallen below.
// usage: sieve s hz substeps attach_compliance dropped(0 fluid,1 solid)
#include "ymir_particles.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static float seglen(float x, float y, float z, float half) // distance from the capsule axis segment (along x here)
{
float cx = x < -half ? -half : x > half ? half : x;
return sqrtf((x - cx) * (x - cx) + y * y + z * z);
}
int main(int argc, char** argv)
{
float s = argc > 1 ? (float)atof(argv[1]) : 0.08f;
float hz = argc > 2 ? (float)atof(argv[2]) : 120.f;
int sub = argc > 3 ? atoi(argv[3]) : 4;
float ac = argc > 4 ? (float)atof(argv[4]) : 1e-3f;
int solid = argc > 5 ? atoi(argv[5]) : 0;
const float R = 0.07f, HALF = 0.18f, Y = 0.5f;
ymir_particle_capacities c = {2048, 8192, 8192, 1024, 2048, 2, 512, 0, 0};
ymir_particles* W = ymir_particles_create(&c, 2);
ymir_material_desc m = {0.3f, 0.01f, 0, 0, 1};
ymir_particles_set_material(W, 0, &m);
// the capsule lies along x: Ymir capsules run along local y, so rotate y onto x (90 degrees about z)
float q[4] = {0, 0, sinf(-0.785398f), cosf(-0.785398f)};
ymir_particle_proxy* b = W->proxies;
memset(b, 0, 2 * sizeof *b);
b[0].shape = YMIR_PROXY_CAPSULE; b[0].size[0] = R; b[0].size[1] = HALF; b[0].position[1] = Y; memcpy(b[0].rotation, q, sizeof q);
b[1].shape = YMIR_PROXY_BOX; b[1].size[0] = 5; b[1].size[1] = 0.5f; b[1].size[2] = 5; b[1].position[1] = -0.5f; b[1].rotation[3] = 1;
W->body_count = 2;
// skin: rings along the capsule at spacing s, staggered, centres on the bone surface; caps left open (a limb continues at its joints)
int rings = (int)ceilf(2 * HALF / s), per = (int)fmaxf(6, roundf(2 * 3.14159f * R / s));
int n = (rings + 1) * per;
ymir_handle* ph = calloc(n, sizeof *ph);
float* lx = calloc(n, sizeof(float)); float* ly = calloc(n, sizeof(float)); float* lz = calloc(n, sizeof(float));
for (int i = 0, k = 0; i <= rings; i++)
for (int j = 0; j < per; j++, k++)
{
float a = 2 * 3.14159f * (j + 0.5f * (i & 1)) / per;
float x = -HALF + 2 * HALF * i / rings;
lx[k] = x; ly[k] = R * cosf(a); lz[k] = R * sinf(a);
// local frame of the body is y along the axis: local = (-world y, world x, z) rotated; keep world-space rest and compute the body point
ymir_particle_desc d = {{x, Y + ly[k], lz[k]}, {x, Y + ly[k], lz[k]}, {0, 0, 0}, 0.012f * (s * s / 0.0064f), 0.45f * s, 1u, 0};
ymir_particles_create_particles(W, 1, &d, 1, &ph[k]);
}
// edges: ring neighbours, next ring's two nearest; bends along rings and along the axis
ymir_handle* eh = calloc(n * 4, sizeof *eh);
int ne = 0;
int* ringe = calloc(n, sizeof(int)); int* axe = calloc(n, sizeof(int));
for (int i = 0; i <= rings; i++)
for (int j = 0; j < per; j++)
{
int k = i * per + j;
ymir_distance_desc d = {ph[k], ph[i * per + (j + 1) % per], -1, 2e-4f};
ymir_particles_create_distances(W, 1, &d, 1, &eh[ne]); ringe[k] = ne++;
if (i < rings)
{
int j2 = (i & 1) ? (j + 1) % per : j, j3 = (i & 1) ? j : (j + per - 1) % per;
ymir_distance_desc d2 = {ph[k], ph[(i + 1) * per + j2], -1, 2e-4f}, d3 = {ph[k], ph[(i + 1) * per + j3], -1, 2e-4f};
ymir_particles_create_distances(W, 1, &d2, 1, &eh[ne]); axe[k] = ne++;
ymir_particles_create_distances(W, 1, &d3, 1, &eh[ne]); ne++;
}
}
for (int i = 0; i <= rings; i++)
for (int j = 0; j < per; j++)
{
int k = i * per + j, kp = i * per + (j + per - 1) % per;
ymir_bend_desc bd = {eh[ringe[kp]], eh[ringe[k]], 1e-3f, -1};
ymir_handle hb; ymir_particles_create_bends(W, 1, &bd, 1, &hb);
}
for (int k = 0; k < n; k++)
{
// the body frame: rotation q maps local y onto world x; local = q^-1 (world - centre): (x, y', z) world -> local (y', -x ... ) computed numerically
float wx = lx[k], wy = ly[k], wz = lz[k];
// inverse of a -90 degree turn about z: local x = -wy? Use the exact inverse: rotate by +90 about z: (x, y) -> (-y, x)
float px = -wy, py = wx, pz = wz;
(void)px;
ymir_attachment_desc ad = {YMIR_ATTACH_BODY, 0, ph[k], 0, {-wy * -1.0f * 0 + (-wy) * 0 + wy * 0 + (-wy) , py, pz}, ac, 0};
ad.point[0] = -wy; ad.point[1] = wx; ad.point[2] = wz;
// check: q (y-axis -> x-axis) applied to (px, py, pz) must give (wx, wy, wz)
ad.point[0] = -wy; ad.point[1] = wx; ad.point[2] = wz;
ymir_handle ha; ymir_particles_attach(W, &ad, 2, 1, &ha);
}
// the load: a 7x7x7 block 0.25 m above the capsule's top, centred on it
int nl = solid ? 64 : 343, side = solid ? 4 : 7;
float lr = solid ? 0.02f : 0.0125f, sp = 2 * lr;
ymir_handle* lh = calloc(nl, sizeof *lh);
for (int a = 0; a < side; a++) for (int bb = 0; bb < side; bb++) for (int cc = 0; cc < side; cc++)
{
int k = (a * side + bb) * side + cc;
float x = -0.5f * (side - 1) * sp + a * sp, y = Y + R + 0.25f + bb * sp, z = -0.5f * (side - 1) * sp + cc * sp;
ymir_particle_desc d = {{x, y, z}, {x, y, z}, {0, 0, 0}, solid ? 0.06f : 0.015625f, lr, solid ? (6u | YMIR_PHASE_SELF_COLLIDE) : (6u | YMIR_PHASE_FLUID), 0};
ymir_particles_create_particles(W, 1, &d, 1, &lh[k]);
}
float h = 1.0f / hz / sub, worst = 0;
int deep = 0, through = 0, inside = 0, ticks = (int)(3 * hz);
char* hit = calloc(nl, 1);
for (int t = 0; t < ticks; t++)
{
ymir_particles_begin(W);
for (int ss = 0; ss < sub; ss++)
{
for (int k = 0; k < 2; k++) { /* static: pose fixed, velocities zero */ }
ymir_particles_substep(W, h, 0, -9.81f, 0);
b[0].position[1] = Y; b[0].position[0] = 0; b[0].position[2] = 0; memcpy(b[0].rotation, q, sizeof q);
b[1].position[1] = -0.5f; b[1].position[0] = b[1].position[2] = 0; b[1].rotation[0] = b[1].rotation[1] = b[1].rotation[2] = 0; b[1].rotation[3] = 1;
}
ymir_particles_end(W, sub);
for (int e = 0; e < ne; e++)
{
int i = W->dist_a[eh[e].lane], j = W->dist_b[eh[e].lane];
float dx = W->pos_x[i] - W->pos_x[j], dy = W->pos_y[i] - W->pos_y[j], dz = W->pos_z[i] - W->pos_z[j];
float st = sqrtf(dx * dx + dy * dy + dz * dz) / W->dist_rest[eh[e].lane] - 1;
if (st > worst) worst = st;
}
for (int k = 0; k < nl; k++)
{
int l = lh[k].lane;
for (int s2 = 0; s2 < n; s2++)
{
int i = ph[s2].lane;
float dx = W->pos_x[i] - W->pos_x[l], dy = W->pos_y[i] - W->pos_y[l], dz = W->pos_z[i] - W->pos_z[l];
if (sqrtf(dx * dx + dy * dy + dz * dz) < 0.5f * (W->radius[i] + lr)) deep++;
}
if (fabsf(W->pos_x[l]) < HALF && seglen(W->pos_x[l], W->pos_y[l] - Y, W->pos_z[l], HALF) < R) inside++;
if (!hit[k] && fabsf(W->pos_x[l]) < HALF && seglen(W->pos_x[l], W->pos_y[l] - Y, W->pos_z[l], HALF) < R + 1.5f * lr) { hit[k] = 1; through++; }
}
}
// a sanity read of the skin's place: mean distance of skin centres from the axis (R when held)
float md = 0;
for (int k = 0; k < n; k++) { int i = ph[k].lane; md += seglen(W->pos_x[i], W->pos_y[i] - Y, W->pos_z[i], HALF); }
int below = 0; for (int k = 0; k < nl; k++) below += W->pos_y[lh[k].lane] < 0.05f;
printf("SIEVE s=%.3f %gHz x%d attach_c=%g load=%s: skin %d particles r %.3f, %d edges | worst stretch %.4f | deep (tick x pair) %d | load reaching the bone through the skin %d of %d | inside the bone (tick x particle) %d | on the floor at 3 s %d | skin mean axis distance %.4f (R %.3f)\n",
s, hz, sub, ac, solid ? "solid" : "fluid", n, 0.45f * s, ne, worst, deep, through, nl, inside, below, md / n, R);
return 0;
}

View file

@ -0,0 +1,691 @@
--- /tmp/base_ymir_particles.c 2026-11-25 20:46:03.839493800 +0100
+++ /c/Users/Meta/AppData/Local/Temp/claude/a3c197df03062ea3/0d8e833d-7cf1-4193-ba8c-8babeaf48eda/scratchpad/imag-flesh/ymir_particles.c 2026-11-25 20:14:24.880958500 +0100
@@ -5,6 +5,16 @@
// every sum in a fixed order. See ymir_particles.h.
#include "ymir_particles.h"
+#include <stdint.h>
+uint8_t* probe_pinned = 0; int32_t* probe_body_group = 0;
+#include <windows.h>
+double probe_t[8];
+static double pnow(void){LARGE_INTEGER c,f;QueryPerformanceCounter(&c);QueryPerformanceFrequency(&f);return 1000.0*(double)c.QuadPart/(double)f.QuadPart;}
+#define T(k,x) do{double _t=pnow(); x; probe_t[k]+=pnow()-_t;}while(0)
+int probe_nocontact = 0;
+#define PIN(i) ((probe_pinned && probe_pinned[i]) || (probe_nocontact && !(p->phase[i] & (YMIR_PHASE_SELF_COLLIDE|YMIR_PHASE_FLUID)) && grid_ctx))
+static int grid_ctx = 0;
+
#include <math.h>
#include <stdlib.h>
@@ -1364,7 +1374,7 @@
memset(p->cell_start, 0, sizeof(int32_t) * (size_t)(buckets + 1));
for (int32_t i = 0; i < p->particle_count; ++i)
{
- if (!p->particle_live[i] || (solids && (p->phase[i] & YMIR_PHASE_FLUID)))
+ if (!p->particle_live[i] || PIN(i) || (solids && (p->phase[i] & YMIR_PHASE_FLUID)))
{
continue;
}
@@ -1380,7 +1390,7 @@
}
for (int32_t i = 0; i < p->particle_count; ++i)
{
- if (p->particle_live[i] && !(solids && (p->phase[i] & YMIR_PHASE_FLUID)))
+ if (p->particle_live[i] && !PIN(i) && !(solids && (p->phase[i] & YMIR_PHASE_FLUID)))
{
uint32_t c = cell_hash(p->cell_x[i], p->cell_y[i], p->cell_z[i], p->table_mask);
p->sorted[p->cell_start[c] + p->cell_fill[c]++] = i;
@@ -1461,7 +1471,7 @@
{
for (int32_t i = 0; i < p->particle_count; ++i)
{
- if (!p->particle_live[i] || (p->phase[i] & YMIR_PHASE_FLUID)) // the grid holds no fluid lane
+ if (!p->particle_live[i] || PIN(i) || (p->phase[i] & YMIR_PHASE_FLUID)) // the grid holds no fluid lane
{
continue;
}
@@ -1979,7 +1989,7 @@
{
continue;
}
- if (held_by(p, i, k))
+ if (probe_body_group ? probe_body_group[k] == (int32_t)(p->phase[i] & YMIR_PHASE_GROUP_MASK) : held_by(p, i, k))
{
continue;
}
@@ -2056,6 +2066,7 @@
continue;
}
int32_t i = p->att_particle[a];
+ if (PIN(i)) continue;
v3 at = end_point(p, a);
v3 d = vsub(P(p, p->pp_x, p->pp_y, p->pp_z, i), at);
float len = vlen(d);
@@ -2088,6 +2099,7 @@
continue;
}
int32_t i = p->dist_a[e], j = p->dist_b[e];
+ if (PIN(i) && PIN(j)) continue;
float wi = p->inv_mass[i], wj = p->inv_mass[j], alpha = p->dist_compliance[e] / (f->h * f->h);
v3 d = vsub(P(p, p->pp_x, p->pp_y, p->pp_z, j), P(p, p->pp_x, p->pp_y, p->pp_z, i));
float len = vlen(d);
@@ -2117,6 +2129,7 @@
continue;
}
int32_t a = p->bend_a[k], b = p->bend_b[k], c = p->bend_c[k];
+ if (PIN(a) && PIN(b) && PIN(c)) continue;
float wa = p->inv_mass[a], wb = p->inv_mass[b], wc = p->inv_mass[c];
v3 ja = {0.0f, 0.0f, 0.0f}, jc = {0.0f, 0.0f, 0.0f};
for (int pass = 0; pass < 2; ++pass)
@@ -2320,17 +2333,17 @@
}
put(p->pp_x, p->pp_y, p->pp_z, i, x);
}
- build_grid(p);
- particle_contacts(p);
+ grid_ctx = 1; T(0, build_grid(p););
+ T(1, particle_contacts(p);); grid_ctx = 0;
if (p->live_fluid > 0)
{
fluid_density(p);
}
- solve_attachments(&f);
- solve_distances(&f);
- solve_bends(&f);
- solve_tethers(&f);
- body_contacts(&f);
+ T(2, solve_attachments(&f););
+ T(3, solve_distances(&f););
+ T(4, solve_bends(&f););
+ T(5, solve_tethers(&f););
+ T(6, body_contacts(&f););
for (int32_t i = 0; i < p->particle_count; ++i)
{
if (!p->particle_live[i])
--- /tmp/base_ymir_particles.h 2026-11-25 20:46:03.949325900 +0100
+++ /c/Users/Meta/AppData/Local/Temp/claude/a3c197df03062ea3/0d8e833d-7cf1-4193-ba8c-8babeaf48eda/scratchpad/imag-flesh/ymir_particles.h 2026-11-25 20:09:57.828286700 +0100
@@ -1,292 +1,292 @@
-// Particles and the constraints between them: Ymir's own position-based solver, stepped inside Box3D's substeps
-// (see ymir_box3d_soa.c, the only Box3D caller). This module knows nothing of Box3D. soa.c fills one proxy per body
-// (pose, velocity, shape, inverse mass and inertia) before each substep, the solver moves those proxies by the
-// constraints it solves, and the impulse each body accumulated comes back in the proxy for soa.c to apply.
-// See docs/soa-world.md.
-//
-// There is no object above the lanes: particles are one lane set with a phase word, and every constraint type
-// (distance, bend, attachment) is a lane set of its own. A thing made of particles is whatever constraints a
-// caller emitted. Tethers and components are derived from the distance graph by rederive_topology, which every
-// topology command ends in and which is their only writer.
-//
-// Determinism: single thread; constraints are Gauss-Seidel in lane order; the one Jacobi pass (particle-particle
-// contact) accumulates and applies after the walk (each pair once, from the lower of its two cells, neighbours read in cell then index order
-// from a stable counting-sort grid, a pair weighted the same at both ends); proxy impulses are summed per body
-// in lane order; heap and breadth-first orders break ties by lane. Compiled with -ffp-contract=off (/fp:precise
-// on MSVC) so no fused multiply-add changes a byte.
-//
-// Licence note: the formulas come from the papers (Kelager 2010 bend, Kim 2012 tethers, Macklin 2014, 2016, 2019).
-// No code of PhysX 5.6.0 or any other solver is adapted here.
-
-#ifndef YMIR_PARTICLES_H
-#define YMIR_PARTICLES_H
-
-#include <stdint.h>
-
-enum
-{
- YMIR_PARTICLES_OK = 0,
- YMIR_PARTICLES_INVALID_ARGUMENT = 1,
- YMIR_PARTICLES_DUPLICATE_ID = 2,
- YMIR_PARTICLES_FULL = 3,
- YMIR_PARTICLES_OUT_OF_MEMORY = 4
-};
-
-// Phase word: bits 0-19 are the group; the three flags below are the only other bits.
-enum
-{
- YMIR_PHASE_GROUP_MASK = 0xFFFFFu,
- YMIR_PHASE_SELF_COLLIDE = 1u << 20, // particles of one group collide (both must set it)
- YMIR_PHASE_SELF_FILTER = 1u << 21, // ... except pairs closer than 1.5 (ri + rj) in the rest pose (either sets it)
- YMIR_PHASE_FLUID = 1u << 22, // a fluid particle: number density, not contact, with other fluid; contact with solids. Group and
- // the SELF_* flags are ignored between two fluid particles (all fluid shares one density field)
- YMIR_PHASE_VALID = YMIR_PHASE_GROUP_MASK | YMIR_PHASE_SELF_COLLIDE | YMIR_PHASE_SELF_FILTER | YMIR_PHASE_FLUID
-};
-
-// Lane sets a command names (a type for remove, a set for free_slots).
-enum
-{
- YMIR_LANES_PARTICLE = 0,
- YMIR_LANES_DISTANCE = 1,
- YMIR_LANES_BEND = 2,
- YMIR_LANES_ATTACHMENT = 3,
- YMIR_LANES_WET = 4, // a wet patch: settled liquid on a body or a solid particle
- YMIR_LANES_SENSOR = 5 // a liquid sensor: a sphere fixed in a body that reads the liquid volume inside it
-};
-
-enum
-{
- YMIR_ATTACH_BODY = 0, // particle to a point fixed in a body (its lane)
- YMIR_ATTACH_STATIC = 1, // particle to a world point
- YMIR_ATTACH_REMOVED = 2
-};
-
-enum
-{
- YMIR_ATTACH_FLAG_TETHER = 1u // bounds each particle by the rest path to the nearest point of its two nearest islands of tether attachments
-};
-
-enum
-{
- YMIR_PROXY_NONE = 0, // no shape (an anchor): attachments reach it, particles never touch it
- YMIR_PROXY_SPHERE = 1,
- YMIR_PROXY_CAPSULE = 2, // segment along local Y
- YMIR_PROXY_BOX = 3
-};
-
-// A lane and the generation it had when the handle was made.
-typedef struct ymir_handle
-{
- int32_t lane;
- uint32_t generation;
-} ymir_handle;
-
-typedef struct ymir_particle_capacities
-{
- int32_t particles, distances, bends, attachments, tethers, materials, fluid, wets, sensors; // fluid: the most live particles with the FLUID phase; wets: the most wet patches; sensors: the most liquid sensors
-} ymir_particle_capacities;
-
-// A particle to create. `mass` kg (0 = immovable); `velocity` m/s; `rest` is its place in the rest pose (rest lengths, bends and
-// the self-collision filter read it); `material` indexes the material table.
-typedef struct ymir_particle_desc
-{
- float position[3];
- float rest[3];
- float velocity[3];
- float mass;
- float radius;
- uint32_t phase;
- uint32_t material;
-} ymir_particle_desc;
-
-// A distance constraint. `rest` < 0 takes the rest-pose length. `compliance` m/N, >= 0.
-typedef struct ymir_distance_desc
-{
- ymir_handle a, b;
- float rest;
- float compliance;
-} ymir_distance_desc;
-
-// A bend over two distance constraints that share one particle. `compliance` < 0 switches the soft pass off;
-// `max_angle` (radians, the deflection from straight) < 0 switches the hard limit off.
-typedef struct ymir_bend_desc
-{
- ymir_handle e1, e2;
- float compliance;
- float max_angle;
-} ymir_bend_desc;
-
-// An attachment of `particle`. `body` is the body lane (body kind) or -1. `point` is in the body's frame (body)
-// or the world (static).
-typedef struct ymir_attachment_desc
-{
- uint32_t kind;
- uint32_t flags;
- ymir_handle particle;
- int32_t body;
- float point[3];
- float compliance; // m/N, >= 0
- uint32_t reserved;
-} ymir_attachment_desc;
-
-// A liquid sensor to create: a sphere of `radius` m (> 0) centred on `point` in the frame of body lane `body`.
-typedef struct ymir_sensor_desc
-{
- int32_t body;
- float point[3];
- float radius;
-} ymir_sensor_desc;
-
-typedef struct ymir_material_desc
-{
- float friction; // >= 0
- float viscosity; // >= 0, XSPH c (dimensionless): a fluid pair's velocities relax toward each other by c W / n0 per substep; mean of the pair's two
- float wet_friction; // >= 0: what settled liquid of this material does to friction where it lands; stored for the friction cut, read by nothing here
- uint32_t pad;
- uint64_t material_id;
-} ymir_material_desc;
-
-// A body as the particle phase sees it. Filled by the caller before a step; `impulse` and `angular_impulse`
-// (N s, world, about the body position) are the totals the particle phase delivered, written by it.
-typedef struct ymir_particle_proxy
-{
- uint32_t shape;
- float size[3];
- float position[3];
- float rotation[4]; // x y z w
- float velocity[3];
- 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]; // 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
-{
- int32_t particle_capacity, distance_capacity, bend_capacity, attachment_capacity, tether_capacity, material_capacity;
- int32_t body_capacity, fluid_capacity; // fluid_capacity: the most live fluid particles; it sizes the neighbour arena
- int32_t particle_count, distance_count, bend_count, attachment_count; // high-water marks
- int32_t tether_count; // derived: the live tethers
- int32_t body_count; // bodies filled in `proxies` this step
- int32_t live_particles;
- int32_t fluid_rings; // derived by fluid_density: rings of kernel cells (4 rfmax) that hold a kernel pair, 0 without fluid
- int32_t fluid_overflow; // kernel neighbours past a fluid particle's list in the last step, summed over substeps: a sensor
- int32_t wet_capacity, wet_count; // wet patches: the capacity and the high-water mark
- int32_t sensor_capacity, sensor_count; // liquid sensors: the capacity and the high-water mark
- int32_t live_fluid; // derived: the live particles with the FLUID phase bit; the create and destroy commands are its only writers
- ymir_particle_proxy* proxies;
-
- // particle lanes; a free lane reads live 0 and every other lane 0 except generation and removed_at
- float *pos_x, *pos_y, *pos_z, *vel_x, *vel_y, *vel_z, *radius, *inv_mass;
- float *rest_x, *rest_y, *rest_z;
- uint32_t *phase, *material, *particle_live, *particle_generation;
- uint64_t* particle_removed_at;
- int32_t* component; // derived: the lowest lane of the particle's distance-connected component
- int32_t *nbr, *nbr_start, *nbr_count; // scratch: the fluid rows' kernel neighbours in walk order, packed (CSR) in an arena of fluid_capacity * YMIR_FLUID_NEIGHBOURS; row i holds nbr_count[i] from nbr_start[i], this substep's
- int32_t *support_body, *support_particle; // sensor: what a fluid particle touched in the last substep, the highest body lane / the first solid lane its row listed touching it; -1 for none, and always -1 for a solid or free lane
- float* density; // sensor: a fluid particle's number density over its rest density n_i / n0 in the last substep it ran, else 0
-
- // wet lanes: a patch of settled liquid on one support, a body (wet_body, its point in the body frame) or a solid particle (wet_particle, point 0);
- // one of the two is -1, and a free lane holds -1 in both. Written by settle, remove and destroy_particles only; the step never touches them.
- int32_t *wet_body, *wet_particle;
- 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;
- int32_t *wet_order, *wet_body_start, *wet_body_count; // derived lookup index (rederive_wet): live body patches by body then centre x; each body's slice
- float* wet_body_box; // derived, 7 per body: min xyz and max xyz of its discs' boxes, then its largest radius
- 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;
-
- // sensor lanes: a sphere fixed in a body; a free lane reads live 0, body -1 and 0 elsewhere except generation and removed_at. Written by create_sensors and
- // remove; sensor_liquid_volume and sensor_wet_volume are written by end alone, at the step's end (m^3): the sum of (2 r)^3 over the live fluid particles whose centre is inside,
- // and the sum of the volumes of the live wet patches whose centre is inside, on any body or solid particle
- int32_t* sensor_body;
- float *sensor_x, *sensor_y, *sensor_z, *sensor_radius, *sensor_liquid_volume, *sensor_wet_volume;
- uint32_t *sensor_live, *sensor_generation;
- uint64_t* sensor_removed_at;
-
- // distance lanes
- int32_t *dist_a, *dist_b;
- float *dist_rest, *dist_compliance;
- float* dist_tension; // N, the load the constraint carries, mean over the substeps: a sensor
- uint32_t *dist_live, *dist_generation;
- uint64_t* dist_removed_at;
-
- // bend lanes: e1 and e2 are distance lanes; a, b, c the particles (b shared)
- int32_t *bend_e1, *bend_e2, *bend_a, *bend_b, *bend_c;
- float *bend_rest, *bend_limit, *bend_compliance; // offsets of b from the centroid; limit < 0 is off
- uint32_t *bend_live, *bend_generation;
- uint64_t* bend_removed_at;
-
- // attachment lanes
- uint32_t *att_kind, *att_flags, *att_generation;
- int32_t *att_particle, *att_body;
- float *att_point_x, *att_point_y, *att_point_z, *att_compliance;
- float *att_force_x, *att_force_y, *att_force_z; // N, the force the attachment exerts on its particles (mean): a sensor
- uint64_t* att_removed_at;
-
- // tether lanes: derived: grouped by island in lane order (an island: tether particles joined by live distances, named by its
- // lowest tether attachment), settle order within; per island one for each particle that is not a tether particle and holds the island
- // among its two nearest, to the attachment point it is nearest; then per attachment pair with a body among them a route of
- // booking lanes (attachment -1, edge only) along their rest path and the pair tether; parents come first;
- // other >= 0 makes the tether a bound between two attachment points (edge -1) instead of a particle and a point
- int32_t *tether_att, *tether_particle, *tether_parent, *tether_edge, *tether_other;
- float* tether_bound;
-
- // material table
- float *mat_friction, *mat_viscosity, *mat_wet_friction;
- uint64_t* mat_id;
-
- // solver scratch, allocated once at create
- float *pp_x, *pp_y, *pp_z, *dp_x, *dp_y, *dp_z, *dist_acc, *tacc_x, *tacc_y, *tacc_z, *lambda;
- float fluid_k; // n0(h) = fluid_k / h^3: the Poly6 sum over the simple-cubic lattice of spacing h / 2, a create-time constant
- int32_t *lane_list, *cell_x, *cell_y, *cell_z, *cell_start, *cell_fill, *sorted, *contacts;
- float *sx, *sy, *sz, *sw; // the fluid grid's lanes in sorted order (predicted position, inverse mass): scratch, only fluid_density reads them
- uint32_t* sphase; // the sorted lanes' phase
- int32_t *adj_start, *adj_edge, *heap_node, *heap_edge, *via, *owner, *tether_of, *queue, *comp_size, *isle, *near_isle;
- float *heap_key, *path, *near_path;
- uint8_t* done;
- uint32_t table_mask;
- void* arena;
-} ymir_particles;
-
-// Creates the state for the capacities (all >= 0) and `body_capacity` proxies; NULL on failure.
-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.
-int32_t ymir_particles_create_particles(ymir_particles* p, int32_t n, const ymir_particle_desc* d, uint64_t step_count, ymir_handle* out);
-int32_t ymir_particles_destroy_particles(ymir_particles* p, int32_t n, const ymir_handle* h, uint64_t step_count);
-int32_t ymir_particles_create_distances(ymir_particles* p, int32_t n, const ymir_distance_desc* d, uint64_t step_count, ymir_handle* out);
-int32_t ymir_particles_create_bends(ymir_particles* p, int32_t n, const ymir_bend_desc* d, uint64_t step_count, ymir_handle* out);
-int32_t ymir_particles_attach(ymir_particles* p, const ymir_attachment_desc* d, int32_t body_count, uint64_t step_count, ymir_handle* out);
-int32_t ymir_particles_remove(ymir_particles* p, uint32_t type, int32_t n, const ymir_handle* h, uint64_t step_count);
-// 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.
-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.
-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);
-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);
-
-// 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
+// Particles and the constraints between them: Ymir's own position-based solver, stepped inside Box3D's substeps
+// (see ymir_box3d_soa.c, the only Box3D caller). This module knows nothing of Box3D. soa.c fills one proxy per body
+// (pose, velocity, shape, inverse mass and inertia) before each substep, the solver moves those proxies by the
+// constraints it solves, and the impulse each body accumulated comes back in the proxy for soa.c to apply.
+// See docs/soa-world.md.
+//
+// There is no object above the lanes: particles are one lane set with a phase word, and every constraint type
+// (distance, bend, attachment) is a lane set of its own. A thing made of particles is whatever constraints a
+// caller emitted. Tethers and components are derived from the distance graph by rederive_topology, which every
+// topology command ends in and which is their only writer.
+//
+// Determinism: single thread; constraints are Gauss-Seidel in lane order; the one Jacobi pass (particle-particle
+// contact) accumulates and applies after the walk (each pair once, from the lower of its two cells, neighbours read in cell then index order
+// from a stable counting-sort grid, a pair weighted the same at both ends); proxy impulses are summed per body
+// in lane order; heap and breadth-first orders break ties by lane. Compiled with -ffp-contract=off (/fp:precise
+// on MSVC) so no fused multiply-add changes a byte.
+//
+// Licence note: the formulas come from the papers (Kelager 2010 bend, Kim 2012 tethers, Macklin 2014, 2016, 2019).
+// No code of PhysX 5.6.0 or any other solver is adapted here.
+
+#ifndef YMIR_PARTICLES_H
+#define YMIR_PARTICLES_H
+
+#include <stdint.h>
+
+enum
+{
+ YMIR_PARTICLES_OK = 0,
+ YMIR_PARTICLES_INVALID_ARGUMENT = 1,
+ YMIR_PARTICLES_DUPLICATE_ID = 2,
+ YMIR_PARTICLES_FULL = 3,
+ YMIR_PARTICLES_OUT_OF_MEMORY = 4
+};
+
+// Phase word: bits 0-19 are the group; the three flags below are the only other bits.
+enum
+{
+ YMIR_PHASE_GROUP_MASK = 0xFFFFFu,
+ YMIR_PHASE_SELF_COLLIDE = 1u << 20, // particles of one group collide (both must set it)
+ YMIR_PHASE_SELF_FILTER = 1u << 21, // ... except pairs closer than 1.5 (ri + rj) in the rest pose (either sets it)
+ YMIR_PHASE_FLUID = 1u << 22, // a fluid particle: number density, not contact, with other fluid; contact with solids. Group and
+ // the SELF_* flags are ignored between two fluid particles (all fluid shares one density field)
+ YMIR_PHASE_VALID = YMIR_PHASE_GROUP_MASK | YMIR_PHASE_SELF_COLLIDE | YMIR_PHASE_SELF_FILTER | YMIR_PHASE_FLUID
+};
+
+// Lane sets a command names (a type for remove, a set for free_slots).
+enum
+{
+ YMIR_LANES_PARTICLE = 0,
+ YMIR_LANES_DISTANCE = 1,
+ YMIR_LANES_BEND = 2,
+ YMIR_LANES_ATTACHMENT = 3,
+ YMIR_LANES_WET = 4, // a wet patch: settled liquid on a body or a solid particle
+ YMIR_LANES_SENSOR = 5 // a liquid sensor: a sphere fixed in a body that reads the liquid volume inside it
+};
+
+enum
+{
+ YMIR_ATTACH_BODY = 0, // particle to a point fixed in a body (its lane)
+ YMIR_ATTACH_STATIC = 1, // particle to a world point
+ YMIR_ATTACH_REMOVED = 2
+};
+
+enum
+{
+ YMIR_ATTACH_FLAG_TETHER = 1u // bounds each particle by the rest path to the nearest point of its two nearest islands of tether attachments
+};
+
+enum
+{
+ YMIR_PROXY_NONE = 0, // no shape (an anchor): attachments reach it, particles never touch it
+ YMIR_PROXY_SPHERE = 1,
+ YMIR_PROXY_CAPSULE = 2, // segment along local Y
+ YMIR_PROXY_BOX = 3
+};
+
+// A lane and the generation it had when the handle was made.
+typedef struct ymir_handle
+{
+ int32_t lane;
+ uint32_t generation;
+} ymir_handle;
+
+typedef struct ymir_particle_capacities
+{
+ int32_t particles, distances, bends, attachments, tethers, materials, fluid, wets, sensors; // fluid: the most live particles with the FLUID phase; wets: the most wet patches; sensors: the most liquid sensors
+} ymir_particle_capacities;
+
+// A particle to create. `mass` kg (0 = immovable); `velocity` m/s; `rest` is its place in the rest pose (rest lengths, bends and
+// the self-collision filter read it); `material` indexes the material table.
+typedef struct ymir_particle_desc
+{
+ float position[3];
+ float rest[3];
+ float velocity[3];
+ float mass;
+ float radius;
+ uint32_t phase;
+ uint32_t material;
+} ymir_particle_desc;
+
+// A distance constraint. `rest` < 0 takes the rest-pose length. `compliance` m/N, >= 0.
+typedef struct ymir_distance_desc
+{
+ ymir_handle a, b;
+ float rest;
+ float compliance;
+} ymir_distance_desc;
+
+// A bend over two distance constraints that share one particle. `compliance` < 0 switches the soft pass off;
+// `max_angle` (radians, the deflection from straight) < 0 switches the hard limit off.
+typedef struct ymir_bend_desc
+{
+ ymir_handle e1, e2;
+ float compliance;
+ float max_angle;
+} ymir_bend_desc;
+
+// An attachment of `particle`. `body` is the body lane (body kind) or -1. `point` is in the body's frame (body)
+// or the world (static).
+typedef struct ymir_attachment_desc
+{
+ uint32_t kind;
+ uint32_t flags;
+ ymir_handle particle;
+ int32_t body;
+ float point[3];
+ float compliance; // m/N, >= 0
+ uint32_t reserved;
+} ymir_attachment_desc;
+
+// A liquid sensor to create: a sphere of `radius` m (> 0) centred on `point` in the frame of body lane `body`.
+typedef struct ymir_sensor_desc
+{
+ int32_t body;
+ float point[3];
+ float radius;
+} ymir_sensor_desc;
+
+typedef struct ymir_material_desc
+{
+ float friction; // >= 0
+ float viscosity; // >= 0, XSPH c (dimensionless): a fluid pair's velocities relax toward each other by c W / n0 per substep; mean of the pair's two
+ float wet_friction; // >= 0: what settled liquid of this material does to friction where it lands; stored for the friction cut, read by nothing here
+ uint32_t pad;
+ uint64_t material_id;
+} ymir_material_desc;
+
+// A body as the particle phase sees it. Filled by the caller before a step; `impulse` and `angular_impulse`
+// (N s, world, about the body position) are the totals the particle phase delivered, written by it.
+typedef struct ymir_particle_proxy
+{
+ uint32_t shape;
+ float size[3];
+ float position[3];
+ float rotation[4]; // x y z w
+ float velocity[3];
+ 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]; // 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
+{
+ int32_t particle_capacity, distance_capacity, bend_capacity, attachment_capacity, tether_capacity, material_capacity;
+ int32_t body_capacity, fluid_capacity; // fluid_capacity: the most live fluid particles; it sizes the neighbour arena
+ int32_t particle_count, distance_count, bend_count, attachment_count; // high-water marks
+ int32_t tether_count; // derived: the live tethers
+ int32_t body_count; // bodies filled in `proxies` this step
+ int32_t live_particles;
+ int32_t fluid_rings; // derived by fluid_density: rings of kernel cells (4 rfmax) that hold a kernel pair, 0 without fluid
+ int32_t fluid_overflow; // kernel neighbours past a fluid particle's list in the last step, summed over substeps: a sensor
+ int32_t wet_capacity, wet_count; // wet patches: the capacity and the high-water mark
+ int32_t sensor_capacity, sensor_count; // liquid sensors: the capacity and the high-water mark
+ int32_t live_fluid; // derived: the live particles with the FLUID phase bit; the create and destroy commands are its only writers
+ ymir_particle_proxy* proxies;
+
+ // particle lanes; a free lane reads live 0 and every other lane 0 except generation and removed_at
+ float *pos_x, *pos_y, *pos_z, *vel_x, *vel_y, *vel_z, *radius, *inv_mass;
+ float *rest_x, *rest_y, *rest_z;
+ uint32_t *phase, *material, *particle_live, *particle_generation;
+ uint64_t* particle_removed_at;
+ int32_t* component; // derived: the lowest lane of the particle's distance-connected component
+ int32_t *nbr, *nbr_start, *nbr_count; // scratch: the fluid rows' kernel neighbours in walk order, packed (CSR) in an arena of fluid_capacity * YMIR_FLUID_NEIGHBOURS; row i holds nbr_count[i] from nbr_start[i], this substep's
+ int32_t *support_body, *support_particle; // sensor: what a fluid particle touched in the last substep, the highest body lane / the first solid lane its row listed touching it; -1 for none, and always -1 for a solid or free lane
+ float* density; // sensor: a fluid particle's number density over its rest density n_i / n0 in the last substep it ran, else 0
+
+ // wet lanes: a patch of settled liquid on one support, a body (wet_body, its point in the body frame) or a solid particle (wet_particle, point 0);
+ // one of the two is -1, and a free lane holds -1 in both. Written by settle, remove and destroy_particles only; the step never touches them.
+ int32_t *wet_body, *wet_particle;
+ 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;
+ int32_t *wet_order, *wet_body_start, *wet_body_count; // derived lookup index (rederive_wet): live body patches by body then centre x; each body's slice
+ float* wet_body_box; // derived, 7 per body: min xyz and max xyz of its discs' boxes, then its largest radius
+ 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;
+
+ // sensor lanes: a sphere fixed in a body; a free lane reads live 0, body -1 and 0 elsewhere except generation and removed_at. Written by create_sensors and
+ // remove; sensor_liquid_volume and sensor_wet_volume are written by end alone, at the step's end (m^3): the sum of (2 r)^3 over the live fluid particles whose centre is inside,
+ // and the sum of the volumes of the live wet patches whose centre is inside, on any body or solid particle
+ int32_t* sensor_body;
+ float *sensor_x, *sensor_y, *sensor_z, *sensor_radius, *sensor_liquid_volume, *sensor_wet_volume;
+ uint32_t *sensor_live, *sensor_generation;
+ uint64_t* sensor_removed_at;
+
+ // distance lanes
+ int32_t *dist_a, *dist_b;
+ float *dist_rest, *dist_compliance;
+ float* dist_tension; // N, the load the constraint carries, mean over the substeps: a sensor
+ uint32_t *dist_live, *dist_generation;
+ uint64_t* dist_removed_at;
+
+ // bend lanes: e1 and e2 are distance lanes; a, b, c the particles (b shared)
+ int32_t *bend_e1, *bend_e2, *bend_a, *bend_b, *bend_c;
+ float *bend_rest, *bend_limit, *bend_compliance; // offsets of b from the centroid; limit < 0 is off
+ uint32_t *bend_live, *bend_generation;
+ uint64_t* bend_removed_at;
+
+ // attachment lanes
+ uint32_t *att_kind, *att_flags, *att_generation;
+ int32_t *att_particle, *att_body;
+ float *att_point_x, *att_point_y, *att_point_z, *att_compliance;
+ float *att_force_x, *att_force_y, *att_force_z; // N, the force the attachment exerts on its particles (mean): a sensor
+ uint64_t* att_removed_at;
+
+ // tether lanes: derived: grouped by island in lane order (an island: tether particles joined by live distances, named by its
+ // lowest tether attachment), settle order within; per island one for each particle that is not a tether particle and holds the island
+ // among its two nearest, to the attachment point it is nearest; then per attachment pair with a body among them a route of
+ // booking lanes (attachment -1, edge only) along their rest path and the pair tether; parents come first;
+ // other >= 0 makes the tether a bound between two attachment points (edge -1) instead of a particle and a point
+ int32_t *tether_att, *tether_particle, *tether_parent, *tether_edge, *tether_other;
+ float* tether_bound;
+
+ // material table
+ float *mat_friction, *mat_viscosity, *mat_wet_friction;
+ uint64_t* mat_id;
+
+ // solver scratch, allocated once at create
+ float *pp_x, *pp_y, *pp_z, *dp_x, *dp_y, *dp_z, *dist_acc, *tacc_x, *tacc_y, *tacc_z, *lambda;
+ float fluid_k; // n0(h) = fluid_k / h^3: the Poly6 sum over the simple-cubic lattice of spacing h / 2, a create-time constant
+ int32_t *lane_list, *cell_x, *cell_y, *cell_z, *cell_start, *cell_fill, *sorted, *contacts;
+ float *sx, *sy, *sz, *sw; // the fluid grid's lanes in sorted order (predicted position, inverse mass): scratch, only fluid_density reads them
+ uint32_t* sphase; // the sorted lanes' phase
+ int32_t *adj_start, *adj_edge, *heap_node, *heap_edge, *via, *owner, *tether_of, *queue, *comp_size, *isle, *near_isle;
+ float *heap_key, *path, *near_path;
+ uint8_t* done;
+ uint32_t table_mask;
+ void* arena;
+} ymir_particles;
+
+// Creates the state for the capacities (all >= 0) and `body_capacity` proxies; NULL on failure.
+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.
+int32_t ymir_particles_create_particles(ymir_particles* p, int32_t n, const ymir_particle_desc* d, uint64_t step_count, ymir_handle* out);
+int32_t ymir_particles_destroy_particles(ymir_particles* p, int32_t n, const ymir_handle* h, uint64_t step_count);
+int32_t ymir_particles_create_distances(ymir_particles* p, int32_t n, const ymir_distance_desc* d, uint64_t step_count, ymir_handle* out);
+int32_t ymir_particles_create_bends(ymir_particles* p, int32_t n, const ymir_bend_desc* d, uint64_t step_count, ymir_handle* out);
+int32_t ymir_particles_attach(ymir_particles* p, const ymir_attachment_desc* d, int32_t body_count, uint64_t step_count, ymir_handle* out);
+int32_t ymir_particles_remove(ymir_particles* p, uint32_t type, int32_t n, const ymir_handle* h, uint64_t step_count);
+// 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.
+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.
+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);
+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);
+
+// 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