SoA particles: chains, attachments and the particle phase in the world step, ABI 9
ymir_particles.c (no Box3D) solves XPBD chains with long-range tethers and two-way coupling to body proxies; soa.c gathers the proxies after Box3D's step, runs the phase and applies the returned impulses. A world with no particle capacity is byte-identical to ABI 8 (pinned hashes pass). Managed surface and lanes added; no behaviour tests yet. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
fb77d15a94
commit
a4e7139188
6 changed files with 1868 additions and 15 deletions
|
|
@ -11,7 +11,7 @@ set(BOX3D_VALIDATE ON CACHE BOOL "" FORCE)
|
|||
|
||||
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../../extern/box3d" "${CMAKE_CURRENT_BINARY_DIR}/box3d")
|
||||
|
||||
add_library(ymir_box3d SHARED ymir_box3d.c ymir_box3d_soa.c)
|
||||
add_library(ymir_box3d SHARED ymir_box3d.c ymir_box3d_soa.c ymir_particles.c)
|
||||
target_link_libraries(ymir_box3d PRIVATE box3d::box3d)
|
||||
target_include_directories(ymir_box3d PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../../extern/box3d/include")
|
||||
target_compile_features(ymir_box3d PRIVATE c_std_17)
|
||||
|
|
@ -28,3 +28,7 @@ if(MINGW)
|
|||
# MinGW would emit libymir_box3d.dll; the managed side loads ymir_box3d.dll.
|
||||
set_property(TARGET ymir_box3d PROPERTY PREFIX "")
|
||||
endif()
|
||||
|
||||
# The particle solver's bytes must not depend on whether the compiler fuses a multiply and an add.
|
||||
set_source_files_properties(ymir_particles.c PROPERTIES COMPILE_OPTIONS
|
||||
"$<IF:$<C_COMPILER_ID:MSVC>,/fp:precise,-ffp-contract=off>")
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@
|
|||
#include <box3d/math_functions.h>
|
||||
#include <box3d/types.h>
|
||||
|
||||
#include "ymir_particles.h"
|
||||
#include "ymir_world_lock.h"
|
||||
|
||||
#include <math.h>
|
||||
|
|
@ -57,7 +58,7 @@ enum
|
|||
YMIR_SOA_JOINT_REMOVED = 4 // a destroyed joint's lane: never accepted by add_joint
|
||||
};
|
||||
|
||||
#define YMIR_SOA_ABI_VERSION 8u
|
||||
#define YMIR_SOA_ABI_VERSION 9u
|
||||
|
||||
// A body to create. `size` by kind: sphere {radius}, capsule {radius, half_length} (segment along
|
||||
// local Y), box and static box {half_x, half_y, half_z}. `parent` is the lane index of an earlier body
|
||||
|
|
@ -123,6 +124,14 @@ typedef struct ymir_box3d_soa_view
|
|||
int32_t joint_capacity;
|
||||
uint32_t reserved;
|
||||
uint64_t step_count;
|
||||
// Particle high-water counts and capacities (all 0 for a world created without particle capacity).
|
||||
int32_t particle_count;
|
||||
int32_t particle_capacity;
|
||||
int32_t chain_count;
|
||||
int32_t chain_capacity;
|
||||
int32_t attachment_count;
|
||||
int32_t attachment_capacity;
|
||||
uint32_t reserved_particles[2];
|
||||
// bodies
|
||||
const uint64_t* stable_id;
|
||||
const uint32_t* kind;
|
||||
|
|
@ -183,6 +192,48 @@ typedef struct ymir_box3d_soa_view
|
|||
// Per joint slot: counts the destroys of that slot. A grab's handle is (lane, generation); destroy_joint refuses
|
||||
// a handle whose generation is not the slot's, so a stale release cannot destroy a later grab in a reused slot.
|
||||
const uint32_t* joint_generation;
|
||||
// The impulse (N s, world) the particle phase delivered to the body over the last step, 0 for a body it did not
|
||||
// touch. Notification only: Box3D already applied it.
|
||||
const float* particle_impulse_x;
|
||||
const float* particle_impulse_y;
|
||||
const float* particle_impulse_z;
|
||||
// Particle lanes (read-only; only `step` and the chain and attachment commands change them), indexed by
|
||||
// particle up to particle_count. A free slot reads chain -1 and every lane 0. link_tension (N) is the load the
|
||||
// link from this particle to the next carries, mean over the substeps: a sensor, Ymir never splits on it.
|
||||
const float* particle_x;
|
||||
const float* particle_y;
|
||||
const float* particle_z;
|
||||
const float* particle_vx;
|
||||
const float* particle_vy;
|
||||
const float* particle_vz;
|
||||
const float* particle_radius;
|
||||
const float* particle_inv_mass;
|
||||
const float* particle_link_tension;
|
||||
const int32_t* particle_chain;
|
||||
// Chain lanes, indexed by chain lane up to chain_count. kind: 0 live, 1 removed (a tombstone keeps its last range).
|
||||
const uint64_t* chain_stable_id;
|
||||
const uint32_t* chain_kind;
|
||||
const uint32_t* chain_generation;
|
||||
const int32_t* chain_first;
|
||||
const int32_t* chain_size;
|
||||
const uint64_t* chain_removed_at;
|
||||
const uint64_t* chain_material_id;
|
||||
const float* chain_stretch_compliance;
|
||||
const float* chain_bend_compliance;
|
||||
const float* chain_friction;
|
||||
// Attachment lanes, indexed by attachment lane up to attachment_count. force (N) is what the attachment exerts on
|
||||
// the chain, mean over the substeps, tether load included: a sensor.
|
||||
const uint32_t* attachment_kind;
|
||||
const uint32_t* attachment_generation;
|
||||
const int32_t* attachment_particle;
|
||||
const int32_t* attachment_body;
|
||||
const float* attachment_point_x;
|
||||
const float* attachment_point_y;
|
||||
const float* attachment_point_z;
|
||||
const float* attachment_compliance;
|
||||
const float* attachment_force_x;
|
||||
const float* attachment_force_y;
|
||||
const float* attachment_force_z;
|
||||
} ymir_box3d_soa_view;
|
||||
|
||||
// A begin or end of touch between two bodies: their lane indices and the shapes' stable ids.
|
||||
|
|
@ -245,7 +296,8 @@ typedef struct ymir_box3d_soa_ray_hit
|
|||
#define YMIR_SOA_STATE_LANES 13
|
||||
#define YMIR_SOA_WRITE_LANES 6
|
||||
#define YMIR_SOA_CONTACT_IMPULSE_LANE (YMIR_SOA_STATE_LANES + YMIR_SOA_WRITE_LANES)
|
||||
#define YMIR_SOA_BODY_FLOAT_LANES (YMIR_SOA_CONTACT_IMPULSE_LANE + 1)
|
||||
#define YMIR_SOA_PARTICLE_IMPULSE_LANE (YMIR_SOA_CONTACT_IMPULSE_LANE + 1) // 3 lanes: x y z
|
||||
#define YMIR_SOA_BODY_FLOAT_LANES (YMIR_SOA_PARTICLE_IMPULSE_LANE + 3)
|
||||
#define YMIR_SOA_JOINT_FLOAT_LANES 15
|
||||
|
||||
typedef struct ymir_box3d_soa_world
|
||||
|
|
@ -281,6 +333,8 @@ typedef struct ymir_box3d_soa_world
|
|||
ymir_box3d_soa_touch_fact* end_facts;
|
||||
ymir_box3d_soa_hit_fact* hit_facts;
|
||||
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
|
||||
} ymir_box3d_soa_world;
|
||||
|
||||
static float* ymir_soa_lane(const ymir_box3d_soa_world* world, int lane)
|
||||
|
|
@ -434,12 +488,14 @@ static void ymir_soa_free(ymir_box3d_soa_world* world)
|
|||
free(world->end_facts);
|
||||
free(world->hit_facts);
|
||||
free(world->contact_scratch);
|
||||
ymir_particles_free(world->particles);
|
||||
free(world);
|
||||
}
|
||||
|
||||
YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_create(
|
||||
int32_t capacity, int32_t joint_capacity, float gravity_x, float gravity_y, float gravity_z,
|
||||
int32_t fact_capacity, float hit_threshold, ymir_box3d_soa_world** output)
|
||||
int32_t fact_capacity, float hit_threshold, int32_t particle_capacity, int32_t chain_capacity,
|
||||
int32_t attachment_capacity, ymir_box3d_soa_world** output)
|
||||
{
|
||||
if (output == NULL)
|
||||
{
|
||||
|
|
@ -448,7 +504,7 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_create(
|
|||
*output = NULL;
|
||||
if (capacity <= 0 || joint_capacity < 0 || fact_capacity <= 0 || !ymir_soa_finite(gravity_x) ||
|
||||
!ymir_soa_finite(gravity_y) || !ymir_soa_finite(gravity_z) || !ymir_soa_finite(hit_threshold) ||
|
||||
hit_threshold < 0.0f)
|
||||
hit_threshold < 0.0f || particle_capacity < 0 || chain_capacity < 0 || attachment_capacity < 0)
|
||||
{
|
||||
return YMIR_SOA_INVALID_ARGUMENT;
|
||||
}
|
||||
|
|
@ -486,6 +542,16 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_create(
|
|||
world->end_facts = (ymir_box3d_soa_touch_fact*)calloc((size_t)fact_capacity, sizeof(ymir_box3d_soa_touch_fact));
|
||||
world->hit_facts = (ymir_box3d_soa_hit_fact*)calloc((size_t)fact_capacity, sizeof(ymir_box3d_soa_hit_fact));
|
||||
world->contact_scratch = (b3ContactData*)calloc(n, sizeof(b3ContactData));
|
||||
world->gravity = (b3Vec3){gravity_x, gravity_y, gravity_z};
|
||||
if (particle_capacity > 0 || chain_capacity > 0 || attachment_capacity > 0)
|
||||
{
|
||||
world->particles = ymir_particles_create(particle_capacity, chain_capacity, attachment_capacity, capacity);
|
||||
if (world->particles == NULL)
|
||||
{
|
||||
ymir_soa_free(world);
|
||||
return YMIR_SOA_OUT_OF_MEMORY;
|
||||
}
|
||||
}
|
||||
if (world->contact_count == NULL || world->material_id == NULL || world->begin_facts == NULL ||
|
||||
world->end_facts == NULL || world->hit_facts == NULL || world->contact_scratch == NULL ||
|
||||
world->bodies == NULL || world->stable_id == NULL || world->kind == NULL || world->parent == NULL ||
|
||||
|
|
@ -995,6 +1061,74 @@ 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.
|
||||
static void ymir_soa_gather_proxies(ymir_box3d_soa_world* world)
|
||||
{
|
||||
ymir_particles* pt = world->particles;
|
||||
pt->body_count = world->count;
|
||||
for (int i = 0; i < world->count; ++i)
|
||||
{
|
||||
ymir_particle_proxy* b = pt->proxies + i;
|
||||
uint32_t kind = world->kind[i];
|
||||
b3BodyId body = world->bodies[i];
|
||||
b3Pos p = b3Body_GetPosition(body);
|
||||
b3Quat q = b3Body_GetRotation(body);
|
||||
b3Vec3 v = b3Body_GetLinearVelocity(body);
|
||||
b3Vec3 w = b3Body_GetAngularVelocity(body);
|
||||
memset(b, 0, sizeof(*b));
|
||||
b->shape = kind == YMIR_SOA_KIND_SPHERE ? YMIR_PROXY_SPHERE
|
||||
: kind == YMIR_SOA_KIND_CAPSULE ? YMIR_PROXY_CAPSULE
|
||||
: ymir_soa_is_anchor(kind) ? YMIR_PROXY_NONE
|
||||
: YMIR_PROXY_BOX;
|
||||
for (int k = 0; k < 3; ++k)
|
||||
{
|
||||
b->size[k] = world->size[(size_t)k * (size_t)world->capacity + (size_t)i];
|
||||
}
|
||||
b->position[0] = p.x; b->position[1] = p.y; b->position[2] = p.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;
|
||||
if (ymir_soa_is_dynamic(kind))
|
||||
{
|
||||
b3Matrix3 m = b3Body_GetWorldInverseRotationalInertia(body);
|
||||
b->inv_mass = 1.0f / b3Body_GetMass(body);
|
||||
b->inv_inertia[0] = m.cx.x; b->inv_inertia[1] = m.cy.x; b->inv_inertia[2] = m.cz.x;
|
||||
b->inv_inertia[3] = m.cy.y; b->inv_inertia[4] = m.cz.y; b->inv_inertia[5] = m.cz.z;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
{
|
||||
const ymir_particles* pt = world->particles;
|
||||
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];
|
||||
}
|
||||
if (!ymir_soa_is_dynamic(world->kind[i]))
|
||||
{
|
||||
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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_step(ymir_box3d_soa_world* world, float time_step, int32_t sub_step_count)
|
||||
{
|
||||
if (world == NULL || !ymir_soa_finite(time_step) || time_step <= 0.0f || sub_step_count <= 0)
|
||||
|
|
@ -1017,6 +1151,14 @@ 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)
|
||||
|
|
@ -1114,6 +1256,86 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_cast_ray_closest(
|
|||
return YMIR_SOA_OK;
|
||||
}
|
||||
|
||||
// Particle chains and attachments. Each command is the whole of its effect: Ymir never splits, destroys or detaches
|
||||
// on a threshold, so tear, cut, gib and release are the caller's say-so. Every refusal changes nothing. A world made
|
||||
// without particle capacity refuses them all.
|
||||
|
||||
YMIR_EXPORT uint32_t YMIR_CALL ymir_box3d_soa_get_chain_desc_size(void)
|
||||
{
|
||||
return (uint32_t)sizeof(ymir_chain_desc);
|
||||
}
|
||||
|
||||
YMIR_EXPORT uint32_t YMIR_CALL ymir_box3d_soa_get_attachment_desc_size(void)
|
||||
{
|
||||
return (uint32_t)sizeof(ymir_attachment_desc);
|
||||
}
|
||||
|
||||
YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_create_chain(
|
||||
ymir_box3d_soa_world* world, const ymir_chain_desc* d, int32_t* chain, uint32_t* generation)
|
||||
{
|
||||
if (world == NULL || world->particles == NULL)
|
||||
{
|
||||
return YMIR_SOA_INVALID_ARGUMENT;
|
||||
}
|
||||
return ymir_particles_create_chain(world->particles, d, world->step_count, chain, generation);
|
||||
}
|
||||
|
||||
YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_split_chain(
|
||||
ymir_box3d_soa_world* world, int32_t chain, uint32_t generation, int32_t link, uint64_t tail_stable_id, int32_t* tail,
|
||||
uint32_t* tail_generation)
|
||||
{
|
||||
if (world == NULL || world->particles == NULL)
|
||||
{
|
||||
return YMIR_SOA_INVALID_ARGUMENT;
|
||||
}
|
||||
return ymir_particles_split_chain(
|
||||
world->particles, chain, generation, link, tail_stable_id, world->step_count, tail, tail_generation);
|
||||
}
|
||||
|
||||
YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_destroy_chain(ymir_box3d_soa_world* world, int32_t chain, uint32_t generation)
|
||||
{
|
||||
if (world == NULL || world->particles == NULL)
|
||||
{
|
||||
return YMIR_SOA_INVALID_ARGUMENT;
|
||||
}
|
||||
return ymir_particles_destroy_chain(world->particles, chain, generation, world->step_count);
|
||||
}
|
||||
|
||||
YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_attach(
|
||||
ymir_box3d_soa_world* world, const ymir_attachment_desc* d, int32_t* attachment, uint32_t* generation)
|
||||
{
|
||||
if (world == NULL || world->particles == NULL)
|
||||
{
|
||||
return YMIR_SOA_INVALID_ARGUMENT;
|
||||
}
|
||||
return ymir_particles_attach(world->particles, d, world->count, world->step_count, attachment, generation);
|
||||
}
|
||||
|
||||
YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_detach(ymir_box3d_soa_world* world, int32_t attachment, uint32_t generation)
|
||||
{
|
||||
if (world == NULL || world->particles == NULL)
|
||||
{
|
||||
return YMIR_SOA_INVALID_ARGUMENT;
|
||||
}
|
||||
return ymir_particles_detach(world->particles, attachment, generation, world->step_count);
|
||||
}
|
||||
|
||||
// The largest run of particle slots a new chain could take right now.
|
||||
YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_free_particle_slots(const ymir_box3d_soa_world* world)
|
||||
{
|
||||
return world == NULL || world->particles == NULL ? 0 : ymir_particles_free_particle_slots(world->particles, world->step_count);
|
||||
}
|
||||
|
||||
YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_free_chain_slots(const ymir_box3d_soa_world* world)
|
||||
{
|
||||
return world == NULL || world->particles == NULL ? 0 : ymir_particles_free_chain_slots(world->particles, world->step_count);
|
||||
}
|
||||
|
||||
YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_free_attachment_slots(const ymir_box3d_soa_world* world)
|
||||
{
|
||||
return world == NULL || world->particles == NULL ? 0 : ymir_particles_free_attachment_slots(world->particles, world->step_count);
|
||||
}
|
||||
|
||||
YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_get_view(const ymir_box3d_soa_world* world, ymir_box3d_soa_view* view)
|
||||
{
|
||||
if (world == NULL || view == NULL)
|
||||
|
|
@ -1124,13 +1346,20 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_get_view(const ymir_box3d_soa_world
|
|||
size_t n = (size_t)world->capacity;
|
||||
float* jb = world->joint_lanes;
|
||||
size_t m = (size_t)world->joint_capacity;
|
||||
*view = (ymir_box3d_soa_view){
|
||||
const ymir_particles* pt = world->particles;
|
||||
*view =(ymir_box3d_soa_view){
|
||||
.abi_version = YMIR_SOA_ABI_VERSION,
|
||||
.count = world->count,
|
||||
.capacity = world->capacity,
|
||||
.joint_count = world->joint_count,
|
||||
.joint_capacity = world->joint_capacity,
|
||||
.step_count = world->step_count,
|
||||
.particle_count = pt != NULL ? pt->particle_count : 0,
|
||||
.particle_capacity = pt != NULL ? pt->particle_capacity : 0,
|
||||
.chain_count = pt != NULL ? pt->chain_count : 0,
|
||||
.chain_capacity = pt != NULL ? pt->chain_capacity : 0,
|
||||
.attachment_count = pt != NULL ? pt->attachment_count : 0,
|
||||
.attachment_capacity = pt != NULL ? pt->attachment_capacity : 0,
|
||||
.stable_id = world->stable_id,
|
||||
.kind = world->kind,
|
||||
.parent = world->parent,
|
||||
|
|
@ -1179,7 +1408,44 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_get_view(const ymir_box3d_soa_world
|
|||
.contact_count = world->contact_count,
|
||||
.material_id = world->material_id,
|
||||
.joint_generation = world->joint_generation,
|
||||
.particle_impulse_x = base + (YMIR_SOA_PARTICLE_IMPULSE_LANE + 0) * n,
|
||||
.particle_impulse_y = base + (YMIR_SOA_PARTICLE_IMPULSE_LANE + 1) * n,
|
||||
.particle_impulse_z = base + (YMIR_SOA_PARTICLE_IMPULSE_LANE + 2) * n,
|
||||
};
|
||||
if (pt != NULL)
|
||||
{
|
||||
view->particle_x = pt->pos_x;
|
||||
view->particle_y = pt->pos_y;
|
||||
view->particle_z = pt->pos_z;
|
||||
view->particle_vx = pt->vel_x;
|
||||
view->particle_vy = pt->vel_y;
|
||||
view->particle_vz = pt->vel_z;
|
||||
view->particle_radius = pt->radius;
|
||||
view->particle_inv_mass = pt->inv_mass;
|
||||
view->particle_link_tension = pt->link_tension;
|
||||
view->particle_chain = pt->chain;
|
||||
view->chain_stable_id = pt->chain_stable_id;
|
||||
view->chain_kind = pt->chain_kind;
|
||||
view->chain_generation = pt->chain_generation;
|
||||
view->chain_first = pt->chain_first;
|
||||
view->chain_size = pt->chain_size;
|
||||
view->chain_removed_at = pt->chain_removed_at;
|
||||
view->chain_material_id = pt->chain_material_id;
|
||||
view->chain_stretch_compliance = pt->chain_stretch;
|
||||
view->chain_bend_compliance = pt->chain_bend;
|
||||
view->chain_friction = pt->chain_friction;
|
||||
view->attachment_kind = pt->att_kind;
|
||||
view->attachment_generation = pt->att_generation;
|
||||
view->attachment_particle = pt->att_particle;
|
||||
view->attachment_body = pt->att_body;
|
||||
view->attachment_point_x = pt->att_point_x;
|
||||
view->attachment_point_y = pt->att_point_y;
|
||||
view->attachment_point_z = pt->att_point_z;
|
||||
view->attachment_compliance = pt->att_compliance;
|
||||
view->attachment_force_x = pt->att_force_x;
|
||||
view->attachment_force_y = pt->att_force_y;
|
||||
view->attachment_force_z = pt->att_force_z;
|
||||
}
|
||||
return YMIR_SOA_OK;
|
||||
}
|
||||
|
||||
|
|
|
|||
1150
native/Ymir.Box3D/ymir_particles.c
Normal file
1150
native/Ymir.Box3D/ymir_particles.c
Normal file
File diff suppressed because it is too large
Load diff
152
native/Ymir.Box3D/ymir_particles.h
Normal file
152
native/Ymir.Box3D/ymir_particles.h
Normal file
|
|
@ -0,0 +1,152 @@
|
|||
// Particle chains and their attachments: Ymir's own position-based solver, stepped by ymir_box3d_soa_step
|
||||
// after Box3D has stepped the rigid bodies. This module knows nothing of Box3D: ymir_box3d_soa.c is the only
|
||||
// Box3D caller. It fills one proxy per body (pose, velocity, shape, inverse mass and inertia) before the step,
|
||||
// the particle phase 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.
|
||||
//
|
||||
// Determinism: single thread; chains are Gauss-Seidel in link order; the one Jacobi pass (particle-particle
|
||||
// contact) is gather-form (each particle writes only its own correction, neighbours read in cell then index
|
||||
// order from a stable counting-sort grid); proxy impulses are summed per body in constraint order. Compiled
|
||||
// with -ffp-contract=off (/fp:precise on MSVC) so no fused multiply-add changes a byte.
|
||||
|
||||
#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
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
YMIR_CHAIN_LIVE = 0,
|
||||
YMIR_CHAIN_REMOVED = 1 // a destroyed chain's lane: reusable only once a step has passed
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
YMIR_ATTACH_PARTICLE_BODY = 0, // particle to a point fixed in a body (its lane)
|
||||
YMIR_ATTACH_PARTICLE_STATIC = 1, // particle to a world point
|
||||
YMIR_ATTACH_PARTICLE_PARTICLE = 2, // particle to another particle (its lane in `body`)
|
||||
YMIR_ATTACH_REMOVED = 3
|
||||
};
|
||||
|
||||
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 chain to create: `count` >= 2 particles at `positions` (xyz interleaved) with `masses` (kg, 0 = immovable).
|
||||
// Link and bend rest lengths are the creation distances.
|
||||
typedef struct ymir_chain_desc
|
||||
{
|
||||
uint64_t stable_id;
|
||||
int32_t count;
|
||||
uint32_t reserved;
|
||||
const float* positions;
|
||||
const float* masses;
|
||||
float radius;
|
||||
float stretch_compliance; // m/N, >= 0
|
||||
float bend_compliance; // m/N, >= 0
|
||||
float friction; // >= 0
|
||||
uint64_t material_id;
|
||||
} ymir_chain_desc;
|
||||
|
||||
// An attachment of `particle`. `body` is the body lane (particle-body), the other particle's lane
|
||||
// (particle-particle) or -1. `point` is in the body's frame (particle-body) or the world (particle-static).
|
||||
typedef struct ymir_attachment_desc
|
||||
{
|
||||
uint32_t kind;
|
||||
int32_t particle;
|
||||
int32_t body;
|
||||
float point[3];
|
||||
float compliance; // m/N, >= 0
|
||||
uint32_t reserved;
|
||||
} ymir_attachment_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];
|
||||
float angular_impulse[3];
|
||||
} ymir_particle_proxy;
|
||||
|
||||
typedef struct ymir_particles
|
||||
{
|
||||
int32_t particle_capacity, chain_capacity, attachment_capacity, body_capacity;
|
||||
int32_t particle_count, chain_count, attachment_count; // high-water marks
|
||||
int32_t body_count; // bodies filled in `proxies` this step
|
||||
int32_t live_chains;
|
||||
ymir_particle_proxy* proxies;
|
||||
|
||||
// particle lanes
|
||||
float *pos_x, *pos_y, *pos_z, *vel_x, *vel_y, *vel_z, *radius, *inv_mass;
|
||||
float* link_tension; // N, link (i, i+1) on particle i: mean over the substeps; a sensor
|
||||
int32_t* chain; // chain lane, -1 for a free slot
|
||||
|
||||
// chain lanes
|
||||
uint64_t* chain_stable_id;
|
||||
uint32_t* chain_kind;
|
||||
uint32_t* chain_generation;
|
||||
int32_t* chain_first;
|
||||
int32_t* chain_size;
|
||||
uint64_t* chain_removed_at;
|
||||
uint64_t* chain_material_id;
|
||||
float *chain_stretch, *chain_bend, *chain_friction;
|
||||
|
||||
// attachment lanes
|
||||
uint32_t* att_kind;
|
||||
uint32_t* att_generation;
|
||||
int32_t* att_particle;
|
||||
int32_t* 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 the chain (mean); a sensor
|
||||
uint64_t* att_removed_at;
|
||||
|
||||
// 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;
|
||||
int32_t *skip_body, *cell_x, *cell_y, *cell_z, *cell_start, *cell_fill, *sorted;
|
||||
uint32_t table_mask;
|
||||
uint8_t* mark;
|
||||
void* arena;
|
||||
} ymir_particles;
|
||||
|
||||
// Creates the state for the given capacities (all >= 0) and `body_capacity` proxies; NULL on failure.
|
||||
ymir_particles* ymir_particles_create(int32_t particle_capacity, int32_t chain_capacity, int32_t attachment_capacity, int32_t body_capacity);
|
||||
void ymir_particles_free(ymir_particles* p);
|
||||
|
||||
// Commands. `step_count` is the world's completed steps: a removed lane is reused once a step has passed.
|
||||
// Every refusal happens before anything changes.
|
||||
int32_t ymir_particles_create_chain(ymir_particles* p, const ymir_chain_desc* d, uint64_t step_count, int32_t* chain, uint32_t* generation);
|
||||
int32_t ymir_particles_split_chain(ymir_particles* p, int32_t chain, uint32_t generation, int32_t link, uint64_t tail_stable_id, uint64_t step_count, int32_t* tail, uint32_t* tail_generation);
|
||||
int32_t ymir_particles_destroy_chain(ymir_particles* p, int32_t chain, uint32_t generation, uint64_t step_count);
|
||||
int32_t ymir_particles_attach(ymir_particles* p, const ymir_attachment_desc* d, int32_t body_count, uint64_t step_count, int32_t* attachment, uint32_t* generation);
|
||||
int32_t ymir_particles_detach(ymir_particles* p, int32_t attachment, uint32_t generation, uint64_t step_count);
|
||||
int32_t ymir_particles_free_particle_slots(const ymir_particles* p, uint64_t step_count); // the largest free run
|
||||
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);
|
||||
|
||||
#endif
|
||||
|
|
@ -67,6 +67,62 @@ internal struct Box3DSoaRayHitNative
|
|||
public uint Reserved;
|
||||
}
|
||||
|
||||
/// <summary>Native layout of <c>ymir_chain_desc</c>.</summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal unsafe struct Box3DSoaChainDesc
|
||||
{
|
||||
public ulong StableId;
|
||||
public int Count;
|
||||
public uint Reserved;
|
||||
public float* Positions;
|
||||
public float* Masses;
|
||||
public float Radius, StretchCompliance, BendCompliance, Friction;
|
||||
public ulong MaterialId;
|
||||
}
|
||||
|
||||
/// <summary>Native layout of <c>ymir_attachment_desc</c>.</summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct Box3DSoaAttachmentDesc
|
||||
{
|
||||
public uint Kind;
|
||||
public int Particle;
|
||||
public int Body;
|
||||
public float PointX, PointY, PointZ;
|
||||
public float Compliance;
|
||||
public uint Reserved;
|
||||
}
|
||||
|
||||
public enum Box3DSoaAttachKind : uint
|
||||
{
|
||||
/// <summary>The particle to a point fixed in a body: <c>target</c> is the body lane, <c>point</c> is in the body's frame.</summary>
|
||||
Body = 0,
|
||||
/// <summary>The particle to a world point (<c>point</c>); <c>target</c> is ignored.</summary>
|
||||
Static = 1,
|
||||
/// <summary>The particle to another particle: <c>target</c> is the other particle's lane.</summary>
|
||||
Particle = 2,
|
||||
/// <summary>The kind lane of a detached attachment (a tombstone).</summary>
|
||||
Removed = 3
|
||||
}
|
||||
|
||||
public enum Box3DSoaChainKind : uint
|
||||
{
|
||||
Live = 0,
|
||||
/// <summary>A destroyed chain's lane: reusable once a step has passed; it keeps its last range until then.</summary>
|
||||
Removed = 1
|
||||
}
|
||||
|
||||
/// <summary>A chain's identity: its lane and the slot's generation when it was created or split off.</summary>
|
||||
public readonly record struct Box3DSoaChainHandle(int Lane, uint Generation)
|
||||
{
|
||||
public static implicit operator int(Box3DSoaChainHandle handle) => handle.Lane;
|
||||
}
|
||||
|
||||
/// <summary>An attachment's identity: its lane and the slot's generation when it was made.</summary>
|
||||
public readonly record struct Box3DSoaAttachmentHandle(int Lane, uint Generation)
|
||||
{
|
||||
public static implicit operator int(Box3DSoaAttachmentHandle handle) => handle.Lane;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A joint's identity: its lane and the slot's generation when it was added. Converts to the lane for reading lanes;
|
||||
/// <see cref="Box3DSoaWorld.DestroyJoint"/> takes the whole handle and refuses one from an earlier occupant of the slot.
|
||||
|
|
@ -91,6 +147,14 @@ internal unsafe struct Box3DSoaViewNative
|
|||
public int JointCapacity;
|
||||
public uint Reserved;
|
||||
public ulong StepCount;
|
||||
public int ParticleCount;
|
||||
public int ParticleCapacity;
|
||||
public int ChainCount;
|
||||
public int ChainCapacity;
|
||||
public int AttachmentCount;
|
||||
public int AttachmentCapacity;
|
||||
public uint ReservedParticles0;
|
||||
public uint ReservedParticles1;
|
||||
public ulong* StableId;
|
||||
public uint* Kind;
|
||||
public int* Parent;
|
||||
|
|
@ -139,6 +203,40 @@ internal unsafe struct Box3DSoaViewNative
|
|||
public int* ContactCount;
|
||||
public ulong* MaterialId;
|
||||
public uint* JointGeneration;
|
||||
public float* ParticleImpulseX;
|
||||
public float* ParticleImpulseY;
|
||||
public float* ParticleImpulseZ;
|
||||
public float* ParticleX;
|
||||
public float* ParticleY;
|
||||
public float* ParticleZ;
|
||||
public float* ParticleVx;
|
||||
public float* ParticleVy;
|
||||
public float* ParticleVz;
|
||||
public float* ParticleRadius;
|
||||
public float* ParticleInvMass;
|
||||
public float* ParticleLinkTension;
|
||||
public int* ParticleChain;
|
||||
public ulong* ChainStableId;
|
||||
public uint* ChainKind;
|
||||
public uint* ChainGeneration;
|
||||
public int* ChainFirst;
|
||||
public int* ChainSize;
|
||||
public ulong* ChainRemovedAt;
|
||||
public ulong* ChainMaterialId;
|
||||
public float* ChainStretchCompliance;
|
||||
public float* ChainBendCompliance;
|
||||
public float* ChainFriction;
|
||||
public uint* AttachmentKind;
|
||||
public uint* AttachmentGeneration;
|
||||
public int* AttachmentParticle;
|
||||
public int* AttachmentBody;
|
||||
public float* AttachmentPointX;
|
||||
public float* AttachmentPointY;
|
||||
public float* AttachmentPointZ;
|
||||
public float* AttachmentCompliance;
|
||||
public float* AttachmentForceX;
|
||||
public float* AttachmentForceY;
|
||||
public float* AttachmentForceZ;
|
||||
}
|
||||
|
||||
/// <summary>Native layout of <c>ymir_box3d_soa_facts</c>: counts, dropped counters, then three pointers.</summary>
|
||||
|
|
@ -256,7 +354,7 @@ internal static unsafe partial class Box3DSoaNative
|
|||
|
||||
[LibraryImport(Library)]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
|
||||
internal static partial int ymir_box3d_soa_create(int capacity, int jointCapacity, float gx, float gy, float gz, int factCapacity, float hitThreshold, out nint world);
|
||||
internal static partial int ymir_box3d_soa_create(int capacity, int jointCapacity, float gx, float gy, float gz, int factCapacity, float hitThreshold, int particleCapacity, int chainCapacity, int attachmentCapacity, out nint world);
|
||||
|
||||
[LibraryImport(Library)]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
|
||||
|
|
@ -301,6 +399,46 @@ internal static unsafe partial class Box3DSoaNative
|
|||
[LibraryImport(Library)]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
|
||||
internal static partial int ymir_box3d_soa_cast_ray_closest(nint world, float* origin, float* translation, out Box3DSoaRayHitNative hit);
|
||||
|
||||
[LibraryImport(Library)]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
|
||||
internal static partial uint ymir_box3d_soa_get_chain_desc_size();
|
||||
|
||||
[LibraryImport(Library)]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
|
||||
internal static partial uint ymir_box3d_soa_get_attachment_desc_size();
|
||||
|
||||
[LibraryImport(Library)]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
|
||||
internal static partial int ymir_box3d_soa_create_chain(nint world, in Box3DSoaChainDesc desc, out int chain, out uint generation);
|
||||
|
||||
[LibraryImport(Library)]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
|
||||
internal static partial int ymir_box3d_soa_split_chain(nint world, int chain, uint generation, int link, ulong tailStableId, out int tail, out uint tailGeneration);
|
||||
|
||||
[LibraryImport(Library)]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
|
||||
internal static partial int ymir_box3d_soa_destroy_chain(nint world, int chain, uint generation);
|
||||
|
||||
[LibraryImport(Library)]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
|
||||
internal static partial int ymir_box3d_soa_attach(nint world, in Box3DSoaAttachmentDesc desc, out int attachment, out uint generation);
|
||||
|
||||
[LibraryImport(Library)]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
|
||||
internal static partial int ymir_box3d_soa_detach(nint world, int attachment, uint generation);
|
||||
|
||||
[LibraryImport(Library)]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
|
||||
internal static partial int ymir_box3d_soa_free_particle_slots(nint world);
|
||||
|
||||
[LibraryImport(Library)]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
|
||||
internal static partial int ymir_box3d_soa_free_chain_slots(nint world);
|
||||
|
||||
[LibraryImport(Library)]
|
||||
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
|
||||
internal static partial int ymir_box3d_soa_free_attachment_slots(nint world);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -412,6 +550,55 @@ public readonly unsafe struct Box3DSoaView
|
|||
public ReadOnlySpan<float> JointOriginY => Lane(_v.JointOriginY, JointCount);
|
||||
public ReadOnlySpan<float> JointOriginZ => Lane(_v.JointOriginZ, JointCount);
|
||||
|
||||
// Particle lanes: read-only, changed only by Step and by the chain and attachment commands. Particle lanes are indexed
|
||||
// by particle up to ParticleCount (a high-water mark); a free slot reads chain -1 and every lane 0.
|
||||
public int ParticleCount => _v.ParticleCount;
|
||||
public int ParticleCapacity => _v.ParticleCapacity;
|
||||
public int ChainCount => _v.ChainCount;
|
||||
public int AttachmentCount => _v.AttachmentCount;
|
||||
public ReadOnlySpan<float> ParticleX => Lane(_v.ParticleX, ParticleCount);
|
||||
public ReadOnlySpan<float> ParticleY => Lane(_v.ParticleY, ParticleCount);
|
||||
public ReadOnlySpan<float> ParticleZ => Lane(_v.ParticleZ, ParticleCount);
|
||||
public ReadOnlySpan<float> ParticleVx => Lane(_v.ParticleVx, ParticleCount);
|
||||
public ReadOnlySpan<float> ParticleVy => Lane(_v.ParticleVy, ParticleCount);
|
||||
public ReadOnlySpan<float> ParticleVz => Lane(_v.ParticleVz, ParticleCount);
|
||||
public ReadOnlySpan<float> ParticleRadius => Lane(_v.ParticleRadius, ParticleCount);
|
||||
public ReadOnlySpan<float> ParticleInvMass => Lane(_v.ParticleInvMass, ParticleCount);
|
||||
/// <summary>N: the load the link from this particle to the next carries, mean over the substeps, tether load included. A sensor: Ymir never splits on it.</summary>
|
||||
public ReadOnlySpan<float> ParticleLinkTension => Lane(_v.ParticleLinkTension, ParticleCount);
|
||||
/// <summary>The particle's chain lane, -1 for a free slot.</summary>
|
||||
public ReadOnlySpan<int> ParticleChain => Lane(_v.ParticleChain, ParticleCount);
|
||||
|
||||
public ReadOnlySpan<ulong> ChainStableId => Lane(_v.ChainStableId, ChainCount);
|
||||
public ReadOnlySpan<uint> ChainKind => Lane(_v.ChainKind, ChainCount);
|
||||
public ReadOnlySpan<uint> ChainGeneration => Lane(_v.ChainGeneration, ChainCount);
|
||||
public ReadOnlySpan<int> ChainFirst => Lane(_v.ChainFirst, ChainCount);
|
||||
public ReadOnlySpan<int> ChainSize => Lane(_v.ChainSize, ChainCount);
|
||||
public ReadOnlySpan<ulong> ChainRemovedAt => Lane(_v.ChainRemovedAt, ChainCount);
|
||||
public ReadOnlySpan<ulong> ChainMaterialId => Lane(_v.ChainMaterialId, ChainCount);
|
||||
public ReadOnlySpan<float> ChainStretchCompliance => Lane(_v.ChainStretchCompliance, ChainCount);
|
||||
public ReadOnlySpan<float> ChainBendCompliance => Lane(_v.ChainBendCompliance, ChainCount);
|
||||
public ReadOnlySpan<float> ChainFriction => Lane(_v.ChainFriction, ChainCount);
|
||||
|
||||
public ReadOnlySpan<uint> AttachmentKind => Lane(_v.AttachmentKind, AttachmentCount);
|
||||
public ReadOnlySpan<uint> AttachmentGeneration => Lane(_v.AttachmentGeneration, AttachmentCount);
|
||||
public ReadOnlySpan<int> AttachmentParticle => Lane(_v.AttachmentParticle, AttachmentCount);
|
||||
/// <summary>The body lane (<see cref="Box3DSoaAttachKind.Body"/>), the other particle's lane (<see cref="Box3DSoaAttachKind.Particle"/>) or -1.</summary>
|
||||
public ReadOnlySpan<int> AttachmentBody => Lane(_v.AttachmentBody, AttachmentCount);
|
||||
public ReadOnlySpan<float> AttachmentPointX => Lane(_v.AttachmentPointX, AttachmentCount);
|
||||
public ReadOnlySpan<float> AttachmentPointY => Lane(_v.AttachmentPointY, AttachmentCount);
|
||||
public ReadOnlySpan<float> AttachmentPointZ => Lane(_v.AttachmentPointZ, AttachmentCount);
|
||||
public ReadOnlySpan<float> AttachmentCompliance => Lane(_v.AttachmentCompliance, AttachmentCount);
|
||||
/// <summary>N: what the attachment exerts on the chain, mean over the substeps, tether load included. A sensor.</summary>
|
||||
public ReadOnlySpan<float> AttachmentForceX => Lane(_v.AttachmentForceX, AttachmentCount);
|
||||
public ReadOnlySpan<float> AttachmentForceY => Lane(_v.AttachmentForceY, AttachmentCount);
|
||||
public ReadOnlySpan<float> AttachmentForceZ => Lane(_v.AttachmentForceZ, AttachmentCount);
|
||||
|
||||
/// <summary>N s, world: the impulse the particle phase delivered to each body over the last step (0 for a body it did not touch). Box3D has already applied it; this is notification only.</summary>
|
||||
public ReadOnlySpan<float> ParticleImpulseX => Lane(_v.ParticleImpulseX, Count);
|
||||
public ReadOnlySpan<float> ParticleImpulseY => Lane(_v.ParticleImpulseY, Count);
|
||||
public ReadOnlySpan<float> ParticleImpulseZ => Lane(_v.ParticleImpulseZ, Count);
|
||||
|
||||
/// <summary>Address of a lane, for tests and interop that need to prove nothing was copied.</summary>
|
||||
public nint PositionYAddress => (nint)_v.PositionY;
|
||||
}
|
||||
|
|
@ -424,7 +611,7 @@ public readonly unsafe struct Box3DSoaView
|
|||
/// </summary>
|
||||
public sealed class Box3DSoaWorld : IDisposable
|
||||
{
|
||||
private const uint ExpectedAbi = 8;
|
||||
private const uint ExpectedAbi = 9;
|
||||
private nint _handle;
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -433,7 +620,8 @@ public sealed class Box3DSoaWorld : IDisposable
|
|||
/// </summary>
|
||||
public Box3DSoaWorld(
|
||||
int capacity, float gravityX = 0f, float gravityY = -10f, float gravityZ = 0f, int jointCapacity = 0,
|
||||
int? factCapacity = null, float hitThreshold = 1f)
|
||||
int? factCapacity = null, float hitThreshold = 1f,
|
||||
int particleCapacity = 0, int chainCapacity = 0, int attachmentCapacity = 0)
|
||||
{
|
||||
RequireNativeAbi(Box3DSoaNative.ymir_box3d_soa_get_abi_version());
|
||||
if (Box3DSoaNative.ymir_box3d_soa_get_view_size() != (uint)System.Runtime.CompilerServices.Unsafe.SizeOf<Box3DSoaViewNative>() ||
|
||||
|
|
@ -442,12 +630,14 @@ public sealed class Box3DSoaWorld : IDisposable
|
|||
Box3DSoaNative.ymir_box3d_soa_get_facts_size() != (uint)System.Runtime.CompilerServices.Unsafe.SizeOf<Box3DSoaFactsNative>() ||
|
||||
Box3DSoaNative.ymir_box3d_soa_get_touch_fact_size() != (uint)System.Runtime.CompilerServices.Unsafe.SizeOf<Box3DSoaTouch>() ||
|
||||
Box3DSoaNative.ymir_box3d_soa_get_hit_fact_size() != (uint)System.Runtime.CompilerServices.Unsafe.SizeOf<Box3DSoaHit>() ||
|
||||
Box3DSoaNative.ymir_box3d_soa_get_ray_hit_size() != (uint)System.Runtime.CompilerServices.Unsafe.SizeOf<Box3DSoaRayHitNative>())
|
||||
Box3DSoaNative.ymir_box3d_soa_get_ray_hit_size() != (uint)System.Runtime.CompilerServices.Unsafe.SizeOf<Box3DSoaRayHitNative>() ||
|
||||
Box3DSoaNative.ymir_box3d_soa_get_chain_desc_size() != (uint)System.Runtime.CompilerServices.Unsafe.SizeOf<Box3DSoaChainDesc>() ||
|
||||
Box3DSoaNative.ymir_box3d_soa_get_attachment_desc_size() != (uint)System.Runtime.CompilerServices.Unsafe.SizeOf<Box3DSoaAttachmentDesc>())
|
||||
{
|
||||
throw new InvalidOperationException("The native Ymir Box3D SoA ABI does not match this build.");
|
||||
}
|
||||
|
||||
Check(Box3DSoaNative.ymir_box3d_soa_create(capacity, jointCapacity, gravityX, gravityY, gravityZ, factCapacity ?? checked(capacity * 4), hitThreshold, out _handle));
|
||||
Check(Box3DSoaNative.ymir_box3d_soa_create(capacity, jointCapacity, gravityX, gravityY, gravityZ, factCapacity ?? checked(capacity * 4), hitThreshold, particleCapacity, chainCapacity, attachmentCapacity, out _handle));
|
||||
}
|
||||
|
||||
/// <summary>Fails closed when the native library is not the ABI this build expects, in either direction.</summary>
|
||||
|
|
@ -658,6 +848,97 @@ public sealed class Box3DSoaWorld : IDisposable
|
|||
return new Box3DSoaRayHit(h.Hit != 0, h.Lane, h.StableId, (h.PointX, h.PointY, h.PointZ), (h.NormalX, h.NormalY, h.NormalZ), h.Fraction);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a particle chain of <c>positions.Length</c> (at least 2) particles, in a free range of the particle lanes.
|
||||
/// <paramref name="masses"/> are kg per particle (0 = immovable); link and bend rest lengths are the creation distances.
|
||||
/// Compliances are m/N (0 = rigid). Returns the chain's handle. Throws for a full world, a duplicate stable id among live
|
||||
/// chains, a link of no length or non-finite input; nothing changes when it throws.
|
||||
/// </summary>
|
||||
public unsafe Box3DSoaChainHandle CreateChain(
|
||||
ulong stableId, ReadOnlySpan<(float X, float Y, float Z)> positions, ReadOnlySpan<float> masses, float radius,
|
||||
float stretchCompliance = 0f, float bendCompliance = 1e-4f, float friction = 0.3f, ulong materialId = 0)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_handle == 0, this);
|
||||
if (masses.Length != positions.Length) throw new ArgumentException("One mass per particle.", nameof(masses));
|
||||
var xyz = new float[positions.Length * 3];
|
||||
for (var i = 0; i < positions.Length; i++)
|
||||
{
|
||||
xyz[3 * i] = positions[i].X; xyz[3 * i + 1] = positions[i].Y; xyz[3 * i + 2] = positions[i].Z;
|
||||
}
|
||||
fixed (float* p = xyz)
|
||||
fixed (float* m = masses)
|
||||
{
|
||||
var desc = new Box3DSoaChainDesc
|
||||
{
|
||||
StableId = stableId, Count = positions.Length, Positions = p, Masses = m, Radius = radius,
|
||||
StretchCompliance = stretchCompliance, BendCompliance = bendCompliance, Friction = friction, MaterialId = materialId,
|
||||
};
|
||||
Check(Box3DSoaNative.ymir_box3d_soa_create_chain(_handle, desc, out var lane, out var generation));
|
||||
return new Box3DSoaChainHandle(lane, generation);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A chain whose particles all weigh <paramref name="massPerParticle"/> kg.</summary>
|
||||
public Box3DSoaChainHandle CreateChain(
|
||||
ulong stableId, ReadOnlySpan<(float X, float Y, float Z)> positions, float massPerParticle, float radius,
|
||||
float stretchCompliance = 0f, float bendCompliance = 1e-4f, float friction = 0.3f, ulong materialId = 0)
|
||||
{
|
||||
var masses = new float[positions.Length];
|
||||
Array.Fill(masses, massPerParticle);
|
||||
return CreateChain(stableId, positions, masses, radius, stretchCompliance, bendCompliance, friction, materialId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cuts the link after particle <paramref name="link"/> (0 to size - 2): the head keeps its lane and stable id, the tail
|
||||
/// takes a new chain lane and <paramref name="tailStableId"/>. No particle changes lane or address, attachments stay put,
|
||||
/// and the cut link's tension reads 0. A command: Ymir never splits a chain on its own.
|
||||
/// </summary>
|
||||
public Box3DSoaChainHandle SplitChain(Box3DSoaChainHandle chain, int link, ulong tailStableId)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_handle == 0, this);
|
||||
Check(Box3DSoaNative.ymir_box3d_soa_split_chain(_handle, chain.Lane, chain.Generation, link, tailStableId, out var lane, out var generation));
|
||||
return new Box3DSoaChainHandle(lane, generation);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a chain and every attachment that reaches its particles. Its lane becomes a tombstone (kind Removed) and its
|
||||
/// particle slots read 0; both are reused only once a <see cref="Step"/> has completed. A stale handle is refused.
|
||||
/// </summary>
|
||||
public void DestroyChain(Box3DSoaChainHandle chain)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_handle == 0, this);
|
||||
Check(Box3DSoaNative.ymir_box3d_soa_destroy_chain(_handle, chain.Lane, chain.Generation));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ties <paramref name="particle"/> to a body point (kind Body: <paramref name="target"/> is the body lane, <paramref name="point"/> in
|
||||
/// its frame), a world point (Static) or another particle (Particle: <paramref name="target"/> is its lane). Also gives the chain
|
||||
/// long-range tethers from this point. <paramref name="compliance"/> is m/N, 0 for rigid. Attach where the particle already is.
|
||||
/// </summary>
|
||||
public Box3DSoaAttachmentHandle Attach(
|
||||
Box3DSoaAttachKind kind, int particle, int target = -1, (float X, float Y, float Z) point = default, float compliance = 0f)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_handle == 0, this);
|
||||
var desc = new Box3DSoaAttachmentDesc
|
||||
{
|
||||
Kind = (uint)kind, Particle = particle, Body = target, PointX = point.X, PointY = point.Y, PointZ = point.Z, Compliance = compliance,
|
||||
};
|
||||
Check(Box3DSoaNative.ymir_box3d_soa_attach(_handle, desc, out var lane, out var generation));
|
||||
return new Box3DSoaAttachmentHandle(lane, generation);
|
||||
}
|
||||
|
||||
/// <summary>Releases an attachment; its lane is a tombstone (kind Removed) until a step has completed. A stale handle is refused.</summary>
|
||||
public void Detach(Box3DSoaAttachmentHandle attachment)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_handle == 0, this);
|
||||
Check(Box3DSoaNative.ymir_box3d_soa_detach(_handle, attachment.Lane, attachment.Generation));
|
||||
}
|
||||
|
||||
/// <summary>The largest run of particle slots a new chain could take right now.</summary>
|
||||
public int FreeParticleSlots { get { ObjectDisposedException.ThrowIf(_handle == 0, this); return Box3DSoaNative.ymir_box3d_soa_free_particle_slots(_handle); } }
|
||||
public int FreeChainSlots { get { ObjectDisposedException.ThrowIf(_handle == 0, this); return Box3DSoaNative.ymir_box3d_soa_free_chain_slots(_handle); } }
|
||||
public int FreeAttachmentSlots { get { ObjectDisposedException.ThrowIf(_handle == 0, this); return Box3DSoaNative.ymir_box3d_soa_free_attachment_slots(_handle); } }
|
||||
|
||||
private static Box3DSoaBodyDesc Describe(
|
||||
Box3DSoaKind kind, ulong stableId, (float X, float Y, float Z) position,
|
||||
(float X, float Y, float Z, float W) rotation, (float, float, float) size, float density,
|
||||
|
|
|
|||
|
|
@ -167,10 +167,10 @@ public sealed class Box3DSoaGrabTests
|
|||
[Fact]
|
||||
public void The_managed_side_refuses_a_native_library_of_another_abi_in_either_direction()
|
||||
{
|
||||
Assert.Equal(8u, Box3DSoaNative.ymir_box3d_soa_get_abi_version());
|
||||
Box3DSoaWorld.RequireNativeAbi(8);
|
||||
Assert.Throws<InvalidOperationException>(() => Box3DSoaWorld.RequireNativeAbi(7)); // an older native under this managed build
|
||||
Assert.Throws<InvalidOperationException>(() => Box3DSoaWorld.RequireNativeAbi(9)); // a newer one
|
||||
Assert.Equal(9u, Box3DSoaNative.ymir_box3d_soa_get_abi_version());
|
||||
Box3DSoaWorld.RequireNativeAbi(9);
|
||||
Assert.Throws<InvalidOperationException>(() => Box3DSoaWorld.RequireNativeAbi(8)); // an older native under this managed build
|
||||
Assert.Throws<InvalidOperationException>(() => Box3DSoaWorld.RequireNativeAbi(10)); // a newer one
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
|
|||
Loading…
Reference in a new issue