-------------------------------
UPDATED THREAD 4/2/2010 10:36pm
-------------------------------
-Verified readPixel() to accurately count pixels in bitmap image
--Have not verified with animations yet...Once we have this, we should be good
With the code below, and using darkGDK, you will be able to get PIXEL PERFECT COLLISIONS for 2D games. But only one thing remains in the way....I can\\\\\\\'t seem to find what is messing up in my collision function...have a look:
STEVE\\\\\\\'S getPixel() function (gamedev.net)
D3DCOLOR readPixel(LPDIRECT3DTEXTURE9 pTexture, UINT x, UINT y)
{
// Lock the texture:
D3DLOCKED_RECT rect;
HRESULT hResult = pTexture->LockRect(0, &rect, NULL, D3DLOCK_READONLY);
if(FAILED(hResult))
{
// Failed to lock the texture, error
return 0;
}
// Get the bits as a BYTE*
const BYTE* pBits = (BYTE*)rect.pBits;
// Seek to the correct pixel and read it
DWORD nBytesPerPixel = 3;
const DWORD* pPixel = (DWORD*)(pBits + y * rect.Pitch + x*nBytesPerPixel);
DWORD dwPixel = *pPixel;
// Unlock the texture
pTexture->UnlockRect(0);
// Return the pixel
return dwPixel;
}
My almost complete pixel collision...I kid you knot, I had this almost working(perfect) last night...someone threw a wrench into it.
bool pixelCollision(int spriteOne, int spriteTwo)
{
vector<int> spriteOneVect;
vector<int> spriteTwoVect;
/*---------------------------------------------------------------------------*/
// Sprite Masking //
// //
/*---------------------------------------------------------------------------*/
for(int y = 0; y < dbSpriteHeight(spriteOne); y++)
{
for(int x = 0; x < dbSpriteWidth(spriteOne); x++)
{
if(readPixel(dbGetImagePointer(spriteOne),x,y) == D3DCOLOR_XRGB(255,0,255))
{
spriteOneVect.push_back(0);
}
else
{
spriteOneVect.push_back(1);
}
}
}
for(int y = 0; y < dbSpriteHeight(spriteTwo); y++)
{
for(int x = 0; x < dbSpriteWidth(spriteTwo); x++)
{
if(readPixel(dbGetImagePointer(spriteTwo),x,y) == D3DCOLOR_XRGB(255,0,255))
{
spriteTwoVect.push_back(0);
}
else
{
spriteTwoVect.push_back(1);
}
}
}
/*---------------------------------------------------------------------------*/
// Collision Overlap //
// //
/*---------------------------------------------------------------------------*/
//find min and max coodinates of collision
int minX;
int maxX;
int minY;
int maxY;
if (dbSpriteX(spriteOne) <= dbSpriteX(spriteTwo))
{
minX = dbSpriteX(spriteTwo);
}
else
{
minX = dbSpriteX(spriteOne);
}
if(dbSpriteX(spriteOne) + dbSpriteWidth(spriteOne) <= dbSpriteX(spriteTwo) + dbSpriteWidth(spriteTwo))
{
maxX = dbSpriteX(spriteOne) + dbSpriteWidth(spriteOne);
}
else
{
maxX = dbSpriteX(spriteTwo) + dbSpriteWidth(spriteTwo);
}
if(dbSpriteY(spriteOne) <= dbSpriteY(spriteTwo))
{
minY = dbSpriteY(spriteTwo);
}
else
{
minY = dbSpriteY(spriteOne);
}
if(dbSpriteY(spriteOne) + dbSpriteHeight(spriteOne) <= dbSpriteY(spriteTwo) + dbSpriteHeight(spriteTwo))
{
maxY = dbSpriteY(spriteOne) + dbSpriteHeight(spriteOne);
}
else
{
maxY = dbSpriteY(spriteTwo) + dbSpriteHeight(spriteTwo);
}
for(int currentY = minY; currentY < maxY; currentY++)
{
int sOneYAP = currentY - dbSpriteY(spriteOne)+1;
int sTwoYAP = currentY - dbSpriteY(spriteTwo)+1;
for(int currentX = minX; currentX < maxX; currentX++)
{
int sOneXAP = currentX - dbSpriteX(spriteOne)+1;
int sTwoXAP = currentX - dbSpriteX(spriteTwo)+1;
if(spriteOneVect[sOneXAP*sOneYAP] == spriteTwoVect[sTwoXAP*sTwoYAP] && spriteOneVect[sOneXAP*sOneYAP] == 1)
{
return true;
}
}
}
return false;
}
I can show you how to implement this in your game as well, I have created a TEST game (2 sprites, 1 animated, 1 still).
Ask questions, post feedback
Thanks.