Collision

I've been working on collision detection for the past 4 hours without actually getting very far. It's glitchy, slow, long, and way too complex. Is a simpler way to detect a hit than by using the x/y positions AND x/y sizes of BOTH objects????
if you have simple bounding boxes:

1
2
3
4
5
6
7
8
9
10
11
//  assuming rects have l,t,r,b sides:

bool DoRectsCollide(const Rect& a, const Rect& b)
{
  if(a.r < b.l)  return false;
  if(b.r < a.l)  return false;
  if(b.b < a.t)  return false;  // assuming +y is down and -y is up
  if(a.b < b.t)  return false;

  return true;
}
Sargon, do you still have my engine?
The "SDL_Sprite_Dude_Who_Jumps_Around_Boxes_Engine"? Yup
Or the "Will_Project_Engine" (which is the same) Yup again.
Thanks Disch, that makes a whole lot more sense that what I was trying

EDIT: Disch, I may sound dumb but how do you get bottom and top???
Last edited on
bro, seriously my code has this exact same function...

and the top is simply your y position the bottom is your y pos + size y
Last edited on
If you want a good book on collision detection I recommend this book:
http://www.amazon.com/Real-Time-Collision-Detection-Interactive-Technology/dp/1558607323/ref=sr_1_1?ie=UTF8&qid=1298755693&sr=8-1

Although it is geared towards complex collision detection mostly in 3D it also has everything you need to know for 2D collision as well. Math heavy but probably the best book you can get on the subject. Just a suggestion to further your knowledge in the subject if you feel so inclined.
@Ulti: Ok, that's what I figured (but I thought I'de check anyways)

@bcthund: Thanks! I'll certainly look into that!
Disch, words can't quite say how much I thank you! With a little addition, your code worked beautifully (the addition was due to the fact the player faces different directions) :) :)
Last edited on
Topic archived. No new replies allowed.