• Advertisement
Sign in to follow this  

sprite collision detection

This topic is 2691 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to use the following code for sprite based collision detection.

//bounding box collision detection
int Collision(SPRITE sprite1, SPRITE sprite2)
{
RECT rect1;
rect1.left = sprite1.x;
rect1.top = sprite1.y;
rect1.right = sprite1.x + sprite1.width;
rect1.bottom = sprite1.y + sprite1.height;

RECT rect2;
rect2.left = sprite2.x;
rect2.top = sprite2.y;
rect2.right = sprite2.x + sprite2.width;
rect2.bottom = sprite2.y + sprite2.height;

RECT dest;
return IntersectRect(&dest, &rect1, &rect2);
}


what does "SPRITE sprite1" refer to , a struct??

Share this post


Link to post
Share on other sites
Advertisement
That would be the logical thing to assume, the function takes two objects "sprite1" and "sprite2" of the class or struct type SPRITE.

Share this post


Link to post
Share on other sites
Paste that function somewhere in your code, I like to use header files, but you could just put it right above int main(). (If using C++)

Then, call it using Collision(Sprite 1, Sprite 2).

Its gonna returns some info, not exactly sure what, since you didn't post the IntersectRect() function.

Share this post


Link to post
Share on other sites
sorry here is the code
if(rect1.right>=rect2.left||rect1.left<=rect2.right)
//collision
else if (rect1.bottom>=rect2.top||rect2.bottom>=rect1.top)
//collsion

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement