Prev: 27705 Up: Map Next: 27781
27731: Check Sprite Collision
Checks if two sprites overlap by comparing their X and Y positions. Each sprite is assumed to be 16 pixels wide and 12 pixels tall.
Input
IX Pointer to first sprite data
IY Pointer to second sprite data
Output
F Carry set = no collision, carry clear = collision
First check horizontal overlap. The two sprites overlap in X if neither sprite's left edge is beyond the other's right edge.
CheckSpriteCollision 27731 LD A,(IX+0) Load A with the first sprite's X position (from *IX+0).
27734 CP (IY+0) Jump to CheckSpriteCollision_TestRight if the first sprite is to the right of the second sprite (checking against *IY+0/ the second sprites X position).
27737 JR NC,CheckSpriteCollision_TestRight
First sprite is to the left so check if its right edge reaches the second sprite.
27739 ADD A,16 Add 16 to the first sprite's X position (add assumed sprite width).
27741 CP (IY+0) Jump to CheckSpriteCollision_TestVertical if the two sprites overlap horizontally.
27744 JR NC,CheckSpriteCollision_TestVertical
27746 RET Return (there's no horizontal overlap; carry is set).
First sprite is to the right so check if the second sprite's right edge reaches the first sprite.
CheckSpriteCollision_TestRight 27747 LD A,(IY+0) Load A with the second sprite's X position (from *IY+0) + 16 (adding the sprite width).
27750 ADD A,16
27752 CP (IX+0) Jump to CheckSpriteCollision_TestVertical if the two sprites overlap horizontally.
27755 JR NC,CheckSpriteCollision_TestVertical
27757 RET Return (there's no horizontal overlap; carry is set).
The two sprites overlap horizontally; now check if there's any vertical overlap.
CheckSpriteCollision_TestVertical 27758 LD A,(IX+1) Load A with the first sprite's Y position (*IX+1).
27761 CP (IY+1) Jump to CheckSpriteCollision_TestBelow if the first sprite is below the second sprite (checking against *IY+1/ the second sprites Y position).
27764 JR NC,CheckSpriteCollision_TestBelow
First sprite is above so check if its bottom edge reaches the second sprite.
27766 ADD A,12 Add 12 to the first sprite's Y position (add assumed sprite height).
27768 CP (IY+1) Return (with; carry clear meaning a collision, and carry set meaning no collision).
27771 RET
First sprite is below so check if the second sprite's bottom edge reaches the first sprite.
CheckSpriteCollision_TestBelow 27772 LD A,(IY+1) Load A with the second sprite's Y position (from *IY+1) + 12 (adding the sprite height).
27775 ADD A,12
27777 CP (IX+1) Return (with; carry clear meaning a collision, and carry set meaning no collision).
27780 RET
Prev: 27705 Up: Map Next: 27781