Qbasicnews.com

Full Version: Box by Box collision detection
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Alright, I can't believe I'm asking this question, but I'm now being reminded that I've never actually attempted box by box collision detection, and I'm stuck. Basically, I want the game to see if Box #1, is intersecting Box #2, and if so, do something. The objects I'm using have an X,Y,Width,and Height variable.

The routine I've come up with seems to be both, over complicated, and most importantly, non-working.

Thanks.
Believe it or not, my brother was working on the same type of thing; he was trying to code a box-by-box collision algo in ASM.

First he tried to write code that essentially checks if any of the corners of one box are within the other box, and found that that doesn't always work. This wouldn't generate a collision, for example:

Code:
+--------+
      |        |
+-----|--------|---+
|     |        |   |
+-----|--------|---+
      |        |
      +--------+

So what you need to check is if any of the sides of one box intersect any of the sides of the other box.

Hope that helps,

--j_k
Actually, that's the exact problem I seem to be happening. The way mine works is that one of the sides of the box absolutely needs to be inside the other box, otherwise it won't work, and for some reason, I can't come up with the math I need to make it happen.

Anonymous

Code:
Function CheckBounds  ( x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer, width1, height1, width2, height2 )


  Dim As Byte xmatch, ymatch

  If  (( x1 + width1 ) >= x2 ) And x1 <= x2 + width2 Then xmatch = -1
  If  (( y1 + height1 ) >= y2 ) And y1 <= y2 + height2 Then ymatch = -1

  If xmatch And ymatch Then
    Return 0
  
  Else
    Return -1

  End If


End Function



Screen 13

f = 0
g = 20

h = 20
i = 0

j = 60
k = 20

l = 20
m = 60

Line (f, g)-(f + j, g + k), 15,b
Line (h, i)-(h + l, i + m), 15,b


? checkbounds (f, g, h, i, j, k, l, m)

sleep

edit: some args were out of order
Here's a helpful article:

http://www.gamedev.net/reference/article...cle754.asp

The author defines the rectangles as
(ax1, ay1, ax2, ay2) and (bx1, by1, bx2, by2).

Quote:There are only one case where a collision MIGHT occur, and that is when the two rectangles (AX1,AY1)-(AX2,AY2) and (BX1,BY1)-(BX2,BY2) overlap.

We can discard many conditions where a collision is NOT possible.

Since AX1<AX2 is true for all cases (same for AY1<AY2, BX1<BX2, and BY1<BY2), we can reject the following conditions:

BY2<AY1

AY2<BY1

BX2<AX1

AX2<BX1

If any of the above relations are TRUE, then no collision can have occurred.

If all of the above relations are FALSE, then a collision MIGHT have occurred.

I believe that covers it well. I've never written this code before, though, so I don't know if it works.

Something to note is that the author goes on to check every to see if the sprites' pixels overlap. Since your not doing that, you can stop after you find that a collision "might have" occured.

--j_k