Public Group

# intersecting rectangles

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

## Recommended Posts

I am still unsure of how to apply this code for collision detection and a bounding box. struct RECTANGLE { int left; int top; int right; int bottom; }; bool IntersectRect(const RECTANGLE * r1, const RECTANGLE * r2) { return ( r2->left > r1->right && r2->right < r1->left && r2->top > r1->bottom && r2->bottom < r1->top ); }

##### Share on other sites
the concept behind it is that if none of the sides of r2 are inside r1, then r1 cannot be colliding with r2

so in this case, if it returns true, then there is no collision between the 2 boxes

so what you do is for any object you want to apply collision to, you set the box around that object, and you can check collision between it, and any otehr object with a box

##### Share on other sites
Basic usage:
RECTANGLE ship;RECTANGLE enemyBullet;if(IntersectRect(&ship, &enemyBullet)){   // mayday code goes here}

However, your IntersectRect function doesn't work correctly: it returns true when r2's left side lies to the right of r1's right side, and if r2's right side lies to the left of r1's left side, and... well, those two things are never going to be true at the same time already so your function always returns false.

To fix it, replace all &&'s with ||'s, negate the result (using !) and swap the < and >'s in the last two lines (the top/bottom checks). The function will then return true if the rectangles collide, and false if they don't. Be sure to run some tests to verify that it works correctly! Also, it's better to pass const references than const pointers. With pointers, you can pass invalid pointers, while references must always refer to an object. In other words, they're safer to use.

Some more notes: UPPERCASE names are usually used for macro's. For classes and structs, CamelCase is much more common. Besides, uppercase looks so 'shouting'... ;)

Also, it's a good idea to write a constructor for this struct. Otherwise, you always have to fill in the values manually, but with a constructor, you can immediately initialize it with some given values.

struct Rectangle{   // Default constructor: all variables are set to 0 if you create a default Rectangle,   // which happens when you type 'Rectangle a;'   Rectangle()    : left(0), top(0), right(0), bottom(0)    // The line above is an 'initializer list'. It initializes member variables.   {   }   // This is a 'custom' constructor. We can now create Rectangles and   // immediately specify their starting values: 'Rectangle a(10, 50, 30, 20);'   Rectangle(int l, int t, int r, int b)    : left(l), top(t), right(r), bottom(b)   {      if(left > right || bottom > top)      {         // Warn the user - this is an invalid rectangle!         // (Or maybe you're fine with that?)      }   }   int left;   int top;   int right;   int bottom;};

1. 1
2. 2
Rutin
21
3. 3
4. 4
A4L
15
5. 5
khawk
14

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633737
• Total Posts
3013607
×