I fount this pixel perfact collision code and have been using it for my asteroids game and for some reason only one of the bullets makes a collision and every time I remove a part of the code a different one makes a collision so what the heck is happening.
here is the code for the library for the pixel perfact collision and the code im using to check for a collision.
#include "DarkGDK.h"
#include "spriteCol.h"
int cromacolour = 0;
//***** 'showmask' ************************************************************
//+**** ========== ************************************************************
void showmask ( int img, int x, int y )
{
int sizex = 0;
int sizey = 0;
int offset = 0;
int pixelx = 0;
int pixely = 0;
int mask = 0;
//This function displays the mask stored in memblock img at co-ordinates x,y with no rotation.
//This function is designed for testing purposes
sizex = dbMemblockWord( img, 0 );
sizey = dbMemblockWord( img, 2 );
offset = 3;
for ( pixelx = 0; pixelx <= sizex - 1; pixelx++ ) {
for ( pixely = 0; pixely <= sizey - 1; pixely++ ) {
offset++;
mask = dbMemblockByte( img, offset );
if ( mask == 1 ) {
dbDot( x + pixelx, y + pixely );
}
}
}
}
//***** 'makemask' ************************************************************
//+**** ========== ************************************************************
void makemask ( char* file_str, int img )
{
int sizex = 0;
int sizey = 0;
int size = 0;
int offset = 0;
int pixelx = 0;
int pixely = 0;
int n = 0;
int mask = 0;
//This function will load an image into ID# img and make a memblock mask with the same ID#
dbLoadBitmap( file_str, 1 );
dbSetCurrentBitmap( 1 );
dbLockPixels();
sizex = dbBitmapWidth( 1 );
sizey = dbBitmapHeight( 1 );
size = ( sizex * sizey ) + 4;
dbMakeMemblock( img, size );
dbWriteMemblockWord( img, 0, sizex );
dbWriteMemblockWord( img, 2, sizey );
offset = 3;
for ( pixelx = 0; pixelx <= sizex - 1; pixelx++ ) {
for ( pixely = 0; pixely <= sizey - 1; pixely++ ) {
offset++;
n = dbPoint( pixelx, pixely );
if ( n == cromacolour ) {
mask = 0;
}
else {
mask = 1;
}
dbWriteMemblockByte( img, offset, mask );
}
}
dbUnlockPixels();
dbDeleteBitmap( 1 );
dbSetCurrentBitmap( 0 );
dbLoadImage( file_str, img, 1 );
}
//***** 'makeoutlinemask' *****************************************************
//+**** ================= *****************************************************
void makeoutlinemask ( char* file_str, int img )
{
int sizex = 0;
int sizey = 0;
int size = 0;
int offset = 0;
int pixelx = 0;
int pixely = 0;
int mask = 0;
int searchx = 0;
int searchy = 0;
int n = 0;
//This function will load an image into ID# img and make a memblock ouline mask with the same ID#
dbLoadBitmap( file_str, 1 );
dbSetCurrentBitmap( 1 );
dbLockPixels();
sizex = dbBitmapWidth( 1 );
sizey = dbBitmapHeight( 1 );
size = ( sizex * sizey ) + 4;
dbMakeMemblock( img, size );
dbWriteMemblockWord( img, 0, sizex );
dbWriteMemblockWord( img, 2, sizey );
offset = 3;
for ( pixelx = 0; pixelx <= sizex - 1; pixelx++ ) {
for ( pixely = 0; pixely <= sizey - 1; pixely++ ) {
offset++;
mask = 0;
for ( searchx = pixelx - OUTLINEWIDTH; searchx <= pixelx + OUTLINEWIDTH; searchx++ ) {
for ( searchy = pixely - OUTLINEWIDTH; searchy <= pixely + OUTLINEWIDTH; searchy++ ) {
if ( searchx >= 0 && searchy >= 0 && searchx < sizex && searchy < sizey ) {
n = dbPoint( searchx, searchy );
if ( n != cromacolour ) {
mask = 1;
}
}
}
}
dbWriteMemblockByte( img, offset, mask );
}
}
dbUnlockPixels();
dbDeleteBitmap( 1 );
dbSetCurrentBitmap( 0 );
dbLoadImage( file_str, img, 1 );
}
//***** 'deletemask' **********************************************************
//+**** ============ **********************************************************
void deletemask ( int img )
{
//This function will delete a memblock mask and associated image
dbDeleteImage( img );
dbDeleteMemblock( img );
}
//***** 'hotspotcollide' ******************************************************
//+**** ================ ******************************************************
int hotspotcollide ( int ida, int idb )
{
int mask = 0;
float rangea_flt = 0;
float rangeb_flt = 0;
float range_flt = 0;
int sizex = 0;
int sizey = 0;
int largest = 0;
float bearing_flt = 0;
int testx = 0;
int testy = 0;
//This function will test for collision between sprites idA and idB.
//idA will test it's hostpot against the pixel perfect mask of idB.
mask = 0;
rangea_flt = dbSpriteX( ida ) - dbSpriteX( idb );
rangeb_flt = dbSpriteY( ida ) - dbSpriteY( idb );
range_flt = dbSQRT( ( rangea_flt * rangea_flt ) + ( rangeb_flt * rangeb_flt ) );
sizex = dbMemblockWord( idb, 0 );
sizey = dbMemblockWord( idb, 2 );
if ( sizex > sizey ) {
largest = sizex;
}
else {
largest = sizey;
}
if ( range_flt <= largest ) {
bearing_flt = dbWrapValue( dbAtanFull( dbSpriteX( idb ) - dbSpriteX( ida ), dbSpriteY( ida ) - dbSpriteY( idb ) ) - dbSpriteAngle( idb ) );
testx = ( dbSin( bearing_flt ) * range_flt ) + dbSpriteOffsetX( idb );
testy = ( dbCOS( bearing_flt ) * range_flt ) + dbSpriteOffsetY( idb );
if ( testx >= 0 ) {
if ( testy >= 0 ) {
if ( testx <= dbMemblockByte( idb, 0 ) ) {
if ( testy <= dbMemblockByte( idb, 2 ) ) {
mask = dbMemblockByte( idb, 4 + ( ( sizey * testx ) + testy ) );
}
}
}
}
}
return mask;
}
//***** 'maskcollide' *********************************************************
//+**** ============= *********************************************************
int maskcollide ( int ida, int idb )
{
int mask = 0;
float rangea_flt = 0;
float rangeb_flt = 0;
float range_flt = 0;
int sizeax = 0;
int sizeay = 0;
int sizebx = 0;
int sizeby = 0;
int largest = 0;
int largestb = 0;
int offset = 0;
int checkx = 0;
int checky = 0;
float bearing_flt = 0;
float distancex_flt = 0;
float distancey_flt = 0;
float distance_flt = 0;
int offsetx = 0;
int offsety = 0;
float facing_flt = 0;
int testx = 0;
int testy = 0;
//This function will test for a mask collision between sprites idA and idB
//For optimal performance idA should be the smaller sprite.
mask = 0;
rangea_flt = dbSpriteX( ida ) - dbSpriteX( idb );
rangeb_flt = dbSpriteY( ida ) - dbSpriteY( idb );
range_flt = dbSQRT( ( rangea_flt * rangea_flt ) + ( rangeb_flt * rangeb_flt ) );
sizeax = dbMemblockWord( ida, 0 );
sizeay = dbMemblockWord( ida, 2 );
sizebx = dbMemblockWord( idb, 0 );
sizeby = dbMemblockWord( idb, 2 );
if ( sizeax > sizeay ) {
largest = sizeax;
}
else {
largest = sizeay;
}
if ( sizebx > largest ) {
largest = sizebx;
}
else {
if ( sizeby > largest ) {
largest = sizeby;
}
}
if ( range_flt <= largest ) {
if ( sizebx > sizeby ) {
largestb = sizebx;
}
else {
largestb = sizeby;
}
offset = 3;
for ( checkx = 0; checkx <= sizeax - 1; checkx++ ) {
for ( checky = 0; checky <= sizeay - 1; checky++ ) {
offset++;
if ( dbMemblockByte( ida, offset ) ) {
bearing_flt = dbWrapValue( dbAtanFull( checkx - dbSpriteOffsetX( ida ), checky - dbSpriteOffsetY( ida ) ) - dbSpriteAngle( ida ) );
distancex_flt = checkx - dbSpriteOffsetX( ida );
distancey_flt = checky - dbSpriteOffsetY( ida );
distance_flt = dbSQRT( ( distancex_flt * distancex_flt ) + ( distancey_flt * distancey_flt ) );
offsetx = ( dbSin( bearing_flt ) * distance_flt ) + dbSpriteX( ida );
offsety = ( dbCOS( bearing_flt ) * distance_flt ) + dbSpriteY( ida );
distancex_flt = dbSpriteX( idb ) - offsetx;
distancey_flt = dbSpriteY( idb ) - offsety;
distance_flt = dbSQRT( ( distancex_flt * distancex_flt ) + ( distancey_flt * distancey_flt ) );
if ( distance_flt <= largestb ) {
facing_flt = dbWrapValue( dbAtanFull( dbSpriteX( idb ) - offsetx, offsety - dbSpriteY( idb ) ) - dbSpriteAngle( idb ) );
testx = ( dbSin( facing_flt ) * distance_flt ) + dbSpriteOffsetX( idb );
testy = ( dbCOS( facing_flt ) * distance_flt ) + dbSpriteOffsetY( idb );
if ( testx >= 0 ) {
if ( testy >= 0 ) {
if ( testx < sizebx ) {
if ( testy < sizeby ) {
mask = dbMemblockByte( idb, 4 + ( ( sizeby * testx ) + testy ) );
}
}
}
}
}
}
if ( mask == 1 ) {
checky = sizeay;
}
}
if ( mask == 1 ) {
checkx = sizeax;
}
}
}
return mask;
}
My code
if ( maskcollide(BulletCount[0],50) == 1)
{
dbHideSprite(50);
}
else
{
dbShowSprite(50);
}
if ( maskcollide(BulletCount[1],50) == 1)
{
dbHideSprite(50);
}
else
{
dbShowSprite(50);
}
if ( maskcollide(BulletCount[2],50) == 1)
{
dbHideSprite(50);
}
else
{
dbShowSprite(50);
}
if ( maskcollide(BulletCount[3],50) == 1)
{
dbHideSprite(50);
}
else
{
dbShowSprite(50);
}
if ( maskcollide(BulletCount[4],50) == 1)
{
dbHideSprite(50);
}
else
{
dbShowSprite(50);
}
if ( maskcollide(BulletCount[5],50) == 1)
{
dbHideSprite(50);
}
else
{
dbShowSprite(50);
}
if ( maskcollide(BulletCount[6],50) == 1)
{
dbHideSprite(50);
}
else
{
dbShowSprite(50);
}
if ( maskcollide(BulletCount[7],50) == 1)
{
dbHideSprite(50);
}
else
{
dbShowSprite(50);
}
if ( maskcollide(BulletCount[8],50) == 1)
{
dbHideSprite(50);
}
else
{
dbShowSprite(50);
}
if ( maskcollide(BulletCount[9],50) == 1)
{
dbHideSprite(50);
}
else
{
dbShowSprite(50);
}
if ( maskcollide(BulletCount[10],50) == 1)
{
dbHideSprite(50);
}
else
{
dbShowSprite(50);
}
All is revealed with time.