The Need for Resolution
Collision Detection (Part 26) only tells you that two objects overlap. Collision Resolution is the logic that moves the objects apart and adjusts their speed (velocity) so that they don’t overlap in the next frame. Without resolution, sprites will get stuck inside walls.
Step 1: Backtracking (The Safest Move)
The most reliable way to resolve an overlap is to simply move the collided object back to its previous, non-colliding position.
The Backtrack Method:
- Before updating the position in the game loop, save the old X and Y coordinates of the sprite in a dedicated memory scratchpad.
- If a collision is detected after the update, restore the X and Y coordinates from the saved scratchpad.
Advantage: This is fast and guarantees the sprite never penetrates the wall, but it results in a ‘sticky’ or rigid stop.
Step 2: Zeroing Velocity (Stopping)
If a sprite hits a solid, fixed object (like a floor or wall), you must stop its movement in that direction by setting its velocity (DX or DY) to zero.
Example: Hitting the Floor If a collision is detected on the bottom side of the sprite:
SPRITE_DY_ADDR EQU +A ; Y-Velocity offset
RESOLVE_FLOOR:
; 1. Restore Y-position from scratchpad (Step 1)
CALL RESTORE_Y_POS
; 2. Zero out the Y-Velocity
LD A, 0
LD (IX + SPRITE_DY_ADDR), A ; DY ← 0
; 3. Set ON_GROUND flag (for jumping logic)
SET 3, (IX + SPRITE_STATUS)
RET
Step 3: Bouncing (Reversing Velocity)
For a bouncy object (like a ball hitting a wall), the resolution involves reversing the velocity in the axis of the collision.
The Principle: Negate the velocity, and optionally apply friction (a dampening factor).
Example: Hitting a Wall (Bouncing X-Velocity)
SPRITE_DX_ADDR EQU +8 ; X-Velocity offset
DAMPING_FACTOR EQU 0D0H ; 8-bit factor (e.g., to reduce speed by 20%)
RESOLVE_WALL:
; 1. Restore X-position from scratchpad
CALL RESTORE_X_POS
; 2. Invert the X-Velocity (A ← -DX)
LD A, (IX + SPRITE_DX_ADDR)
CPL ; A ← one's complement
INC A ; A ← two's complement (negates A)
; 3. (Optional) Apply Damping/Friction
; ... (Code to multiply A by DAMPING_FACTOR)
LD (IX + SPRITE_DX_ADDR), A ; Store the new, inverted velocity
RET