thats what i am already doing. i have been doing that for several months now.
the point is that it is slow. nested for loops are slow. i am looking for speed here, and I am wondering if a memcpy could simplify things anywhere in this process...
I have been tinkering with my code a little bit, and this is what I have come up with. I am not quite sure if it is legal, but I hope it is.
In the code below, you will see a function called AND_DTP. It is simply a function that runs through two matrixes and AND's the bits of every corresponding element in each matrix together, and returns them in a variable called finalMatrix, which is a reference variable. It also physically returns (as a return statement), a value indicating whether or not any collision occurred at all. finalMatrix is for pinpointing the exact location of a collision, the returned value is for finding out whether there was a collision or not. Although finalMatrix does not play a big part in this function, it does play a big part in other functions.
I will show my old function first, and then my new function. The function isnt that long, so dont worry:
Code:
int PLAYER_DTP::CheckForCollision ( MAP_DTP *Map )
{
int **finalMatrix, **mapMatrix;
int cX = Map->GetX() + CurPlayerX; //get starting coords of rect
int cY = Map->GetY() + CurPlayerY;
int tempy = cY;
//size the matrix
mapMatrix = new int *[ MATRIX_WIDTH ];
for ( int x = 0; x < MATRIX_WIDTH; x++, cX++ )
{
mapMatrix[x] = new int [ MATRIX_HEIGHT ];
for ( int y = 0; y < MATRIX_HEIGHT; y++, cY++ )
{
mapMatrix[x][y] = Map->ForegroundBitMatrix[cX][cY];
}
cY = tempy;
}
// AND the matrixes together:
int isCollision = AND_DTP ( CharacterAnimation[CurAnimationIndex]->GetCurrentFrame()->bitMatrix,
mapMatrix, finalMatrix );
delete [] mapMatrix;
delete [] finalMatrix;
return isCollision;
}
that was my old function. Now I have changed it to the following function. Notice the memcpy statmenet. Check the legality of it for me.
Code:
int PLAYER_DTP::CheckForCollision ( MAP_DTP *Map )
{
int **finalMatrix;
int mapMatrix [ MATRIX_WIDTH ][ MATRIX_HEIGHT ];
int cX = Map->GetX() + CurPlayerX; //get starting coords of rect
int cY = Map->GetY() + CurPlayerY;
int tempy = cY;
//Since we are checking for a collision between the player and just a small piece of the map
//We need to take a small part of the map, equal to the size of the player, that corresponds
//to the position that the player is in, and put it in a seperate matrix. We will then
//do a boolean AND operation on the two matrixes that will return a final matrix which can
//tell us whether a collision has occurred or not.
//Size and fill the matrix containing the piece of the map:
mapMatrix = new int *[ MATRIX_WIDTH ];
for ( int x = 0; x < MATRIX_WIDTH; x++, cX++ )
{
mapMatrix[x] = new int [ MATRIX_HEIGHT ];
memcpy ( mapMatrix[x], *(Map->ForegroundBitMatrix[cX] + 4), MATRIX_HEIGHT );
}
// AND the matrixes together:
int isCollision = AND_DTP ( CharacterAnimation[CurAnimationIndex]->GetCurrentFrame()->bitMatrix,
mapMatrix, finalMatrix );
delete [] mapMatrix;
delete [] finalMatrix;
return isCollision;
}