Jump to content
• Advertisement

Public Group

# My Detection Function

This topic is 4815 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

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.

#### Share this post

##### Share on other sites
Advertisement

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
49
2. 2
3. 3
4. 4
5. 5
• Advertisement

• 10
• 28
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633409
• Total Posts
3011722
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!