How to do Collision Checking in SDL?

Started by
12 comments, last by boogyman19946 12 years, 3 months ago
If the rectangles were on the same x axis but something like 1 pixel from being perfectly alligned on the Y axis I do not think this would work..
To tired to test though ;D
Advertisement
Thanks for all the help guys but I'm still not getting the bigger picture here If I draw a rectangle on a piece of paper and then mark out each section of that rectangle like so

4Uh4W.png|

how would I get it so that my program knows how to intersect and also how to intersect

I had a thought if I could make a big massive picture and basically make a mask of that image like so

Unmasked image
07wwT.png


Masked Image
WkeP5.png



That I could then Code it exactly like this probably going to be wrong here but its a guess


int Intersects(SDL_Rect HitBoxRect SDL_Rect ImageRect)
{ //if hitbox exists
if(HitBoxRect)
{

//do a loop that checks for value equality
for(count; count < 1; count++)
{
//they collide
if(HitBoxRect.x && ImageRect.x = 100)
{
//move image downwards by 1
ImageRect.x -= 1;
}
}
}





is this correct or somewhere near correct I want the simplest method : )
anyone got any ideas?
If you're doing collisions between rectangles, then the separating axis theorem is your best bet.

To quote from http://www.metanetsoftware.com/technique/tutorialA.html#section1 says that if we take two convex shapes and we take their respective projections for all axes then if we can find a single axis where their projections don't overlap, we can be certain that the figures don't overlap either.

To put it in simple terms, we basically take each axis (x and y in this case) and take the coordinate points along that axis of the figure. If you'd like, you can think of it as looking at the "shadow" of the shape on the axis. This way we can check if the figures' shadows overlap on that axis. If they don't, then you're done: these two objects are not overlapping. If the shadows do overlap, then move on to the next axis. If all the axes overlap, then your objects are colliding.

I'd suggest checking out that site for visual pictures and whatnot. They can really help.

Yo dawg, don't even trip.

This topic is closed to new replies.

Advertisement