Hey everyone,
I've been coding a collision detection function for about 12 hours now using DarkGDK, DirectX, and C++.
My 2D game detects the function call. I know this because the frame rate drops like crazy and I can fix that.
My issue is in this function here:
Code:
bool pixelCollision(int spriteOne, int spriteTwo)
{
int minX = 0;
int maxX = 0;
int minY = 0;
int maxY = 0;
vector<int> spriteOneVect;
vector<int> spriteTwoVect;
/*---------------------------------------------------------------------------*/
// Sprite Masking //
// //
/*---------------------------------------------------------------------------*/
//fill vectors with '0' for non-opaque and '1' for opaque colors from bitmap
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
if(dbSpriteX(spriteOne) <= dbSpriteX(spriteTwo))
{
minX = dbSpriteX(spriteTwo) - dbSpriteX(spriteOne);
maxX = dbSpriteX(spriteOne) + dbSpriteWidth(spriteOne) - dbSpriteX(spriteTwo);
}
else
{
minX = dbSpriteX(spriteOne) - dbSpriteX(spriteTwo);
maxX = dbSpriteX(spriteTwo) + dbSpriteWidth(spriteTwo) - dbSpriteX(spriteOne);
}
if(dbSpriteY(spriteOne) <= dbSpriteY(spriteTwo))
{
minY = dbSpriteY(spriteTwo) - dbSpriteY(spriteOne);
maxY = dbSpriteY(spriteOne) + dbSpriteHeight(spriteTwo) - dbSpriteY(spriteTwo);
}
else
{
minY = dbSpriteY(spriteOne) - dbSpriteY(spriteTwo);
maxY = dbSpriteY(spriteTwo) + dbSpriteHeight(spriteOne) - dbSpriteY(spriteOne);
}
for(int i = minY; i < maxY; i++)
{
for(int j = minX; j < maxX; j++)
{
if(spriteOneVect.size() <= (i*j) && spriteTwoVect.size() <= (i*j))
{
if(spriteOneVect[i*j] == spriteTwoVect[i*j] && spriteOneVect[i*j] == 1)
return true;
}
}
}
return false;
}
The highlighted code in red never returns true. It is my vector bounds I know, but I don't know where. As you can see, I have set minX,maxX, minY, and maxY values (I have a hunch its in here).
In case you are wondering, this detects collision between 2 bitmap images, it allows for the opaque colors to be used for detection instead of the non-opaque. This is a solution for pixel perfect collision detection using darkGDK functions.
If you can help me find my bound errors, i'd be very grateful.
Please ask questions if you have any, thanks