Public Group

# My Detection Function

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

## Recommended Posts

Hi all! In SDL in Code::Blocks I made a function for collision detection... Its here not in "SDL" cuz its how the function compares, not SDL itslef. It'll take me a lot of time to draw all the graphics that'll need this function, so I just wanted to ask if you think it'll actually detect collision. Here it is:
[source language="CPP"]
int DetectCollision(SDL_Surface *sprite1, int x1, int y1,
SDL_Surface *sprite2, int x2, int y2)
{
//Width
if ((x1 + sprite1->w) == x2 && (y1 >= y2 && y1 <= (y2 + sprite2->h) && y2 >= y1 && y2 <= (y1 + sprite1->h)))
return 1;
if (x1 == (x2 + sprite2->w) && (y1 >= y2 && y1 <= (y2 + sprite2->h) && y2 >= y1 && y2 <= (y1 + sprite1->h)))
return 1;
//Height
if ((y1 + sprite1->h) == y2 && (x1 >= x2 && x1 <= (x2 + sprite2->w) && x2 >= x1 && x2 <= (x1 + sprite1->w)))
return 1;
if (y1 == (y2 + sprite1->h) && (x1 >= x2 && x1 <= (x2 + sprite2->w) && x2 >= x1 && x2 <= (x1 + sprite1->w)))
return 1;

return 0;
}


I just want to know if you think it'll do the job. And yeah I know it might be SLOW, but I need detection right now, not speed. Thanks.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 33
• 16
• 11
• 10
• ### Forum Statistics

• Total Topics
634124
• Total Posts
3015630
×