Hi guys, this should be a simple code but is driving me crazy 2 days:
A simple routine to check if a point is inside a box, a building block of almost all games:
; --------------------------------------------------------- ; Checks if the point (x, y) is inside the box (x1, y1, x2, y2) ; ; Destroys: ; ; Inputs: ; HL: point to be checked (H: x, L: y) ; BC: upper left corner of box to be checked (B: x1, C: y1) ; DE: down right corner of box to be checked (D: x2, E: y2) ; Output: ; A = 0 : not collided ; A = 1 : collided CheckCollision: ;To compare stuff, simply do a CP, and if the zero flag is set, ;A and the argument were equal, else if the carry is set the argument was greater, and finally, if ;neither is set, then A must be greater (CP does nothing to the registers, only the F (flag) register ;is changed). ; if (x >= x1) ld a, h cp b jp c, .false ; c: a < parameter ; if (x <= x2) ld a, h cp d jp nc, .false ; nc: a > parameter ; if (y >= y1) ld a, l cp c jp c, .false ; c: a < parameter ; if (y <= y2) ld a, l cp e jp nc, .false ; nc: a > parameter ;true: ld a, 1 ret .false: ld a, 0 ret
I'm testing it isolated in another program, it gets false all the time:
ld h, 30 ; point (30, 20) ld l, 20 ld b, 15 ; box upper left (15, 10) ld c, 10 ld d, 40 ; box bottom right (40, 30) ld e, 30 call CheckCollision ; cp a jp nz, True jp False ret False: ld hl,FalseMsg call Print ret True: ld hl,TrueMsg call Print ret TrueMsg: db "True",0 FalseMsg: db "False",0
Not having debug tools, trace, step, unit tests, it's very hard to be productive, you got stuck on something all the time.
Programmers of the 80's were real heroes.
Any idea?
Login or register to post comments