I have a brick breaker clone working fairly well. The collision between the ball and the blocks does not work all of the time. I needed to figure out if the ball was hitting a left, right, top, or bottom side so I could react accordingly. So I came up with this function:
rem
rem Gets a sprite's zone and returns 1 for top through 4 clockwise
rem
Function GetSpriteZone(sp1 as integer, sp2 as integer)
value as integer
If GetSpriteY(sp1) <= GetSpriteY(sp2) rem Top Zone
value = 1
ElseIf GetSpriteY(sp1) >= GetSpriteY(sp2) + GetSpriteHeight(sp2) rem Bottom Zone
value = 3
ElseIf GetSpriteY(sp1) > GetSpriteY(sp2) And GetSpriteY(sp1) <= GetSpriteY(sp2) + GetSpriteHeight(sp2) And GetSpriteX(sp1) <= GetSpriteX(sp2) rem Left Zone
value = 4
ElseIf GetSpriteY(sp1) > GetSpriteY(sp2) And GetSpriteY(sp1) <= GetSpriteY(sp2) + GetSpriteHeight(sp2) And GetSpriteX(sp1) >= GetSpriteX(sp2) + GetSpriteWidth(sp2) rem Right Zone
value = 2
EndIf
EndFunction value
Now in my game loop I test like so:
For r = 200 to 301
If GetSpriteExists(r) = 1
If GetSpriteCollision(5, r) = 1
If GetSpriteZone(5, r) = 3
yval = yval * -1
ElseIf GetSpriteZone(5, r) = 1
yval = abs(yval)
ElseIf GetSpriteZone(5, r) = 2
xval = xval * -1
ElseIf GetSpriteZone(5, r) = 4
xval = abs(xval)
EndIf
PlaySound(2, 20)
DeleteSprite(r)
EndIf
EndIf
Next
It works as expected sometimes, but other times, the ball goes right through the brick.