Skip to content

Add rect_overlap collision detection helper #421

@jonathanpeppers

Description

@jonathanpeppers

Collision detection between sprites is a common need that is currently hand-written with byte-distance checks. The climber sample has ~15 lines of collision code:

// Today: manual collision in climber
byte dx = (byte)(actor_x[0] - actor_x[ci]);
if (dx >= 248) dx = (byte)(0 - dx);
byte dyl = (byte)(actor_yy_lo[0] - actor_yy_lo[ci]);
if (dyl >= 248) dyl = (byte)(0 - dyl);
if (dx < 8 && dyl < 8)
{
    // collision!
}

Similar patterns appear in pong (ball vs paddle) and other game samples.

Proposed API:

// Returns true if two axis-aligned rectangles overlap
bool hit = rect_overlap(x1, y1, w1, h1, x2, y2, w2, h2);

// Simpler variant for same-size sprites:
bool hit = sprite_overlap(x1, y1, x2, y2, threshold);

This maps to a small 6502 subroutine that does unsigned subtraction + comparison on both axes. A pure neslib addition with a corresponding built-in subroutine.

Once implemented, the climber sample collision check and gap-fall detection should be updated to use this helper, along with pong's paddle/ball collision.

Related to #30

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions