Advertisement Jump to content
Sign in to follow this  
nullsquared

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.

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


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

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!