OK that didn't take long.
There are 2 ways you can do the intersect of 2 image rectangles:
1) Just run som numbers and say if intersect or not ie True or False
2nd way, you not only say if they intersect but you also say exactly where they intersect.
Here I strongly suspect this is where you are getting thrown of the scent @madscijr.
So lets look at the parameters to this SUB
(b1x, b1y, b1w, b1h, b2x, b2y, b2w, b2h, bix As Long, biy As Long, biw As Long, bih As Long)
Long story short they are 3 boxes
box 1 image 1 is b1x, b1y, b1w, b1h
box 2 image 2 is b2x, b2y, b2w, b2h
and the last box describes the box of intersect (which could be also a line or point but would have at least and x, y value.
box intersect bix As Long, biy As Long, biw As Long, bih As Long
note bi stands for box intersect
So my box intersect collision says where the box of intersect is, where points in common to both images might be, so we've only a very small box, smaller than either image likely to search for over lapping pixels!
That is key point to understanding the pixel collision sub routine. *** It already knows from the 2nd box intersect SUB where exactly to look for pixel collisons inside both image rectangles:
@madscijr is that enough of a clue to the PixelCollision routine?
BTW my code assumes the object point color in the intersecting images is not 0 ie all background of object is Black.
BTW the spiders legs of the images are moving all the time so an updated image is drawn I assume in separate container on each frame.
So really cool to catch an intersect off a dynamically changing image, way way way more tricky than merely getting intersect of polygons!
There are 2 ways you can do the intersect of 2 image rectangles:
1) Just run som numbers and say if intersect or not ie True or False
Code: (Select All)
Function BoxCollision% (b1x, b1y, b1w, b1h, b2x, b2y, b2w, b2h)
' x, y represent the box left most x and top most y
' w, h represent the box width and height which is the usual way sprites / tiles / images are described
' such that boxbottom = by + bh
' and boxright = bx + bw
If (b1y + b1h < b2y) Or (b1y > b2y + b2h) Or (b1x > b2x + b2w) Or (b1x + b1w < b2x) Then
BoxCollision% = 0
Else
BoxCollision% = -1
End If
End Function2nd way, you not only say if they intersect but you also say exactly where they intersect.
Code: (Select All)
' this needs max, min functions as well as BoxCollision%
Sub Intersect2Boxes (b1x, b1y, b1w, b1h, b2x, b2y, b2w, b2h, bix As Long, biy As Long, biw As Long, bih As Long)
If b2x >= b1x And b2x <= b1x + b1w And b2y >= b1y And b2y <= b1y + b1h Then 'top left corner in 2nd box
bix = b2x: biy = b2y
If b2x + b2w <= b1x + b1w Then biw = b2w Else biw = b1x + b1w - b2x
If b2y + b2h <= b1y + b1h Then bih = b2h Else bih = b1y + b1h - b2y
ElseIf b2x >= b1x And b2x <= b1x + b1w And b2y + b2h >= b1y And b2y + b2h <= b1y + b1h Then 'bottom left corner of 2nd box in first
bix = b2x
If b2x + b2w <= b1x + b1w Then biw = b2w Else biw = b1x + b1w - b2x
If b2y <= b1y Then biy = b1y: bih = b2y + b2h - b1y Else biy = b2y: bih = b2h
ElseIf b2x + b2w >= b1x And b2x + b2w <= b1x + b1w And b2y >= b1y And b2y <= b1y + b1h Then 'right top corner 2nd box in first
If b2x >= b1x Then bix = b2x: biw = b2w Else bix = b1x: biw = b2x + b2w - b1x
biy = b2y
If b2y + b2h <= b1y + b1h Then bih = b2h Else bih = b1y + b1h - b2y
ElseIf b2x + b2w >= b1x And b2x + b2w <= b1x + b1w And b2y + b2h >= b1y And b2y + b2h <= b1y + b1h Then 'left bottom corners in first box
If b2x >= b1x Then bix = b2x: biw = b2w Else bix = b1x: biw = b2x + b2w - b1x
If b2y >= b1y Then biy = b2y: bih = b2h Else biy = b1y: bih = b2y + b2h - b1y
ElseIf BoxCollision%(b1x, b1y, b1w, b1h, b2x, b2y, b2w, b2h) Then
bix = max(b1x, b2x): biy = max(b1y, b2y)
biw = min(b1x + b1w, b2x + b2w) - bix: bih = min(b1y + b1h, b2y + b2h) - biy
Else 'no intersect
bix = -1: biy = -1: biw = 0: bih = 0
End If
End SubHere I strongly suspect this is where you are getting thrown of the scent @madscijr.
So lets look at the parameters to this SUB
(b1x, b1y, b1w, b1h, b2x, b2y, b2w, b2h, bix As Long, biy As Long, biw As Long, bih As Long)
Long story short they are 3 boxes
box 1 image 1 is b1x, b1y, b1w, b1h
box 2 image 2 is b2x, b2y, b2w, b2h
and the last box describes the box of intersect (which could be also a line or point but would have at least and x, y value.
box intersect bix As Long, biy As Long, biw As Long, bih As Long
note bi stands for box intersect
So my box intersect collision says where the box of intersect is, where points in common to both images might be, so we've only a very small box, smaller than either image likely to search for over lapping pixels!
That is key point to understanding the pixel collision sub routine. *** It already knows from the 2nd box intersect SUB where exactly to look for pixel collisons inside both image rectangles:
Code: (Select All)
' this sub needs Intersect2Boxes which uses max, min, and BoxCollision Functions
Function PixelCollision& (img1 As boxType, img2 As boxType, intx As Long, inty As Long)
' boxType here needs at least an x, y, w, h and img
Dim As Long x, y, ix, iy, iw, ih
Dim As _Unsigned Long p1, p2
intx = -1: inty = -1 ' no collision set
Intersect2Boxes img1.x, img1.y, img1.w, img1.h, img2.x, img2.y, img2.w, img2.h, ix, iy, iw, ih ' ***
If ix <> -1 Then ' the boxes intersect
y = iy: x = ix
Do
_Source img1.img
p1 = Point(x - img1.x, y - img1.y) ' point minus img x, y location = location in image I hope
_Source img2.img
p2 = Point(x - img2.x, y - img2.y)
If (p1 <> 0) And (p2 <> 0) Then
PixelCollision& = -1: intx = x: inty = y: Exit Function
End If
If (x + 1) > (ix + iw - 1) Then ' get rid of 2 slow For Loops
x = ix: y = y + 1
If y >= (iy + ih - 1) Then
_Source 0: Exit Function
Else
y = y + 1
End If
Else
x = x + 1
End If
Loop
End If
End Function@madscijr is that enough of a clue to the PixelCollision routine?
BTW my code assumes the object point color in the intersecting images is not 0 ie all background of object is Black.
BTW the spiders legs of the images are moving all the time so an updated image is drawn I assume in separate container on each frame.
So really cool to catch an intersect off a dynamically changing image, way way way more tricky than merely getting intersect of polygons!
724 855 599 923 575 468 400 206 147 564 878 823 652 556 bxor cross forever


