Sure thing Matt. Here's the code to a simple collision detection routine for my sprite engine with an explanation afterwards:
- Code:
// checks collisions between a sprite (index) and all sprites on a layer (zOrder) and returns the sprite index on collision or -1
DWORD CSpriteHandler::detectCollisions(DWORD index, DWORD zOrder)
{
RECT s1 = {NULL};
::SetRect(&s1;,
m_sprite[index]->m_animDef[m_sprite[index]->getAnimationState()].getCurrentFrame() * m_sprite[index]->getSize().x,
m_sprite[index]->getAnimationState() * m_sprite[index]->getSize().y,
m_sprite[index]->m_animDef[m_sprite[index]->getAnimationState()].getCurrentFrame() * m_sprite[index]->getSize().x + m_sprite[index]->getSize().x,
m_sprite[index]->getAnimationState() * m_sprite[index]->getSize().y + m_sprite[index]->getSize().y);
RECT s = m_sprite[index]->getBoundingBox();
for(DWORD i = 0; i < __MAXSPRITES; i++) {
if(m_sprite[i] && i != index) {
if(m_sprite[i]->getZOrder() == zOrder && m_sprite[i]->isVisible()) {
RECT r = m_sprite[i]->getBoundingBox();
RECT r1 = {NULL};
::SetRect(&r1;,
m_sprite[i]->m_animDef[m_sprite[i]->getAnimationState()].getCurrentFrame() * m_sprite[i]->getSize().x,
m_sprite[i]->getAnimationState() * m_sprite[index]->getSize().y,
m_sprite[i]->m_animDef[m_sprite[i]->getAnimationState()].getCurrentFrame() * m_sprite[i]->getSize().x + m_sprite[i]->getSize().x,
m_sprite[i]->getAnimationState() * m_sprite[i]->getSize().y + m_sprite[i]->getSize().y);
POINT intersection = {-1, -1};
if(CGapiSurface::Intersect(s.left, s.top, &m;_sprite[index]->m_surface, &s1;,
r.left, r.top, &m;_sprite[i]->m_surface, &r1;,
&intersection;) != GD_OK) return -1;
if(intersection.x > -1 || intersection.y > -1) {
return i;
}
}
}
}
return -1;
}
First of all, this code simply checks all the sprites on a particular rendering layer to see if they collide with the sprite in question, and then returns the index to the sprite it collided with. At this time it doesn't handle simultaneous collisions with multiple sprites (since I didn't need that support for my current project, its not implemented).
A quick overview of my sprite engine so you understand some of the methods relating to them. The engine handles any number of sprites, each with any number of animation definitions (for each animation such as walk north, walk south, run east, shoot west, etc), and each on any zorder layer for collisions and rendering order (such as a plane flying over a bird flying over a dog on the ground). These sprites are all registered in a sprite handler which does all the rendering in the proper order with a single call.
So, back to the detectCollisions method (one of several different collision methods in the sprite engine, but the most basic one for the example) which takes two arguments - index and zOrder. The argument 'index' is simply the index value of the source sprite (such as a bullet sprite), and the argument 'zOrder' is the layer it is rendered on so we don't try to check for collisions between planes and cars for instance (unless we really want to).
The first thing detectCollisions does is create a couple of source RECTs based on our sprite location (RECT s), and its current animation frame (RECT s1). We'll need both of these to do our collision test with the other sprites.
Next, we loop thru ALL of the sprites in the world (yes, there is room for optimization here), ensure that we're not testing for a collision against ourself, and check that the sprite is on the layer of interest.
If we get past those couple of tests, we have a sprite that we could potentially have a collision with and we proceed to test it. We'll need a couple of testing RECTs similar to above to do that - one based on this sprite's location (RECT r) and another on its current animation frame (RECT r1).
Now the method that does all the work - CGapiSurface::Intersect( ). As the GapiDraw docs explain, this method does a quick rectangular collision test first, then, if warrented, it does a pixel perfect collision test returning the point of intersection between the sprites.
Calling this method is pretty simple - we pass it:
( 1 ) the top left corner of our source RECT s,
( 2 ) the CGapiSurface of our source sprite (by the way, ensure all the surfaces use the same transparent color, ie Magenta, or this won't work at all),
( 3 ) the source RECT s1 (essentially the area in the CGapiSurface we're testing against),
( 4 ) the top left corner of our testing RECT r,
( 5 ) the CGapiSurface of our test sprite,
( 6 ) the testing RECT r1, and finally
( 7 ) the address of a POINT structure to return the intersection point in
If CGapiSurface::Intersection successfully executes (regardless of any collisions) it will return GD_OK. If there is a collision, the intersection point will be > -1,-1 and we know what sprite we have collided with and return that value to act upon.
Whew! That was alot longer of an explanation than I intended, but if it helps you get your collisions working its worth it. If I remember correctly I had troubles getting this implemented until I realized the transparent colors of the surfaces being tested needed to be set (and be the same).
-- GR