Say what was hit, which way is out, and how far

The narrowphase has been producing a contact since it went in, and the interpreter
was throwing it away. `RCOLLISION(n, f)` reports it: what was hit (a sprite or a
`SOLID` rectangle), which one, the contact normal, the penetration depth, the
contact point, and which axis to reverse.

**The normal points out of the other thing and toward this one**, so a program
moves along it by the depth and is exactly clear. That sign is the one assertion
in the new test that could not be caught any other way -- both parties of a
sprite-against-sprite hit get their own record, each pointing the way *that*
sprite has to move, and sharing one would tell both to go the same direction,
which is how two things end up stuck inside each other.

**Field 7 is the one that deletes the most BASIC.** It is the minimum translation
axis, computed from the normal in C, and it is there because doing it in BASIC
means comparing two floats -- which is exactly where this dialect's left-operand
rule catches people. `BALLBRICKS`/`TESTCELL` in the artwork breakout spend six
lines computing an overlap rectangle and comparing its width to its height to get
this number.

The record is **sticky and deepest-wins**: replaced whenever that sprite is in a
contact and otherwise left alone, so `BUMP` stays the event and this stays the
detail of it. Making it clear itself when nothing touches would break the pairing,
because `BUMP` accumulates across steps and a once-a-frame poll would find the
detail already gone. Reading `BUMP` clears both, so they cannot disagree.

Deliberately narrower than `akgl_Contact`: no actor pointers, because BASIC has no
actor; no tile fields, because there is no tilemap; no z, because every test is
planar; and **no `dt` and no `sensor`**, which libakgl documents as filled in by
the resolver. This interpreter never resolves anything, so those two come back
zero and mean nothing, and an always-zero field in a reference table is a lie.

Documented with the two caveats that matter: fields 2, 3 and 4 are floats and want
a `%` variable, and the contact *point* is exact only for two boxes -- libakgl's
solver returns a point on the portal it converged to, while the normal and depth
are exact for every pair.

Chapter 8's collision section stops claiming only type 1 exists, which has been
false since the previous commit.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EwxGB6TdoVvZ11KQQME9cL
Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
This commit is contained in:
2026-08-02 10:53:26 -04:00
parent d3b69ff867
commit a9059423e9
12 changed files with 401 additions and 4 deletions

View File

@@ -582,6 +582,27 @@ akerr_ErrorContext *akbasic_collision_service(akbasic_Runtime *obj)
if ( obj->sprites->solids != NULL ) {
PASS(errctx, obj->sprites->solids(obj->sprites, &solidmask));
}
/*
* Pull the detail for whatever collided, while the scan that produced it is
* still the current one. RCOLLISION answers from here rather than from the
* device, because by the time a handler runs the sprites have moved on --
* akbasic_sprite_service() runs immediately before this.
*/
if ( (mask | solidmask) != 0 && obj->sprites->contact != NULL ) {
uint16_t both = (uint16_t)(mask | solidmask);
int i = 0;
for ( i = 0; i < AKBASIC_MAX_SPRITES; i++ ) {
bool found = false;
if ( (both & (uint16_t)(1u << i)) == 0 ) {
continue;
}
/* PASS rather than CATCH: this is a loop. */
PASS(errctx, obj->sprites->contact(obj->sprites, i + 1,
&obj->sprite_state.contacts[i], &found));
}
}
if ( solidmask != 0 ) {
obj->sprite_state.bumpedsolid |= solidmask;
PASS(errctx, akbasic_runtime_raise_interrupt(obj, AKBASIC_INTERRUPT_BACKGROUND));
@@ -895,6 +916,65 @@ akerr_ErrorContext *akbasic_fn_rsphit(akbasic_Runtime *obj, akbasic_ASTLeaf *exp
SUCCEED_RETURN(errctx);
}
/**
* @brief One field of the contact sprite n is in.
*
* Sticky: the record is replaced whenever that sprite is in a contact and
* otherwise left alone, so `BUMP` remains the event -- "has anything hit me since
* I last looked" -- and this is the detail of it. Making it clear itself when
* nothing is touching would break the pairing, because `BUMP` accumulates across
* steps and a once-a-frame poll would find the detail already gone.
*
* Reading `BUMP` clears it, so the two stay in step.
*/
akerr_ErrorContext *akbasic_fn_rcollision(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
{
PREPARE_ERROR(errctx);
akbasic_Contact *contact = NULL;
int64_t n = 0;
int64_t field = 0;
int index = 0;
(void)lval; (void)rval;
FAIL_ZERO_RETURN(errctx, (obj != NULL && dest != NULL), AKERR_NULLPOINTER,
"NULL argument in RCOLLISION");
PASS(errctx, nth_number(obj, expr, "RCOLLISION", 0, &n));
PASS(errctx, nth_number(obj, expr, "RCOLLISION", 1, &field));
PASS(errctx, sprite_index((int)n, "RCOLLISION", &index));
contact = &obj->sprite_state.contacts[index];
switch ( field ) {
case 0:
PASS(errctx, integer_result(obj, (int64_t)contact->what, dest));
break;
case 1:
PASS(errctx, integer_result(obj, (int64_t)contact->other, dest));
break;
case 2:
PASS(errctx, float_result(obj, contact->nx, dest));
break;
case 3:
PASS(errctx, float_result(obj, contact->ny, dest));
break;
case 4:
PASS(errctx, float_result(obj, contact->depth, dest));
break;
case 5:
PASS(errctx, float_result(obj, contact->px, dest));
break;
case 6:
PASS(errctx, float_result(obj, contact->py, dest));
break;
case 7:
PASS(errctx, integer_result(obj, (int64_t)contact->axis, dest));
break;
default:
FAIL_RETURN(errctx, AKBASIC_ERR_BOUNDS,
"RCOLLISION: field %" PRId64 " is outside 0..7", field);
}
SUCCEED_RETURN(errctx);
}
akerr_ErrorContext *akbasic_fn_rsprite(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
{
akbasic_Sprite *sprite = NULL;