Public Group

# sprite collision detection

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

## Recommended Posts

I am trying to use the following code for sprite based collision detection.
//bounding  box collision detectionint 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 on other sites
That would be the logical thing to assume, the function takes two objects "sprite1" and "sprite2" of the class or struct type SPRITE.

##### Share on other sites
my next question is how would I use this code.

##### 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 on other sites
well here is some code for collision detection using two rectangles as bounding boxes.

##### 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

• 10
• 9
• 48
• 12
• 10
• ### Forum Statistics

• Total Topics
631385
• Total Posts
2999705
×