Don't mind if snag these into my stash o' scripts?
My main thoughts were that most of the time collision checks will fail, so why not check for failure conditions first.
Most of the other collision algorithms all work well, it's more a matter of taste I suppose. My code is just more in line with how I think and how I like to read my code. We're all different.
Neil, note that you can do without the sqrt(), using a multiplication of the other term instead :
double dist_squared = (dist_x * dist_x) + (dist_y * dist_y);
return dist_squared <= rt * rt;
Yeah, I think I discovered that I didn't need the sqrt() in another project, a 3D one actually (City3D, one of my many 3D experiments), when thinking about how to speed it up it occurred to me if I could eliminate the sqrt, which is expensive, that might help, and it did. It was a similar calculation as I recall. I never even thought to also update my DP2 game, but it's only 2D and much simpler. I'll probably add that in, thanks.
Is there a benefit to using multiplication over sqrt()?
sqrt() is very expensive, if you can avoid it, you should. His suggestion is a very nice one and will be faster.
I'm no math whiz either, I tend to think in non-mathematical ways and if I can solve a problem without it, all the better, though in recent years I have studied math more in depth when working in 3D (you have to) and have learned to love it.
Edit: in my circular collision, in case you haven't already guessed, g->r and p->r are the ghost and pacman radius (in pixels), that is, the distance from their centre. And the function basically checks to see if their radius overlaps or touches. You can reduce the radius a little so that they actually have to overlap a little, or increase it so that they barely touch, it's up to you, and it's easy to adjust rather than resizing a box.
Additionally, you could combine the two so that you have a box collision test, reject any collision if they are not close to each other like in the first test, and then if they are within a certain boundry, you could then do a circle collision or something else to fine tune it.
Often games do like Eric mentioned, AABB tests (Axis Aligned Bounding Boxes) to see if objects are close enough to even qualify for a more in depth collision, then you can go even more in depth if that is true for pixel perfect collision etc if need be, though for most 2D games, it is now.
I switched to circle collision for obvious reasons, my Pacman is round, and most of the ghosts are round as well, so it was a no brainer.