;############## Calcuzap by Patrick Davidson - collision detection ;############## Sets the first object for collision check by structure ; Takes HL pointing to the following four bytes: ; X-coordinate, Y-coordinate, pointer to image (height, then width) set_collision_check_object: ld a,(hl) ; X inc hl ld c,(hl) ; Y inc hl #ifdef TI84CE ld hl,(hl) ld de,(hl) #else ld e,(hl) inc hl ld d,(hl) ; DE -> image ex de,hl ; HL -> image ld e,(hl) ; height inc hl ld d,(hl) ; width #endif ;############## Sets the first object for collision check ; A = X, C = Y, E = height, D = width set_collision_check_coords: ld (ccx+1),a ld a,c ld (ccy+1),a ld a,d ld (ccw+1),a ld a,e ld (cch+1),a ret ;############## Test for collisions between two objects ; ; Carry flag set if they collided, clear if they didn't ; Takes HL pointing to the following four bytes: ; X-coordinate, Y-coordinate, pointer to image (height, then width) collision_check: ;############## Ensure that X1 < X2 + W2 ld a,(hl) ; A = X1 inc hl ld b,(hl) ; B = Y1 inc hl #ifdef TI84CE ld hl,(hl) ; HL -> image height #else ld e,(hl) inc hl ld d,(hl) ; DE -> image ex de,hl ; HL -> image height #endif ld c,(hl) ; C = image height ccx: sub 3 ; A = X1 - X2 jr c,cc1 ; (X1 < X2) --> (X1 < X2 + W2) ccw: cp 3 ret nc ; Exit if (X1 - X2) >= W2 jr cc2 ; (X2 <= X1) --> (X2 < X1 + W1) ;############## Ensure that X2 < X1 + W1 cc1: neg ; A = X2 - X1 (which is > 0) inc hl cp (hl) ret nc ; Exit if (X2 - X1) >= W1 ;############## Ensure that Y1 < Y2 + H2 cc2: ld a,b ; A = Y1 ccy: sub 3 ; A = Y1 - Y2 jr c,cc3 ; (Y1 < Y2) --> (Y1 < Y2 + H2) cch: cp 3 ; Carry set if (Y1 - Y2) < H2 ret ; End of testing (last cond. certain ; since Y2 <= Y1, due to jr c,cc3) ;############## Ensure that Y2 < Y1 + H1 cc3: neg ; A = Y2 - Y1 (which is > 0) cp c ret ; Carry set if (Y2 - Y1) < H1