First, check to see the boxes are touching/overlapping. Next, depends on how you expect your collision to work. My approach makes the assumption that box A is never inside box B. (but I could adjust it to account for that)
Assuming the boxes only hit from the outside, then the right side of box A can never hit the right side of box B, only its left. So I subtract the left side of box A from the right side of box B. If the value is between 0 and some threshold (to allow for a small amount of overlapping) then the left side of box A has been hit (or the right side of box B). I take a similar approach to the other 3 sides.
Each side hit contains a different number, each representing a binary digit so that when added up you get a single number which can represent multiple hits.
Left=1, right=2, top=4, bottom=8
Obviously you can never have both left and right sides hit simultaneously unless the boxes are the same size. But, the right and top could hit at the same time, such as in a corner hit. For that, the function returns 6. Bottom left would return 9, and so on.
type BoxObject
x as integer
y as integer
width as integer
height as integer
endtype
A as BoxObject
B as BoxObject
a.width = 50
a.height = 50
b.x = 300
b.y = 300
b.width = 150
b.height = 75
do
cls
rem center box at mouse
a.x = mousex() - 25
a.y = mousey() - 25
drawBox(A)
drawBox(B)
print boxHit(A, B, 2)
loop
function boxHit(A as BoxObject, B as BoxObject, threshold)
sides = 0
rem check if boxes are overlapping
if A.x <= B.x+B.width and A.x+A.width >= B.x
if A.y <= B.y+B.height and A.y+A.height > B.y
rem find which sides are touching
left = (B.x+B.width-A.x)
right = (A.x+A.width-B.x)
top = (B.y+B.height-A.y)
bottom = (A.y+A.height-B.y)
if left >= 0 and left <= threshold then sides = sides + 1
if right >= 0 and right <= threshold then sides = sides + 2
if top >= 0 and top <= threshold then sides = sides + 4
if bottom >= 0 and bottom <= threshold then sides = sides + 8
endif
endif
endfunction sides
function drawBox(b as BoxObject)
line b.x, b.y, b.x+b.width, b.y
line b.x, b.y+b.height, b.x+b.width, b.y+b.height
line b.x, b.y, b.x, b.y+b.height
line b.x+b.width, b.y, b.x+b.width, b.y+b.height
endfunction