Call collide from the simulation, after the move rather than before it
The collide slot has existed unused since the backend was written. It is wired in now, and its signature changes from (self, a1, a2) to (self, actor, dt): the old shape had nowhere to put a normal, a depth, or a piece of map geometry, and no answer to which of the two actors it was supposed to be resolving. **Resolution runs after `move`.** That is the decision everything else follows from. A resolver called from movementlogicfunc runs before gravity, drag and the move, so it has to predict where the actor will end up -- which means re-implementing the integrator's arithmetic in the game, and being wrong whenever the integrator changes. examples/sidescroller/collision.c carries forty lines of exactly that, and says so. Only `move` is subdivided. Gravity, drag, the thrust integration and the speed ellipse all still run once against the whole dt, and sum(subdt) is dt, so an actor with nothing to collide with takes one sub-step of exactly dt -- `dt/1.0f` is dt exactly in IEEE-754 -- and follows the arithmetic path it always did. That is what lets the integrator stay untouched and every recorded physics number stay valid, and tests/physics.c and tests/physics_sim.c pass unchanged with no world attached. Collision is opt-in. A backend with no collision world costs one comparison per actor per step and does nothing else. Also here: - `self->gravity` was never null-checked and is dereferenced unconditionally, so a hand-built backend with only `move` filled in crashed rather than reporting. - Children are re-snapped in a post-pass, gated on collision being on. The in-loop snap uses whatever position the parent had when the child's slot came up in pool order, which is already sometimes a frame stale; that was harmless while a parent only moved by v*dt and is not once a parent can be pushed out of a wall mid-step. - tests/physics.c's deliberate tripwire has been visited. It asserted that arcade collide always raised AKERR_API, with a comment saying it existed so that implementing collision would have to come back here. What replaces it asserts the opt-in contract instead. - physics_sim.c's hard-coded "%d of 7 simulations" is derived now. A literal goes stale the first time somebody adds a simulation, which is this commit. Three new whole-motion simulations, and the third took two attempts to make honest: - Landing and resting: 0.0000 px of drift over 120 steps after settling. - Walking after landing: 107 px in a second, which is the symptom a player sees when a resting actor is caught on the ground it is standing on. - A fast fall not tunnelling. The first version used gravity, so the step size grew every frame and the actor happened to land *inside* the floor rather than stepping over it -- it passed with sub-stepping disabled and proved nothing. It uses a constant 1200 px/s now, so every step is exactly 60 pixels against a 32-pixel window in which an overlap exists, and the samples miss it cleanly. With sub-stepping the actor stops at y=288; without it, y=1300. Co-Authored-By: Claude Code <noreply@anthropic.com> Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
310
src/collision.c
310
src/collision.c
@@ -17,6 +17,8 @@
|
||||
#include <akstdlib.h>
|
||||
#include <akgl/collision_arena.h>
|
||||
#include <akgl/error.h>
|
||||
#include <akgl/actor.h>
|
||||
#include <akgl/heap.h>
|
||||
#include <akgl/tilemap.h>
|
||||
#include <akgl/types.h>
|
||||
|
||||
@@ -56,6 +58,24 @@
|
||||
/** @brief Tiles akgl_collision_settle will lift a shape before giving up. */
|
||||
#define AKGL_COLLISION_SETTLE_STEPS 4
|
||||
|
||||
/**
|
||||
* @brief Fraction of the thinnest passable thickness one sub-step may cover.
|
||||
*
|
||||
* Half. Anything that moves less than half the thinnest thing it could pass
|
||||
* through cannot be on both sides of it between two samples.
|
||||
*/
|
||||
#define AKGL_COLLISION_SUBSTEP_FRACTION 0.5f
|
||||
|
||||
/**
|
||||
* @brief Hard ceiling on sub-steps per actor per step.
|
||||
*
|
||||
* A cost bound. Above roughly `MAX_SUBSTEPS * FRACTION * cellsize` per step an
|
||||
* actor can still pass through a wall -- about 1280 px/s on 16-pixel tiles at
|
||||
* the default step, which is a projectile and not a walker. The real answer is
|
||||
* a swept narrowphase; #AKGL_COLLISION_FLAG_BULLET reserves the bit for it.
|
||||
*/
|
||||
#define AKGL_COLLISION_MAX_SUBSTEPS 8
|
||||
|
||||
/** @brief One positioned shape, in the form libccd's callbacks read. */
|
||||
typedef struct {
|
||||
uint8_t kind; /**< AKGL_COLLISION_SHAPE_*. */
|
||||
@@ -675,3 +695,293 @@ akerr_ErrorContext *akgl_collision_settle(akgl_CollisionWorld *self, akgl_Collis
|
||||
maxsteps
|
||||
);
|
||||
}
|
||||
|
||||
/** @brief Carries one actor's resolution through the broad-phase visitor. */
|
||||
typedef struct {
|
||||
akgl_CollisionWorld *world;
|
||||
akgl_Actor *actor;
|
||||
akgl_CollisionProxy *proxy;
|
||||
float32_t dt;
|
||||
akerr_ErrorContext *failure;
|
||||
} collision_resolve_state;
|
||||
|
||||
/**
|
||||
* @brief Test one candidate against the actor being resolved, and answer it.
|
||||
*
|
||||
* The visitor cannot use CATCH: a `break` here would leave the broad phase's own
|
||||
* walk rather than this function, so a failure would look like the end of the
|
||||
* list. The status is carried out on the state instead and handed over by the
|
||||
* caller.
|
||||
*/
|
||||
static akerr_ErrorContext *collision_resolve_visit(akgl_CollisionProxy *other, void *data)
|
||||
{
|
||||
collision_resolve_state *state = (collision_resolve_state *)data;
|
||||
akgl_Contact contact;
|
||||
bool interacts = false;
|
||||
bool hit = false;
|
||||
|
||||
PREPARE_ERROR(errctx);
|
||||
FAIL_ZERO_RETURN(errctx, other, AKERR_NULLPOINTER, "NULL proxy in a resolve");
|
||||
FAIL_ZERO_RETURN(errctx, state, AKERR_NULLPOINTER, "NULL resolve state");
|
||||
|
||||
if ( other == state->proxy ) {
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/*
|
||||
* Nested rather than written with early returns. A *_RETURN inside an
|
||||
* ATTEMPT block leaves past CLEANUP, which is a real defect even where the
|
||||
* CLEANUP happens to be empty today -- and scripts/check_error_protocol.py
|
||||
* refuses it, correctly.
|
||||
*/
|
||||
ATTEMPT {
|
||||
CATCH(errctx, akgl_collision_shape_interacts(&state->proxy->shape, &other->shape, &interacts));
|
||||
if ( interacts == true ) {
|
||||
state->world->tests += 1;
|
||||
CATCH(errctx, akgl_collision_test(state->proxy, other, state->world->flags, &contact, &hit));
|
||||
if ( hit == true ) {
|
||||
contact.self = state->actor;
|
||||
contact.other = other->owner;
|
||||
contact.dt = state->dt;
|
||||
contact.sensor = (((state->proxy->shape.flags & AKGL_COLLISION_FLAG_SENSOR) != 0) ||
|
||||
((other->shape.flags & AKGL_COLLISION_FLAG_SENSOR) != 0));
|
||||
contact.statichit = ((other->shape.flags & AKGL_COLLISION_FLAG_STATIC) != 0);
|
||||
|
||||
CATCH(errctx, state->actor->collidefunc(state->actor, &contact));
|
||||
|
||||
// The actor moved, so its own proxy is stale for the rest of the pass.
|
||||
CATCH(errctx, akgl_collision_proxy_sync(state->proxy, &state->actor->shape,
|
||||
state->actor->x, state->actor->y,
|
||||
state->actor->z));
|
||||
}
|
||||
}
|
||||
} CLEANUP {
|
||||
} PROCESS(errctx) {
|
||||
} FINISH(errctx, true);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Resolve one actor against the tiles its shape overlaps.
|
||||
*
|
||||
* Tiles are not proxies, so they are scanned rather than queried: a stack-local
|
||||
* proxy is synthesized for each solid cell and handed to the ordinary
|
||||
* narrowphase, which keeps one code path for "what does a contact look like"
|
||||
* rather than two.
|
||||
*/
|
||||
static akerr_ErrorContext *collision_resolve_tiles(akgl_CollisionWorld *self, akgl_Actor *actor,
|
||||
akgl_CollisionProxy *proxy, float32_t dt)
|
||||
{
|
||||
akgl_CollisionProxy tile;
|
||||
akgl_CollisionShape tileshape;
|
||||
akgl_Contact contact;
|
||||
SDL_FRect body;
|
||||
bool hit = false;
|
||||
int32_t tx = 0;
|
||||
int32_t ty = 0;
|
||||
int32_t x0 = 0;
|
||||
int32_t y0 = 0;
|
||||
int32_t x1 = 0;
|
||||
int32_t y1 = 0;
|
||||
int layer = 0;
|
||||
|
||||
PREPARE_ERROR(errctx);
|
||||
|
||||
if ( (self->tilesource == NULL) || (self->tilelayers == 0) ) {
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
if ( (proxy->shape.collidemask & self->tilelayermask) == 0 ) {
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
x0 = (int32_t)floorf(proxy->bounds.x / (float32_t)self->tilesource->tilewidth);
|
||||
y0 = (int32_t)floorf(proxy->bounds.y / (float32_t)self->tilesource->tileheight);
|
||||
x1 = (int32_t)floorf(((proxy->bounds.x + proxy->bounds.w) - AKGL_COLLISION_TILE_EPSILON) /
|
||||
(float32_t)self->tilesource->tilewidth);
|
||||
y1 = (int32_t)floorf(((proxy->bounds.y + proxy->bounds.h) - AKGL_COLLISION_TILE_EPSILON) /
|
||||
(float32_t)self->tilesource->tileheight);
|
||||
|
||||
body.w = (float32_t)self->tilesource->tilewidth;
|
||||
body.h = (float32_t)self->tilesource->tileheight;
|
||||
body.x = 0.0f;
|
||||
body.y = 0.0f;
|
||||
|
||||
for ( ty = y0; ty <= y1; ty++ ) {
|
||||
for ( tx = x0; tx <= x1; tx++ ) {
|
||||
if ( !collision_tile_solid(self, tx, ty) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ATTEMPT {
|
||||
CATCH(errctx, akgl_collision_shape_box(&tileshape, &body, 0.0f));
|
||||
tileshape.layermask = self->tilelayermask;
|
||||
tileshape.flags |= AKGL_COLLISION_FLAG_STATIC;
|
||||
CATCH(errctx, akgl_collision_proxy_initialize(
|
||||
&tile, NULL, &tileshape,
|
||||
(float32_t)(tx * self->tilesource->tilewidth),
|
||||
(float32_t)(ty * self->tilesource->tileheight), proxy->z));
|
||||
|
||||
CATCH(errctx, akgl_collision_test(proxy, &tile, self->flags, &contact, &hit));
|
||||
self->tests += 1;
|
||||
if ( hit == true ) {
|
||||
contact.self = actor;
|
||||
contact.other = NULL;
|
||||
contact.dt = dt;
|
||||
contact.statichit = true;
|
||||
contact.tilex = tx;
|
||||
contact.tiley = ty;
|
||||
contact.tilegid = 0;
|
||||
contact.tilelayer = -1;
|
||||
// Which layer and which tile, so a game can tell a spike from
|
||||
// a floor without looking it up again.
|
||||
for ( layer = 0; layer < self->tilesource->numlayers; layer++ ) {
|
||||
if ( (self->tilelayers & (1u << layer)) == 0 ) {
|
||||
continue;
|
||||
}
|
||||
if ( self->tilesource->layers[layer].data[(ty * self->tilesource->width) + tx] != 0 ) {
|
||||
contact.tilelayer = layer;
|
||||
contact.tilegid = self->tilesource->layers[layer].data[(ty * self->tilesource->width) + tx];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
CATCH(errctx, actor->collidefunc(actor, &contact));
|
||||
CATCH(errctx, akgl_collision_proxy_sync(proxy, &actor->shape,
|
||||
actor->x, actor->y, actor->z));
|
||||
}
|
||||
} CLEANUP {
|
||||
} PROCESS(errctx) {
|
||||
} FINISH(errctx, true);
|
||||
}
|
||||
}
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext *akgl_collision_resolve(akgl_CollisionWorld *self, akgl_Actor *actor, float32_t dt)
|
||||
{
|
||||
collision_resolve_state state;
|
||||
|
||||
PREPARE_ERROR(errctx);
|
||||
FAIL_ZERO_RETURN(errctx, self, AKERR_NULLPOINTER, "NULL collision world reference");
|
||||
FAIL_ZERO_RETURN(errctx, actor, AKERR_NULLPOINTER, "NULL actor reference");
|
||||
|
||||
if ( (actor->proxy == NULL) || (actor->shape.kind == AKGL_COLLISION_SHAPE_NONE) ) {
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
if ( actor->collidefunc == NULL ) {
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
// The actor has just moved, so the index and the copy of its shape are both
|
||||
// a step out of date.
|
||||
PASS(errctx, akgl_collision_proxy_sync(actor->proxy, &actor->shape, actor->x, actor->y, actor->z));
|
||||
PASS(errctx, self->partitioner.move(&self->partitioner, actor->proxy));
|
||||
|
||||
PASS(errctx, collision_resolve_tiles(self, actor, actor->proxy, dt));
|
||||
|
||||
memset(&state, 0x00, sizeof(state));
|
||||
state.world = self;
|
||||
state.actor = actor;
|
||||
state.proxy = actor->proxy;
|
||||
state.dt = dt;
|
||||
PASS(errctx, self->partitioner.query(&self->partitioner, &actor->proxy->bounds,
|
||||
actor->shape.collidemask, &collision_resolve_visit, &state));
|
||||
|
||||
// Whatever the responses did, the index has to end the pass agreeing with
|
||||
// where the actor actually is.
|
||||
PASS(errctx, akgl_collision_proxy_sync(actor->proxy, &actor->shape, actor->x, actor->y, actor->z));
|
||||
PASS(errctx, self->partitioner.move(&self->partitioner, actor->proxy));
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext *akgl_collision_substeps(akgl_CollisionWorld *self, akgl_Actor *actor, float32_t dt, int *dest)
|
||||
{
|
||||
float32_t span = 0.0f;
|
||||
float32_t limit = 0.0f;
|
||||
float32_t thin = 0.0f;
|
||||
|
||||
PREPARE_ERROR(errctx);
|
||||
FAIL_ZERO_RETURN(errctx, actor, AKERR_NULLPOINTER, "NULL actor reference");
|
||||
FAIL_ZERO_RETURN(errctx, dest, AKERR_NULLPOINTER, "NULL destination reference");
|
||||
|
||||
/*
|
||||
* One, unless there is something to collide with. A step of exactly `dt`
|
||||
* takes the same arithmetic path it always has -- `dt / 1.0f` is `dt`
|
||||
* exactly in IEEE-754 -- which is what lets every existing assertion about
|
||||
* a non-colliding actor hold unchanged.
|
||||
*/
|
||||
*dest = 1;
|
||||
if ( (self == NULL) || (actor->shape.kind == AKGL_COLLISION_SHAPE_NONE) ) {
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
span = fabsf(actor->vx);
|
||||
if ( fabsf(actor->vy) > span ) { span = fabsf(actor->vy); }
|
||||
if ( fabsf(actor->vz) > span ) { span = fabsf(actor->vz); }
|
||||
span = span * dt;
|
||||
|
||||
/*
|
||||
* Do not travel further in one sub-step than the thinnest thing that could
|
||||
* be passed through. That is a cell *or the actor's own extent*, whichever
|
||||
* is smaller: a small fast actor is the one that tunnels, and bounding only
|
||||
* by cell size misses it entirely.
|
||||
*/
|
||||
thin = self->cellwidth;
|
||||
if ( self->cellheight < thin ) { thin = self->cellheight; }
|
||||
if ( (2.0f * actor->shape.hx) < thin ) { thin = 2.0f * actor->shape.hx; }
|
||||
if ( (2.0f * actor->shape.hy) < thin ) { thin = 2.0f * actor->shape.hy; }
|
||||
limit = AKGL_COLLISION_SUBSTEP_FRACTION * thin;
|
||||
|
||||
if ( limit <= 0.0f ) {
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
*dest = (int)(span / limit) + 1;
|
||||
if ( *dest > AKGL_COLLISION_MAX_SUBSTEPS ) {
|
||||
*dest = AKGL_COLLISION_MAX_SUBSTEPS;
|
||||
}
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext *akgl_collision_sync_actors(akgl_CollisionWorld *self)
|
||||
{
|
||||
akgl_Actor *actor = NULL;
|
||||
int i = 0;
|
||||
|
||||
PREPARE_ERROR(errctx);
|
||||
FAIL_ZERO_RETURN(errctx, self, AKERR_NULLPOINTER, "NULL collision world reference");
|
||||
|
||||
/*
|
||||
* One pass over the pool: give a proxy to anything that has gained a shape,
|
||||
* take one back from anything that has lost one, and refresh the rest. This
|
||||
* is the only place a proxy is created or destroyed during a step, so
|
||||
* nothing below has to think about lifetimes.
|
||||
*/
|
||||
for ( i = 0; i < AKGL_MAX_HEAP_ACTOR; i++ ) {
|
||||
actor = &akgl_heap_actors[i];
|
||||
if ( actor->refcount == 0 ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( actor->shape.kind == AKGL_COLLISION_SHAPE_NONE ) {
|
||||
if ( actor->proxy != NULL ) {
|
||||
PASS(errctx, self->partitioner.remove(&self->partitioner, actor->proxy));
|
||||
PASS(errctx, akgl_heap_release_collision_proxy(actor->proxy));
|
||||
actor->proxy = NULL;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( actor->proxy == NULL ) {
|
||||
PASS(errctx, akgl_heap_next_collision_proxy(&actor->proxy));
|
||||
PASS(errctx, akgl_collision_proxy_initialize(actor->proxy, actor, &actor->shape,
|
||||
actor->x, actor->y, actor->z));
|
||||
PASS(errctx, self->partitioner.insert(&self->partitioner, actor->proxy));
|
||||
continue;
|
||||
}
|
||||
|
||||
PASS(errctx, akgl_collision_proxy_sync(actor->proxy, &actor->shape,
|
||||
actor->x, actor->y, actor->z));
|
||||
PASS(errctx, self->partitioner.move(&self->partitioner, actor->proxy));
|
||||
}
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include <math.h>
|
||||
#include <akstdlib.h>
|
||||
#include <akgl/collision.h>
|
||||
#include <akgl/physics.h>
|
||||
#include <akgl/actor.h>
|
||||
#include <akgl/game.h>
|
||||
@@ -20,12 +21,18 @@ akerr_ErrorContext *akgl_physics_null_gravity(akgl_PhysicsBackend *self, akgl_Ac
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext *akgl_physics_null_collide(akgl_PhysicsBackend *self, akgl_Actor *a1, akgl_Actor *a2)
|
||||
akerr_ErrorContext *akgl_physics_null_collide(akgl_PhysicsBackend *self, akgl_Actor *actor, float32_t dt)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
FAIL_ZERO_RETURN(errctx, self, AKERR_NULLPOINTER, "self");
|
||||
FAIL_ZERO_RETURN(errctx, a1, AKERR_NULLPOINTER, "a1");
|
||||
FAIL_ZERO_RETURN(errctx, a2, AKERR_NULLPOINTER, "a2");
|
||||
FAIL_ZERO_RETURN(errctx, actor, AKERR_NULLPOINTER, "actor");
|
||||
(void)dt;
|
||||
|
||||
/*
|
||||
* Nothing collides, and that is an answer rather than a gap. A caller who
|
||||
* wants a world with no collision in it installs this backend and gets it,
|
||||
* without the simulation having to branch on whether collision exists.
|
||||
*/
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
@@ -79,11 +86,19 @@ akerr_ErrorContext *akgl_physics_arcade_gravity(akgl_PhysicsBackend *self, akgl_
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext *akgl_physics_arcade_collide(akgl_PhysicsBackend *self, akgl_Actor *a1, akgl_Actor *a2)
|
||||
akerr_ErrorContext *akgl_physics_arcade_collide(akgl_PhysicsBackend *self, akgl_Actor *actor, float32_t dt)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
FAIL_ZERO_RETURN(errctx, self, AKERR_NULLPOINTER, "self");
|
||||
FAIL_RETURN(errctx, AKERR_API, "Not implemented");
|
||||
FAIL_ZERO_RETURN(errctx, actor, AKERR_NULLPOINTER, "actor");
|
||||
|
||||
// No world means no collision, and costs one comparison. This is what makes
|
||||
// the feature opt-in: a backend nobody attached a world to runs exactly as
|
||||
// it did before any of this existed.
|
||||
if ( self->collision == NULL ) {
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
PASS(errctx, akgl_collision_resolve(self->collision, actor, dt));
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
@@ -152,10 +167,17 @@ akerr_ErrorContext *akgl_physics_simulate(akgl_PhysicsBackend *self, akgl_Iterat
|
||||
float32_t dt = 0;
|
||||
float32_t overshoot = 0.0f;
|
||||
float32_t thrustscale = 0.0f;
|
||||
float32_t subdt = 0.0f;
|
||||
int substeps = 1;
|
||||
int s = 0;
|
||||
akgl_Actor *actor = NULL;
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, self, AKERR_NULLPOINTER, "self");
|
||||
FAIL_ZERO_RETURN(errctx, self->move, AKERR_NULLPOINTER, "self->move");
|
||||
// `gravity` was never checked, and is dereferenced unconditionally below. A
|
||||
// backend built by hand with only `move` filled in crashed here rather than
|
||||
// reporting; `collide` is allowed to be NULL and means "nothing collides".
|
||||
FAIL_ZERO_RETURN(errctx, self->gravity, AKERR_NULLPOINTER, "self->gravity");
|
||||
|
||||
// Reading the elapsed time requires self, so it cannot be hoisted above
|
||||
// the null check.
|
||||
@@ -183,6 +205,12 @@ akerr_ErrorContext *akgl_physics_simulate(akgl_PhysicsBackend *self, akgl_Iterat
|
||||
opflags = &defflags;
|
||||
}
|
||||
|
||||
// One pass to give every shaped actor a proxy and take one back from
|
||||
// anything that lost its shape, so the loop below never has to.
|
||||
if ( self->collision != NULL ) {
|
||||
PASS(errctx, akgl_collision_sync_actors(self->collision));
|
||||
}
|
||||
|
||||
|
||||
for ( int i = 0; i < AKGL_MAX_HEAP_ACTOR; i++ ) {
|
||||
actor = &akgl_heap_actors[i];
|
||||
@@ -268,13 +296,49 @@ akerr_ErrorContext *akgl_physics_simulate(akgl_PhysicsBackend *self, akgl_Iterat
|
||||
actor->vy = actor->ey + actor->ty;
|
||||
actor->vz = actor->ez + actor->tz;
|
||||
|
||||
PASS(errctx, self->move(self, actor, dt));
|
||||
/*
|
||||
* Only `move` is subdivided. Gravity, drag, the thrust integration
|
||||
* and the speed-ellipse cap above all ran once against the whole dt,
|
||||
* and sum(subdt) is dt -- so an actor with nothing to collide with
|
||||
* takes exactly one sub-step of exactly dt and follows the arithmetic
|
||||
* path it always did. That is what lets the integrator stay untouched
|
||||
* and every recorded physics number stay valid.
|
||||
*/
|
||||
CATCH(errctx, akgl_collision_substeps(self->collision, actor, dt, &substeps));
|
||||
subdt = dt / (float32_t)substeps;
|
||||
for ( s = 0; s < substeps; s++ ) {
|
||||
PASS(errctx, self->move(self, actor, subdt));
|
||||
if ( (self->collide != NULL) && (self->collision != NULL) ) {
|
||||
PASS(errctx, self->collide(self, actor, subdt));
|
||||
}
|
||||
}
|
||||
} CLEANUP {
|
||||
} PROCESS(errctx) {
|
||||
} HANDLE(errctx, AKGL_ERR_LOGICINTERRUPT) {
|
||||
// noop
|
||||
} FINISH(errctx, true);
|
||||
}
|
||||
/*
|
||||
* Re-snap children after everything has moved, but only when collision is
|
||||
* on. The in-loop snap uses whatever position the parent had when the
|
||||
* child's own slot came up in pool order, which is already sometimes a
|
||||
* frame stale -- harmless while a parent only ever moved by `v * dt`, and
|
||||
* not harmless once a parent can also be pushed out of a wall part way
|
||||
* through its own sub-steps. Gating it keeps a collision-free world
|
||||
* byte-for-byte what it was.
|
||||
*/
|
||||
if ( self->collision != NULL ) {
|
||||
for ( int i = 0; i < AKGL_MAX_HEAP_ACTOR; i++ ) {
|
||||
actor = &akgl_heap_actors[i];
|
||||
if ( (actor->refcount == 0) || (actor->parent == NULL) ) {
|
||||
continue;
|
||||
}
|
||||
actor->x = actor->parent->x + actor->vx;
|
||||
actor->y = actor->parent->y + actor->vy;
|
||||
actor->z = actor->parent->z + actor->vz;
|
||||
}
|
||||
}
|
||||
|
||||
self->gravity_time = curtime;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user