<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Bounding Box Collision Detection</title>
		<link>http://www.allegro.cc/forums/view/595810</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 03 Apr 2008 04:15:03 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I am totally lost on this topic. Could someone please explain to me exactly how this is done? Ive seen algorithms, but im clueless as to how they work.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitikonti553)</author>
		<pubDate>Tue, 01 Apr 2008 09:52:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Basically you treat your sprites as rectangles and you check the coordinates and size to see if they intersect eachother or not.<br />Sorry if thats too vague, I&#39;m pretty much still sleeping as I just got to work <img src="http://www.allegro.cc/forums/smileys/cool.gif" alt="8-)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ixilom)</author>
		<pubDate>Tue, 01 Apr 2008 11:48:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You just give each collidable object a bounding box, which is a rectangle that is just large enough for the object to fit inside. Then you run a check to see if any bounding boxes collide with each other. Consider this code:
</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="c">// Player is an object that contains an integer x and an integer y that</span></td></tr><tr><td class="number">2</td><td><span class="c">// store the player's position</span></td></tr><tr><td class="number">3</td><td>&#160;</td></tr><tr><td class="number">4</td><td><span class="c">// Wall is an object that contains an integer x and an integer y that</span></td></tr><tr><td class="number">5</td><td><span class="c">// store the wall's position</span></td></tr><tr><td class="number">6</td><td>&#160;</td></tr><tr><td class="number">7</td><td><span class="c">// note that both the player and the wall are size 32 pixels by 32 pixels</span></td></tr><tr><td class="number">8</td><td>&#160;</td></tr><tr><td class="number">9</td><td><span class="k1">if</span><span class="k2">(</span>Player.x <span class="k3">&gt;</span><span class="k3">=</span> wall.x <span class="k3">&amp;</span><span class="k3">&amp;</span> Player.x <span class="k3">&lt;</span><span class="k3">=</span> wall.x <span class="k3">+</span> <span class="n">32</span><span class="k2">)</span> <span class="c">// if the player's x position is</span></td></tr><tr><td class="number">10</td><td>                                                  <span class="c">// inside the wall's x position</span></td></tr><tr><td class="number">11</td><td><span class="k2">{</span></td></tr><tr><td class="number">12</td><td>   <span class="k1">if</span><span class="k2">(</span>Player.y <span class="k3">&gt;</span><span class="k3">=</span> wall.y <span class="k3">&amp;</span><span class="k3">&amp;</span> Player.y <span class="k3">&lt;</span><span class="k3">=</span> wall.y <span class="k3">+</span> <span class="n">32</span><span class="k2">)</span> <span class="c">// run the same check for the</span></td></tr><tr><td class="number">13</td><td>                                                     <span class="c">// y coordinate.</span></td></tr><tr><td class="number">14</td><td>   <span class="k2">{</span></td></tr><tr><td class="number">15</td><td>      <span class="k1">return</span> <span class="k1">true</span><span class="k2">;</span></td></tr><tr><td class="number">16</td><td>   <span class="k2">}</span></td></tr><tr><td class="number">17</td><td><span class="k2">}</span></td></tr><tr><td class="number">18</td><td>&#160;</td></tr><tr><td class="number">19</td><td><span class="c">// this checks collision for the upper left corner of the player object,</span></td></tr><tr><td class="number">20</td><td><span class="c">// you also need to check the other three corners of the player object,</span></td></tr><tr><td class="number">21</td><td><span class="c">// which are (Player.x, Player.y + 32), (Player.x + 32, Player.y), and</span></td></tr><tr><td class="number">22</td><td><span class="c">// (Player.x + 32, Player.y + 32)</span></td></tr></tbody></table></div></div><p>

Note that this method only works with bounding boxes that are the same size. I had a problem in programming class that dealt with differently sized rectangles, and the only method I found for solving it was very inefficient. Basically I checked every single pixel in one rectangle to see if it collided with the other rectangle. Not something I would recommend trying in a game because of speed issues. I would be very interested in learning a fast way of checking collision of two differently sized rectangles.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Black)</author>
		<pubDate>Tue, 01 Apr 2008 13:10:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>For each dimension of each box check whether it&#39;s endpoints are contained on the line segment of the other box. If any of the 4 checks per dimension are true it overlaps on that dimension. If this is true for all dimensions, the rectangles overlap. This will work for rectangles of different sizes as well and accounts for cases where the larger rectangle completely overlaps the other.</p><p>Alternatively check whether the left edge of r2 is less than or equal to the right edge of r1 and the right edge of r2 is greater than or equal to the left edge of r1. As long as they&#39;re both true , it overlaps on the x dimension. Perform a similar check for the other dimensions. If they overlap on all dimensions then they are occupying at least some of the same space. This will work for objects of differing size as well.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Tue, 01 Apr 2008 14:28:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thank you replying. I think Im getting it now; except for your code Neil Black, when i tried using that method, my boxes kept getting sucked to the bottom right corner of my screen o.e
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitikonti553)</author>
		<pubDate>Wed, 02 Apr 2008 05:21:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code snippet"><div class="inner"><pre><span class="k1">struct</span> Box<span class="k2">{</span>
  <span class="k1">int</span> x, y, w, h<span class="k2">;</span> <span class="c">// position and size (width, height)</span>
<span class="k2">}</span><span class="k2">;</span>

Box box1, box2<span class="k2">;</span> 

<span class="k1">bool</span> BoundingBoxCheck<span class="k2">(</span><span class="k2">)</span>
<span class="k2">{</span>
   <span class="k1">if</span><span class="k2">(</span>box1.x <span class="k3">&gt;</span> <span class="k2">(</span>box2.x<span class="k3">+</span>box2.w<span class="k2">)</span><span class="k2">)</span> <span class="k1">return</span> <span class="k1">false</span><span class="k2">;</span> <span class="c">// box1 is too far right, no collision</span>
   <span class="k1">else</span> <span class="k1">if</span><span class="k2">(</span><span class="k2">(</span>box1.x<span class="k3">+</span>box1.w<span class="k2">)</span> <span class="k3">&lt;</span> box2.x<span class="k2">)</span> <span class="k1">return</span> <span class="k1">false</span><span class="k2">;</span> <span class="c">// box1 is too far left, no collision</span>
   <span class="k1">else</span> <span class="k1">if</span><span class="k2">(</span>box1.y <span class="k3">&gt;</span> <span class="k2">(</span>box2.y<span class="k3">+</span>box2.h<span class="k2">)</span><span class="k2">)</span> <span class="k1">return</span> <span class="k1">false</span><span class="k2">;</span> <span class="c">// box1 is too far down, no collision</span>
   <span class="k1">else</span> <span class="k1">if</span><span class="k2">(</span><span class="k2">(</span>box1.y<span class="k3">+</span>box1.h<span class="k2">)</span> <span class="k3">&lt;</span> box2.y<span class="k2">)</span> <span class="k1">return</span> <span class="k1">false</span><span class="k2">;</span> <span class="c">// box1 is too far up, no collision</span>
   <span class="k1">else</span> <span class="k1">return</span> <span class="k1">true</span><span class="k2">;</span> <span class="c">// there is a collision</span>
<span class="k2">}</span>
</pre></div></div><p>

This is a basic bounding box check. If any of the if statements are satisfied, then there is no way the boxes can be overlapping because they are too far left, right, up, down, etc. If none of them are, that means there is an overlap somewhere.</p><p>This can be easily implemented with BITMAPs in allegro, and it works with differently sized bounding boxes. Unfortunately, it&#39;s not as precise with certain sprites with a lot of &quot;magic pink&quot; in them. Further, it doesn&#39;t detect <i>where</i> the collision happened, only that it did. Although I&#39;m sure it wouldn&#39;t be too hard to add that to the code.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (moon_rabbits)</author>
		<pubDate>Wed, 02 Apr 2008 06:10:37 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
For each dimension of each box check whether it&#39;s endpoints are contained on the line segment of the other box.
</p></div></div><p>

I don&#39;t understand how this works. It seems like there could be overlaps that would be missed.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Black)</author>
		<pubDate>Wed, 02 Apr 2008 07:48:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/c/f/cfe27da63a4bf88cd9f28a345496e176.png" alt="594871" width="165" height="98" /> <img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/f/b/fbf6c03675fe7e86dd482e7f53af8d01.png" alt="594872" width="225" height="98" /></p><p>If any endpoint of either line is within the boundaries of the other line then they have to be overlapping in that dimension. However , the algorithm moon_rabbits put up is much faster and just as accurate for checking overlaps. I always seem to take the long way &#39;round somehow.</p><p><s>Edit</s><br />Added picture where one line&#39;s endpoints both exceed the endpoints of the other line. Which both algorithms work for.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Wed, 02 Apr 2008 09:17:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>This is what I use:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">inline</span> <span class="k1">bool</span> collide<span class="k2">(</span><span class="k1">int</span> x1, <span class="k1">int</span> y1, <span class="k1">int</span> x2, <span class="k1">int</span> y2, <span class="k1">int</span> xx1, <span class="k1">int</span> yy1, <span class="k1">int</span> xx2, <span class="k1">int</span> yy2<span class="k2">)</span>
<span class="k2">{</span>
     <span class="k1">if</span> <span class="k2">(</span>x2 <span class="k3">&lt;</span><span class="k3">=</span> xx1<span class="k2">)</span> <span class="k1">return</span> <span class="k1">false</span><span class="k2">;</span>
     <span class="k1">else</span> <span class="k1">if</span> <span class="k2">(</span>x1 <span class="k3">&gt;</span><span class="k3">=</span> xx2<span class="k2">)</span> <span class="k1">return</span> <span class="k1">false</span><span class="k2">;</span>
     <span class="k1">else</span> <span class="k1">if</span> <span class="k2">(</span>y2 <span class="k3">&lt;</span><span class="k3">=</span> yy1<span class="k2">)</span> <span class="k1">return</span> <span class="k1">false</span><span class="k2">;</span>
     <span class="k1">else</span> <span class="k1">if</span> <span class="k2">(</span>y1 <span class="k3">&gt;</span><span class="k3">=</span> yy2<span class="k2">)</span> <span class="k1">return</span> <span class="k1">false</span><span class="k2">;</span>
     <span class="k1">else</span> <span class="k1">return</span> <span class="k1">true</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paul whoknows)</author>
		<pubDate>Wed, 02 Apr 2008 10:12:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
If any endpoint of either line is within the boundaries of the other line then they have to be overlapping in that dimension.
</p></div></div><p>

That&#39;s what I took your second method to mean, which I understood perfectly. The first one is the one I was having trouble with.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Black)</author>
		<pubDate>Wed, 02 Apr 2008 11:19:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Same thing really , just checking for it in different ways.<br />The second method takes one to two inequality checks per dimension and is just the inverse of what moon_rabbits and Paul whoknows posted while the first method has anywhere from 2 to 8 inequality checks per dimension which is wasteful.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Wed, 02 Apr 2008 12:27:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The way you worded the first one confused me. I get it now.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Black)</author>
		<pubDate>Wed, 02 Apr 2008 12:33:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code snippet"><div class="inner"><pre><span class="p">#define check_bb_collision(x1,y1,w1,h1,x2,y2,w2,h2) (!( ((x1)&gt;=(x2)+(w2)) || ((x2)&gt;=(x1)+(w1)) || ((y1)&gt;=(y2)+(h2)) </span>
<span class="k3">|</span><span class="k3">|</span> <span class="k2">(</span><span class="k2">(</span>y2<span class="k2">)</span><span class="k3">&gt;</span><span class="k3">=</span><span class="k2">(</span>y1<span class="k2">)</span><span class="k3">+</span><span class="k2">(</span>h1<span class="k2">)</span><span class="k2">)</span> <span class="k2">)</span><span class="k2">)</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Walker)</author>
		<pubDate>Wed, 02 Apr 2008 13:40:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Neil, that&#39;s nice! but which one is faster? yours or my &quot;inlined&quot; version? I am not sure <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paul whoknows)</author>
		<pubDate>Wed, 02 Apr 2008 22:19:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>There&#39;s only one way to find out... FIGHT!!!!</p><p>(apologies for the Harry Hill there, it just slipped out.)</p><p>But who knows, There probably isn&#39;t much difference (unless the compiler decides not to inline your function) between the inline and define, but I don&#39;t know how the compiler will optimise your comparison against mine, which is slightly different.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Walker)</author>
		<pubDate>Wed, 02 Apr 2008 22:53:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If you want a function that allows you to project the object out, you can use the <a href="http://www.allegro.cc/forums/thread/594774/723644#target">one I made for mrukki.</a> It has an okay explanation and may not be very efficient, but it works.</p><p>EDIT: Link fixed (Thanks Neil Black.)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (someone972)</author>
		<pubDate>Thu, 03 Apr 2008 04:01:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
(I don&#39;t know how to link to a post)
</p></div></div><p>

Click the part that says &quot;Posted on 04/02/2008 6:01 PM&quot; (or whatever the date and time are) on the post you want to line to. The url in your address bar will be a direct link to that post.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Black)</author>
		<pubDate>Thu, 03 Apr 2008 04:15:03 +0000</pubDate>
	</item>
</rss>
