Add rect_overlap and sprite_overlap collision detection helpers#439
Merged
jonathanpeppers merged 4 commits intomainfrom Apr 14, 2026
Merged
Add rect_overlap and sprite_overlap collision detection helpers#439jonathanpeppers merged 4 commits intomainfrom
jonathanpeppers merged 4 commits intomainfrom
Conversation
Add two new NESLib collision detection APIs: - rect_overlap(x1, y1, w1, h1, x2, y2, w2, h2) for AABB rectangle overlap - sprite_overlap(x1, y1, x2, y2, threshold) for distance-based same-size sprite collision Both return bool (0/1 in A register) and are implemented as 6502 subroutines that read arguments directly from the cc65 parameter stack for efficiency. Updated samples: - climber: replaced manual byte-distance collision with sprite_overlap - pong: replaced nested paddle collision checks with rect_overlap Closes #421 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
080d711 to
236a528
Compare
The transpiler's HandleLdelemU1 saves prior values to a single TEMP register, not the cc65 parameter stack. When multiple array accesses are chained as function arguments, values get overwritten. Fix by storing array element values in local variables before the call, which uses WriteLdloc's pusha emission path instead. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The transpiler's default Call path never handled multi-argument NESLib method calls correctly - all existing multi-arg NESLib methods used dedicated intrinsic handlers. Adding rect_overlap (8 args) and sprite_overlap (5 args) exposed this gap. Transpiler fixes: - Add IsDefaultCallPathTarget/ScanForUpcomingMultiArgCall helpers to detect when a value load precedes a multi-arg call - Extend WriteLdloc, WriteLdc(byte), HandleLdsfld to emit JSR pusha for arguments destined for default-path calls - Extend HandleLdelemU1 to emit JSR pusha when array element values are arguments for upcoming multi-arg calls - Extend ScanForUpcomingMultiArgCall to handle ldelem_u1 and stloc opcodes in the forward scan Test updates: - Increase pusha balance test window from 8 to 20 instructions to handle ldelem_u1 pushes that occur far before the call site - Rebuild test DLLs and accept updated verified.bin snapshots Verified via disassembly (4 JSR pusha + A = 5 args for sprite_overlap) and Mesen2 runtime screenshots showing correct rendering. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…dition - Add cheap ball_x range guard before rect_overlap calls in pong to avoid unnecessary JSR+stack traffic when ball is far from paddles - Update rect_overlap XML docs to document that x+w and y+h must not wrap past 255 (8-bit arithmetic precondition) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add two new NESLib collision detection APIs as described in #421:
rect_overlap(x1, y1, w1, h1, x2, y2, w2, h2)— Full AABB rectangle overlap test. Returns true if two axis-aligned rectangles overlap. Uses unsigned byte arithmetic, suitable for NES screen coordinates.sprite_overlap(x1, y1, x2, y2, threshold)— Distance-based collision for same-size sprites. Returns true if the unsigned absolute difference on both axes is less than the threshold.Both are implemented as 6502 subroutines that read arguments directly from the cc65 parameter stack via indirect indexed addressing, avoiding expensive per-arg
popacalls. They depend onaddyspfor stack cleanup.Changes
ForEachOptionalBuiltInwithaddyspdependencysprite_overlaprect_overlapBefore/After (climber)
Before/After (pong)
Closes #421