Welcome! Log In Create A New Profile

Advanced

Fine Collision Detection *SOLVED*

Posted by LordAshes 
Fine Collision Detection *SOLVED*
February 15, 2010 04:58AM
I am new to GRRLIB.

I have seen some code that uses GRRLIB_RectOnRect to detect collision. This is fine for some applications but not good enough when you need your collision to be accurate. For example, consider to circles. The bounding square (assuming there is not wasted space) would mean that an object coming directly at a 45 degree angle towards the circle would trigger a collision about 20% earlier (based on the circle's size). Thus two circles coming together at 45 degrees would trigger a collision approx 40% earlier (based on circle size).

Is there an easy GRRLIB function that does a fine collision check based on two objects taking into account the alpha transparency of the object (i.e. does not trigger collisions on parts of the object that are transparent)?

If there is such a function, you probably don't need to explain it too much, all I need is to be pointed in the right direction and then I can do the detailed research myself.

If there is not such function, has anyone seen some good code which implements these types of checks?



Edited 1 time(s). Last edit at 03/15/2010 11:55AM by LordAshes.
Re: Fine Collision Detection
February 15, 2010 09:19AM
There is pixel collision detection where you use a B/W mask of the sprite to check per pixel for a collision, you would want to do a rect collision before hand an ensure that passed before doing a cpu heavy pixel on pixel test.

If you are dealing with circles a method would be to check distance from the center point + the radius and see if it overlaps with another object.

I have a bunch of good links I try and dig up tomorrow. But this should at least give you something to google for now.
Re: Fine Collision Detection
February 15, 2010 11:14AM
You might want to take a look at [www.box2d.org] which can not only handle collision detection, but also physic simulation if you want.
Re: Fine Collision Detection
February 15, 2010 11:50AM
Scanff,

The B/W mask type collision detection is what I had in mind if I was going to code it manually but I wanted to see if there are any function to help with this...no point in re-inventing the wheel if the functions already exist. I look forward to the links.

Thanks for the tip with circles but that was just an example which easily demonstrates how far off a rectangular collision check can be even with simple shapes.

Daid,

Thanks for the link. I followed it but have not had time to actually look at the two posts (Box2D and the other one for 3D). However I am guessing they will be probably much more than I need at this time.
Re: Fine Collision Detection
February 15, 2010 07:09PM
Yes box2d is a great library, I'm using it in a game I'm developing right now. I don't think it does pixel collision though.

Here's a quick article I found on pixel collision :- [www.gamedev.net].

I do have some code that does something like this, it's in SDL so references SDL surfaces but it might help if you wanted to take a look.
Re: Fine Collision Detection *SOLVED*
March 08, 2010 02:57AM
SOLVED.

Since I know the positions of my 2 objects, I just run a loop through the x and y coordinates over the combined object's area. If the point does not map into both objects then discard it and continue. If the point maps into both objects then use GRRLIB's get pixel from texture function to compare the alpha values. If both points are not transparent then you have a collision.

I am sure the process could be optimized by only checking the overlap area but I did not find an easy way to calculate this since the objects can overlap in any number of ways (including being inside each other). The above solution works great so I am happy.



Edited 1 time(s). Last edit at 03/08/2010 02:57AM by LordAshes.
Re: Fine Collision Detection
March 08, 2010 05:54AM
would love to see your code for this as i may be doing a collision type game next!
Re: Fine Collision Detection
March 08, 2010 11:16AM
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; Y0){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.
Sorry, only registered users may post in this forum.

Click here to login