Quote: "I made this tool to speed up setting multiple complex shapes for sprites. SetSpriteShape v1"
Thats pretty cool...it certainly speeds up the generation of sprite shapes. Thanks for that.
Quote: "I'm confident I can support animations in my pixel collision system. Rotation, however, I'm still researching. I feel there is a simple method of being able to take my pixel data and add a rotational transform matrix to it but I haven't worked out how yet."
I can support pixel collision detection with rotation of the two sprites but not tried with animation yet....as that then makes the sprite image area a small subset of the image it is showing. Ive never actually tried adding animation support with the function I wrote. It might actually work with animation if i add a few lines to convert from sprite pixel x/y to uv x/y based on the current frame? I can post the function that works with rotation if it helps
function GetSpritePixelCollision(spr1 as integer, spr2 as integer)
time# = timer()
// check if these sprites are collisdng using hsapes for a quick exit
if GetSpriteCollision(spr1,spr2) = 0
exitfunction 0
endif
// Get dimensions and images for both sprites
spr1w# = GetSpriteWidth(spr1)
spr1h# = GetSpriteHeight(spr1)
spr1im = GetSpriteImageID(spr1)
spr1imw = GetImageWidth(spr1im)
spr1imh = GetImageHeight(spr1im)
spr1xof = GetSpriteOffsetX(spr1)
spr1yof = GetSpriteOffsety(spr1)
spr2w# = GetSpriteWidth(spr2)
spr2h# = GetSpriteHeight(spr2)
spr2im = GetSpriteImageID(spr2)
spr2imw = GetImageWidth(spr2im)
spr2imh = GetImageHeight(spr2im)
spr2xof = GetSpriteOffsetX(spr2)
spr2yof = GetSpriteOffsety(spr2)
// we need to cache these rather than generating every time!!!!
spr1blk = CreateMemblockFromImage(spr1im)
spr2blk = CreateMemblockFromImage(spr2im)
// Algorithm:
// check the pixel in spr1 has an alpha value>0
// go from spr1 pixel to sprite space
// go from sprite space to world space
// check the pixel is on the sprite
// go from world space to spr2 space
// go from sprite 2 space to pixel space
// check the alpha value of this pixel - if greater than 0 then collision occured
// loop through all the pixels
for y = 0 to spr1imh-1
for x = 0 to spr1imw-1
// check the alpha
alpha1 = GetMemblockByte(spr1blk,12+ 3+ 4*(x+(y*spr1imw)))
if alpha1 = 0 then continue
xc# = GetSpriteXFromPixel( spr1, x ) -spr1xof
yc# = GetSpriteYFromPixel( spr1, y ) -spr1yof
xcw# = GetWorldXFromSprite( spr1, xc#, yc# )
ycw# = GetWorldYFromSprite( spr1, xc#, yc# )
if GetSpriteHitTest(spr2,xcw#,ycw#) = 0 then continue
xcb# = GetSpriteXFromWorld( spr2, xcw#, ycw# )
ycb# = GetSpriteYFromWorld( spr2, xcw#, ycw# )
xcf# = GetSpritePixelFromX( spr2, xcb#+spr2xof )
ycf# = GetSpritePixelFromY( spr2, ycb#+spr2yof )
xcol = floor(xcf#)
ycol = floor(ycf#)
offset = 12+ 3+ 4*(xcol+(ycol*spr2imw))
if offset>GetMemblockSize(spr2blk)
continue
endif
// Get the alpha from the secondimage
alpha2 = GetMemblockByte(spr2blk,offset)
if alpha2>0
DeleteMemblock(spr1blk)
DeleteMemblock(spr1blk)
//print("Pixel collision occurred")
//time# = Timer() - time#
//print(time#)
exitfunction 1
endif
next x
next y
// free the memblocks
DeleteMemblock(spr1blk)
DeleteMemblock(spr1blk)
//time# = Timer() - time#
//print(time#)
endfunction 0
.....The other issue I have is that each collision check between 2 sprites that are each 64x64 takes 0.5-1ms which is sllloooww. It generates iamge memblocks on every collision check which makes it slow and thes should be genrated once and kept. This algorithm really needs doing in the C source code rather than interpreted basic as to check 10 sprites for pixel perfect collision takes approx 8ms and so is a bit slow to do for lots of sprites. Its pre-checking for a collision between the sprite shapes too so theres not to much i can do to speed it up other than keep copies of image memblocks and that only helps a bit. I wouldnt use this for checking large sprites!!
Sprite shapes is by far the fastest way...pixelcollision is only good for low res sprites or...teir 2