Here is the code pertaining to the Collison Detection...
int Fine_Collision_Detection()
{
// This function returns 1 if Objs1 and Objs2 have collided. This function returns 0 otherwsie.
// Collision detection is based on checking overlapping pixels to see if both are non-transparent.
u32 pixel1; // Storage For Pixel Alpha for Object 1
u32 pixel2; // Storage For Pixel Alpha for Object 2
int OffsetX = objs1.x - objs2.x; // Determine Offset Between Objects
int OffsetY = objs1.y - objs2.y; // Determine Offset Between Objects
int collision = 0; // Assume there is no collision
int X;// Loop counter
int Y;// Loop counter
for(Y= 1; Y
0){break;}
for(X= 1; X0){break;}
// Check to see that the point exists on both object
if(((X + OffsetX) > 0) && ((X + OffsetX) < objs2.Current_Sprite.w) &&
((Y + OffsetY) > 0) && ((Y + OffsetY) < objs2.Current_Sprite.h))
{
// Get the Alpha component of both objects
pixel1 = GRRLIB_GetPixelFromtexImg(X,Y,objs1.Current_Sprite)&0xFF;
pixel2 = GRRLIB_GetPixelFromtexImg(X+OffsetX,Y+OffsetY,objs2.Current_Sprite)&0xFF;
// If the Alpha level for both objects is not 0 (transparent) then we have a collision
if((pixel1>0)&&(pixel2>0)){collision=true;break;}
}
}
}
return collision;
}
objs1 and objs2 are structures which include Current_Sprite which is a GRRLIB_texImage.
Basically I figure out the distances of the two objects and store it in OffsetX and OffsetY.
Then I run through all the points in one of the objects and seeing if I apply the Offsets to this point,
if the resulting point is somewhere in the second object. If not check the next point.
If so then I get the pixel color of the point in both objects. I then mask off everything except for the lowest byte (alpha value).
If both alpha values are not 0 (transparent) then there is a collision. In such a case I set the collsion
variable which then breaks out of all the loops (so that I don't perform unnecessary checking).
The value of the collision variable is returned as the function result.
You will probably want to modify the code a bit depending on the object structure that you have and
you will probably also want to make objs1 and objs2 inputs to the function. In my case I just hard coded
the selection because I never needed to call the function for any other object combinations.
Edited 1 time(s). Last edit at 03/08/2010 11:16AM by LordAshes.