Answer sprite collision through libakgl's narrowphase

`spr_collisions()` computed axis-aligned overlaps itself, because at libakgl
0.7.0 there was nothing to delegate to: `akgl_collide_rectangles()` has a
documented corner-containment defect and the physics backend's `collide` slot
raised "Not implemented". 0.8.0 brought a real narrowphase, and this moves onto
it.

The mask is bit-identical and every test from the previous commit passes
**unmodified**, which was the gate this stage had to clear -- including the two
that were written to be hard to satisfy. Edge-to-edge is still not a collision,
so a tile-aligned program is unaffected. The cross-shaped overlap is still
reported, which is the one that could have regressed: it is the case
`akgl_collide_rectangles()` gets wrong and the reason the hand-written loop
existed, and `akgl_collision_test()`'s box path gets it right.

What it buys is the contact -- a normal, a penetration depth and a point --
which four comparisons cannot produce. Nothing consumes it yet; that is the next
commit. It is here now because the mask and the contact come out of the same
test, and computing them in two places would be two things to keep in step.

**The first attempt was ten times slower and the benchmark caught it.** Syncing
all eight proxies and running the narrowphase on all twenty-eight pairs measured
984 ns a scan against 96 ns for the loop it replaced -- 21% of a frame at 256
scans a frame -- to produce a mask that was bit-identical and a contact that was
thrown away. Two changes fixed it, and both are what a broad phase *is* rather
than workarounds for a slow library:

- **Reject on the bounding boxes first.** The four comparisons that were always
  here now decide which pairs are worth an exact answer. The narrowphase still
  decides the bit -- the box test only says "maybe", which will matter the moment
  a shape is not the whole frame.
- **Do not sync a proxy that has not moved.** The scan runs at the top of every
  interpreter step and a sprite moves at most once in that time, so almost every
  sync would rewrite a proxy with what it already holds. Compared against the
  last synced rectangle rather than flagged by the verbs, because a host game can
  move a BASIC sprite through the actor registry and a flag would miss that.

Measured after: 54.9 ns at eight sprites spread out, which is *faster* than the
96.3 ns it replaced -- boxes are now built eight times a scan instead of
fifty-six. The number that matters is the new benchmark row for the arrangement
`examples/breakout/sprites/breakout.bas` actually has, which is 211.8 ns, or
4.5% of a frame. That game reaches it because two of its eight sprites are the
screen -- a captured HUD strip and a captured play field -- so the field's box
covers everything and those pairs can never be rejected. Roughly double the old
cost, for contacts. Recorded in MAINTENANCE.md with the two synthetic extremes
either side of it as a bracket.

The eight proxies are claimed once at init and held, so exhaustion of the pool
shared with an embedding host is an initialization failure that names the pool
rather than a collision scan refusing halfway through somebody's game. The shape
is built before the proxy is spawned from it and the acquire sits adjacent to the
initialize, which are two traps libakgl hit itself and documents.

`tests/akgl_backends.c` now tears the sprite backend down between cases. It never
did, and got away with it while init claimed nothing; eight proxies apiece across
twenty cases is a hundred and sixty against a pool of a hundred and twenty-eight.
A host releases what it took, and so does the harness.

Both games run forty seconds headless with no error line. 111 with akgl, 110
without.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EwxGB6TdoVvZ11KQQME9cL
This commit is contained in:
2026-08-02 09:48:11 -04:00
parent 1da37cf374
commit f06c53110d
5 changed files with 335 additions and 56 deletions

View File

@@ -479,10 +479,90 @@ static akerr_ErrorContext *spr_shared_colors(akbasic_SpriteBackend *self, akbasi
SUCCEED_RETURN(errctx);
}
/**
* @brief Is slot @p i a thing that can be collided with at all?
*
* The three guards the pair loop used to repeat inline. A slot with no actor was
* never defined; a slot with no sprite has no frame and therefore no size; and an
* invisible sprite is out of the world entirely, which is the cheapest way a
* program has of switching one off without forgetting where it was.
*/
static bool slot_collidable(akbasic_AkglSprites *state, int i)
{
return (state->actors[i] != NULL
&& state->sprites[i] != NULL
&& state->actors[i]->visible);
}
/**
* @brief Point slot @p i's proxy at where the sprite is and what size it is now.
*
* The shape is rebuilt every scan rather than cached against a dirty flag. It is
* derived from the sprite's frame and the two expansion bits, both of which a
* program may change at any statement, and building a box is a handful of
* arithmetic -- cheaper than being wrong about what invalidates it.
*
* **The mask override is the load-bearing line.** akgl_collision_shape_box()
* leaves a shape on the ACTOR layer responding to STATIC only, so that giving a
* town full of NPCs hitboxes does not make them shove each other around. That is
* the right default for a tile game and exactly wrong here: BASIC's collision is
* sprite-against-sprite first, and taking the library's default would make
* BUMP(1) report nothing at all, silently.
*/
static akerr_ErrorContext AKERR_NOIGNORE *sync_proxy(akbasic_AkglSprites *state, int i,
SDL_FRect *box)
{
PREPARE_ERROR(errctx);
SDL_FRect body;
/*
* **Nothing to do if nothing moved.** The scan runs at the top of every
* interpreter step -- up to 256 times a rendered frame -- and a sprite moves
* at most once in that time, so the overwhelming majority of syncs would
* rewrite a proxy with what it already holds. Four float comparisons to find
* that out are far cheaper than the shape build and the sync they skip.
*
* Compared against the last synced rectangle rather than tracked with a dirty
* flag a verb sets. A flag would be smaller and would be wrong: this file's
* own header says a host game sees BASIC's sprites in libakgl's actor
* registry "and can do anything to them it can do to an actor", which
* includes moving one behind the interpreter's back. Comparing the answer
* cannot miss that; flagging the question can.
*/
if ( state->syncedbox[i].x == box->x && state->syncedbox[i].y == box->y
&& state->syncedbox[i].w == box->w && state->syncedbox[i].h == box->h ) {
SUCCEED_RETURN(errctx);
}
body.x = 0.0f;
body.y = 0.0f;
body.w = box->w;
body.h = box->h;
/*
* A zero-sized frame would be refused by the shape builder, and a sprite can
* legitimately have one before its sheet is loaded. Treat it as not
* collidable rather than raising: the program has not done anything wrong.
*/
if ( body.w <= 0.0f || body.h <= 0.0f ) {
SUCCEED_RETURN(errctx);
}
PASS(errctx, akgl_collision_shape_box(&state->shapes[i], &body, 0.0f));
state->shapes[i].collidemask = AKGL_COLLISION_LAYER_ACTOR | AKGL_COLLISION_LAYER_STATIC;
PASS(errctx, akgl_collision_proxy_sync(state->proxies[i], &state->shapes[i],
box->x, box->y, 0.0f));
state->syncedbox[i] = *box;
SUCCEED_RETURN(errctx);
}
static akerr_ErrorContext *spr_collisions(akbasic_SpriteBackend *self, uint16_t *mask)
{
PREPARE_ERROR(errctx);
akbasic_AkglSprites *state = NULL;
akgl_Contact contact;
SDL_FRect box[AKBASIC_MAX_SPRITES];
bool live[AKBASIC_MAX_SPRITES];
bool synced[AKBASIC_MAX_SPRITES];
bool hit = false;
int i = 0;
int j = 0;
@@ -490,41 +570,81 @@ static akerr_ErrorContext *spr_collisions(akbasic_SpriteBackend *self, uint16_t
FAIL_ZERO_RETURN(errctx, (mask != NULL), AKERR_NULLPOINTER, "NULL mask in collisions");
*mask = 0;
for ( i = 0; i < AKBASIC_MAX_SPRITES; i++ ) {
live[i] = slot_collidable(state, i);
synced[i] = false;
if ( !live[i] ) {
continue;
}
box[i].x = state->actors[i]->x;
box[i].y = state->actors[i]->y;
box[i].w = (float32_t)state->sprites[i]->width * (state->xexpand[i] ? 2.0f : 1.0f);
box[i].h = (float32_t)state->sprites[i]->height * (state->yexpand[i] ? 2.0f : 1.0f);
}
/*
* Axis-aligned bounding boxes, compared here rather than through
* akgl_collide_rectangles(): that function has a documented
* corner-containment defect -- it misses a plus-shaped overlap where neither
* rectangle contains a corner of the other -- and nothing in libakgl calls
* it. Four comparisons are both correct and smaller.
* **Two phases, and the split is the whole performance story.**
*
* Box against box, not pixel against pixel. A C128 collides on set pixels,
* so two sprites whose boxes touch but whose art does not are reported as
* colliding here and would not be there. Recorded in TODO.md section 5.
* The obvious implementation -- sync all eight proxies, then run
* akgl_collision_test() on all twenty-eight pairs -- was measured at 984 ns a
* scan against 96 ns for the four-comparison loop it replaced. At 256 scans a
* frame that is 21% of a frame, and it bought nothing: the mask came back
* bit-identical and the contact was thrown away.
*
* So: reject on the bounding boxes first, in the four comparisons that were
* always here, and pay for the narrowphase only on a pair that survives.
* Nearly every scan of a real game rejects all twenty-eight, and a pair that
* does overlap is worth an exact answer. This is not a workaround for a slow
* library -- it is what a broad phase *is*, and akgl_collision_test() does the
* same thing internally with the proxies' own bounds before it commits to a
* solver.
*
* **The narrowphase still decides.** The box test only says "maybe"; the bit
* is set by what the library answers. That matters for a shape that is not a
* box -- a circle inscribed in a frame overlaps a smaller region than the
* frame does -- so the prefilter can over-report and must never be the final
* word. Today every shape is the whole frame and the two always agree, which
* is exactly why this is the moment to get the ordering right rather than
* the moment it starts to matter.
*
* AKGL_COLLISION_TEST_PLANAR because a Commodore sprite has no z. Without it
* the extrusion the shape builder chose takes part in the test and the
* minimum axis can come back as a depth along z, which no BASIC program can
* act on.
*
* Still box against box, not pixel against pixel: a C128's VIC-II collides on
* set pixels, so two sprites whose boxes overlap but whose art does not are
* reported as colliding here and would not be there. TODO.md section 5.
*/
for ( i = 0; i < AKBASIC_MAX_SPRITES; i++ ) {
if ( !live[i] ) {
continue;
}
for ( j = i + 1; j < AKBASIC_MAX_SPRITES; j++ ) {
float32_t ax = 0.0f, ay = 0.0f, aw = 0.0f, ah = 0.0f;
float32_t bx = 0.0f, by = 0.0f, bw = 0.0f, bh = 0.0f;
if ( state->actors[i] == NULL || state->actors[j] == NULL ) {
if ( !live[j] ) {
continue;
}
if ( !state->actors[i]->visible || !state->actors[j]->visible ) {
if ( !(box[i].x < box[j].x + box[j].w && box[j].x < box[i].x + box[i].w
&& box[i].y < box[j].y + box[j].h && box[j].y < box[i].y + box[i].h) ) {
continue;
}
if ( state->sprites[i] == NULL || state->sprites[j] == NULL ) {
continue;
/*
* PASS rather than CATCH throughout: this is a loop, and CATCH
* expands to a break, which would leave the remaining pairs untested
* and the mask half built.
*/
if ( !synced[i] ) {
PASS(errctx, sync_proxy(state, i, &box[i]));
synced[i] = true;
}
ax = state->actors[i]->x;
ay = state->actors[i]->y;
aw = (float32_t)state->sprites[i]->width * (state->xexpand[i] ? 2.0f : 1.0f);
ah = (float32_t)state->sprites[i]->height * (state->yexpand[i] ? 2.0f : 1.0f);
bx = state->actors[j]->x;
by = state->actors[j]->y;
bw = (float32_t)state->sprites[j]->width * (state->xexpand[j] ? 2.0f : 1.0f);
bh = (float32_t)state->sprites[j]->height * (state->yexpand[j] ? 2.0f : 1.0f);
if ( ax < bx + bw && bx < ax + aw && ay < by + bh && by < ay + ah ) {
if ( !synced[j] ) {
PASS(errctx, sync_proxy(state, j, &box[j]));
synced[j] = true;
}
hit = false;
PASS(errctx, akgl_collision_test(state->proxies[i], state->proxies[j],
AKGL_COLLISION_TEST_PLANAR, &contact, &hit));
if ( hit ) {
*mask |= (uint16_t)(1u << i);
*mask |= (uint16_t)(1u << j);
}
@@ -536,6 +656,7 @@ static akerr_ErrorContext *spr_collisions(akbasic_SpriteBackend *self, uint16_t
akerr_ErrorContext *akbasic_sprite_init_akgl(akbasic_SpriteBackend *obj, akbasic_AkglSprites *state, akgl_RenderBackend *renderer, akbasic_AkglGraphics *graphics)
{
PREPARE_ERROR(errctx);
int i = 0;
FAIL_ZERO_RETURN(errctx, (obj != NULL && state != NULL && renderer != NULL),
AKERR_NULLPOINTER, "NULL argument in sprite_init_akgl");
@@ -546,6 +667,37 @@ akerr_ErrorContext *akbasic_sprite_init_akgl(akbasic_SpriteBackend *obj, akbasic
state->renderer = renderer;
state->graphics = graphics;
/*
* The eight collision proxies, claimed here and held for the life of the
* backend.
*
* Up front rather than on demand because the pool is shared: a host game
* embedding this interpreter draws its own shaped actors from the same
* AKGL_MAX_HEAP_COLLISION_PROXY. Claiming late would mean a collision scan
* discovering mid-game that a host had taken the last slot, and reporting it
* against whichever BASIC line was unlucky. Claiming here makes it an
* initialization failure that names the pool, before a program has run.
*
* A default box shape so a proxy is never initialized from an uninitialized
* shape -- libakgl fixed exactly that defect in 3a6569e, where filing a proxy
* under half-extents that had not been written yet was invisible to its
* tests and visible only to a memory checker. Real geometry arrives on the
* first sync_proxy(); this is only ever a placeholder.
*/
for ( i = 0; i < AKBASIC_MAX_SPRITES; i++ ) {
SDL_FRect placeholder = { 0.0f, 0.0f, 1.0f, 1.0f };
PASS(errctx, akgl_collision_shape_box(&state->shapes[i], &placeholder, 0.0f));
/*
* Acquire and initialize adjacent, with nothing between them: the slot
* is free until initialize takes the reference, and libakgl's heap says
* so where next_collision_proxy is declared.
*/
PASS(errctx, akgl_heap_next_collision_proxy(&state->proxies[i]));
PASS(errctx, akgl_collision_proxy_initialize(state->proxies[i], NULL,
&state->shapes[i], 0.0f, 0.0f, 0.0f));
}
memset(obj, 0, sizeof(*obj));
obj->self = state;
obj->define = spr_define;
@@ -591,5 +743,16 @@ void akbasic_sprite_akgl_shutdown(akbasic_SpriteBackend *obj)
state = (akbasic_AkglSprites *)obj->self;
for ( i = 0; i < AKBASIC_MAX_SPRITES; i++ ) {
IGNORE(release_slot(state, i));
/*
* The proxies go back too. They are not registered with any partitioner,
* so there is nothing to unlink first -- which is the one ordering trap
* here, and the reason worth writing down: releasing a proxy that *is*
* registered leaves stale cell entries holding a pool index, and the
* damage only shows when the slot is handed out again.
*/
if ( state->proxies[i] != NULL ) {
IGNORE(akgl_heap_release_collision_proxy(state->proxies[i]));
state->proxies[i] = NULL;
}
}
}