I fixed the problem. It was a simple typo. If you look at this line:

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if getPixelMatrixValue(p[a], aPos)=1 and getPixelMatrixValue(p[a], bPos)=1 then exitfunction 1

I was comparing a bit to itself. One of those function calls should using p[

**b**] but I used 'a' for both instead. Oops!

This code should work. I brought my laptop to work and it's running fine but I can't get it online so I'm just updating the text file here and posting it. So provided I didn't make any more dumb typos, this should be pixel perfect collision with low overhead. With the bug fixed, I'll have to see if it still crashes with large images or not.

I'm getting around 500 fps on my i7 laptop. It dips to 350 on an intersection, but the code has a lot of needless loops in it for drawing for demo purposes.

Ok, I took out all the needless stuff and it's twice as fast. About 1200 fps without collision, 600-900 once the sprites' bounding boxes overlap. Still seems like too large of a hit to me, maybe there's still room to optimize it.

// Project: pixelCollision
// Created: 2017-04-04
Â
// show all errors
SetErrorMode(2)
Â
// set window properties
SetWindowTitle( "pixelCollision" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
Â
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 0, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
Â
Â
Â
Type Rectangle
Â Â Â Â x as integer
Â Â Â Â y as integer
Â Â Â Â width as integer
Â Â Â Â height as integer
EndType
Â Â
Â Â
Type PixelCollision
Â Â Â Â spriteId as integer
Â Â Â Â matrix as integer[]
Â Â Â Â m as integer
EndType
Â Â
Â Â
pixelSprites as PixelCollision[]
Â Â
Â Â
Â Â
dude1 = createSprite(loadImage("GameArtPartners_1_FeatureImage2.png"))
p1 = addPixelCollision(pixelSprites, dude1)
setSpritePosition(dude1, 300, 200)
Â
dude2 = createSprite(loadImage("pirate-royalty-free-game-art2.png"))
p2 = addPixelCollision(pixelSprites, dude2)
Â
Â
Â
c = 0xFFFFFF
Â
Â
Â
do
Â Â Â Â Â
Â Â Â Â Â
Â
Â Â Â Â Â
Â Â Â Â Â
Â Â Â Â setSpritePosition(dude2, getRawMouseX(), getRawMouseY())
Â Â Â Â Â
Â Â Â Â Â
Â
Â Â Â Â bmax = pixelSprites[p1].matrix.length*8
Â Â Â Â for i = 0 to bmax
Â Â Â Â Â Â Â Â y = i / 64
Â Â Â Â Â Â Â Â x = i - y*64
Â Â Â Â Â Â Â Â Â
Â Â Â Â Â Â Â Â `if getPixelMatrixValue(pixelSprites[p1], i) > 0 then drawBox(x+100, y+100, x+101, y+101,c,c,c,c,1)
Â Â Â Â next i
Â Â Â Â Â
Â Â Â Â Â
Â Â Â Â bmax = pixelSprites[p2].matrix.length*8
Â Â Â Â for i = 0 to bmax
Â Â Â Â Â Â Â Â y = i / 64
Â Â Â Â Â Â Â Â x = i - y*64
Â Â Â Â Â Â Â Â Â
Â Â Â Â Â Â Â Â `if getPixelMatrixValue(pixelSprites[p2], i) > 0 then drawBox(x+200, y+100, x+201, y+101,c,c,c,c,1)
Â Â Â Â next i
Â Â Â Â Â
Â Â Â Â Â
Â Â Â Â Â
Â Â Â Â result = pixelCollide(pixelSprites, p1, p2)
Â Â Â Â print(result)
Â
Â Â Â Â Print( ScreenFPS() )
Â Â Â Â Sync()
loop
Â
Â
Â
Â
Â
Â Â
Â Â
Â Â
Â Â
function pixelCollide(p as PixelCollision[], a as integer, b as integer)
Â Â Â Â x1 = getSpriteX(p[a].spriteId)
Â Â Â Â y1 = getSpriteY(p[a].spriteId)
Â Â Â Â x2 = x1 + getSpriteWidth(p[a].spriteId)
Â Â Â Â y2 = y1 + getSpriteHeight(p[a].spriteId)
Â Â
Â Â Â Â x3 = getSpriteX(p[b].spriteId)
Â Â Â Â y3 = getSpriteY(p[b].spriteId)
Â Â Â Â x4 = x3 + getSpriteWidth(p[b].spriteId)
Â Â Â Â y4 = y3 + getSpriteHeight(p[b].spriteId)
Â Â Â Â Â
// bounding boxes around sprites
Â Â Â Â c = 0xFFFFFF
Â Â Â Â `drawBox(x1,y1,x2,y2,c,c,c,c,0)
Â Â Â Â `drawBox(x3,y3,x4,y4,c,c,c,c,0)
Â Â
Â Â Â Â // check bounding box overlap
Â Â Â Â if x3 < x2 and x4 > x1 and y3 < y2 and y4 > y1
Â Â Â Â Â Â Â Â // Get intersecting rectangle
Â Â Â Â Â Â Â Â r as Rectangle
Â Â Â Â Â Â Â Â r = getIntersection(x1,y1,x2,y2,x3,y3,x4,y4)
Â Â Â Â Â Â Â Â Â Â
Â Â Â Â Â Â Â Â x1 = r.x-x1
Â Â Â Â Â Â Â Â y1 = r.y-y1
Â Â Â Â Â Â Â Â Â Â
Â Â Â Â Â Â Â Â x3 = r.x - x3
Â Â Â Â Â Â Â Â y3 = r.y - y3
Â Â Â Â Â Â Â Â Â Â
Â Â Â Â Â Â Â Â aWidth = getSpriteWidth(p[a].spriteId)
Â Â Â Â Â Â Â Â bWidth = getSpriteWidth(p[b].spriteId)
Â Â Â Â Â Â Â Â Â
Â Â Â Â Â Â Â Â // show intersection box
Â Â Â Â Â Â Â Â c = 0xFFFF00
Â Â Â Â Â Â Â Â //drawBox(r.x, r.y, r.x+r.width, r.y+r.height,c,c,c,c,0)
Â Â Â Â Â Â Â Â
c1 = 0xFFFFFF
c2 = 0xFF0000
c3 = 0x0000FF
// Show what parts of the image are being checked
Â Â Â Â Â Â Â Â for y = 0 to r.height-1
Â Â Â Â Â Â Â Â Â Â Â Â for x = 0 to r.width-1
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â aPos = (y1+y)*aWidth + x1+x
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â bPos = (y3+y)*bWidth + x3+x
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if getPixelMatrixValue(p[a], aPos)=1 then drawBox(x+100, y+100, x+101, y+101,c1,c1,c1,c1,1)
if getPixelMatrixValue(p[b], bPos)=1 then drawBox(x+100, y+100, x+101, y+101,c2,c2,c2,c2,1)
cx = r.x+x
cy = r.y+y
if getPixelMatrixValue(p[a], aPos)=1 and getPixelMatrixValue(p[b], bPos)=1 then drawBox(cx,cy,cx+1,cy+1,c3,c3,c3,c3,1)
Â Â Â Â Â Â Â Â Â Â Â Â next x
Â Â Â Â Â Â Â Â next y
Â Â Â Â Â Â Â Â
// Check for overlapping collisionÂ
Â Â Â Â Â Â Â Â for y = 0 to r.height-1
Â Â Â Â Â Â Â Â Â Â Â Â for x = 0 to r.width-1
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â aPos = (y1+y)*aWidth + x1+x
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â bPos = (y3+y)*bWidth + x3+x
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if getPixelMatrixValue(p[a], aPos)=1 and getPixelMatrixValue(p[b], bPos)=1 then exitfunction 1
Â Â Â Â Â Â Â Â Â Â Â Â next x
Â Â Â Â Â Â Â Â next y
Â Â Â Â Â Â Â Â Â Â
Â Â Â Â endif
Â Â
endfunction 0
Â
Â
Â Â
Â Â
function getIntersection(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2)
Â Â Â Â leftÂ Â = max(ax1, bx1)
Â Â Â Â topÂ Â Â = max(ay1, by1)
Â Â Â Â rightÂ = min(ax2, bx2)
Â Â Â Â bottom = min(ay2, by2)
Â Â
Â
Â Â Â Â r as Rectangle
Â Â
Â Â Â Â r.x = left
Â Â Â Â r.y = top
Â Â Â Â r.width = 0
Â Â Â Â r.height = 0
Â Â Â Â if right > left then r.width = right - left
Â Â Â Â if bottom > top then r.height = bottom - top
Â Â Â Â Â Â
endfunction r
Â Â
Â Â
Â Â
function addPixelCollision(p ref as PixelCollision[], sprite as integer)
Â Â Â Â Â Â
Â Â Â Â img = getSpriteImageID(sprite)
Â Â
Â Â Â Â collisionMatrixSize = ceil((getImageWidth(img) * getImageHeight(img)) / 8)
Â Â
Â Â Â Â x as PixelCollision
Â Â Â Â x.spriteId = sprite
Â Â Â Â x.matrix.length = collisionMatrixSize
Â Â
Â Â
Â Â Â Â m = createMemblockFromImage(img)
Â Â Â Â Â
Â Â Â Â Â
Â Â Â Â Â
Â
Â Â Â Â Â
Â Â Â Â bit = 0
Â Â Â Â for i = 12 to getMemblockSize(m)-4 step 4
Â Â Â Â Â Â Â Â if getMemblockByte(m, i+3) > 0
Â Â Â Â Â Â Â Â Â Â Â Â setPixelMatrixValue(x, bit, 1)
Â Â Â Â Â Â Â Â endif
Â Â Â Â Â Â Â Â inc bit
Â Â Â Â next i
Â Â
Â Â Â Â x.m = m
Â Â Â Â Â
Â Â Â Â `deleteMemblock(m)
Â Â
Â Â
Â Â Â Â p.insert(x)
endfunction p.length
Â Â
Â Â
Â Â
Â Â
Â Â
function setPixelMatrixValue(p ref as PixelCollision, bit, bool)
Â Â Â Â i = floor(bit / 8)
Â Â Â Â b = bit - i*8
Â Â
Â Â Â Â p.matrix[i] = p.matrix[i] || (1 << b)
endfunction
Â Â
Â Â
Â Â
Â Â
Â Â
function getPixelMatrixValue(p as PixelCollision, bit)
Â Â Â Â i = floor(bit / 8)
Â Â Â Â b = bit - i*8
Â Â Â Â value = (p.matrix[i] >> b) && 1
endfunction value
Â
Â
Â
function max(a, b)
Â Â Â Â if a > b then exitfunction a
endfunction b
Â
function min(a, b)
Â Â Â Â if a < b then exitfunction a
endfunction b

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds