<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Circular collision detection</title>
		<link>http://www.allegro.cc/forums/view/590321</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 15 Mar 2007 01:54:43 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m making an RPG with a type of battle system that has the character surrounded by a circle showing their attack radius.  How can I effectively check to see if another sprite is inside the circle?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Taiko Keiji)</author>
		<pubDate>Wed, 28 Feb 2007 22:36:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Basic pyth math...
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">if</span><span class="k2">(</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_618.html" target="_blank">pow</a><span class="k2">(</span>c_x-test_x,<span class="n">2</span><span class="k2">)</span> <span class="k3">+</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_618.html" target="_blank">pow</a><span class="k2">(</span>c_y-test_y,<span class="n">2</span><span class="k2">)</span> <span class="k3">&lt;</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_618.html" target="_blank">pow</a><span class="k2">(</span>radius,<span class="n">2</span><span class="k2">)</span> <span class="k2">)</span>
<span class="k2">{</span>
<span class="c">//inside</span>
<span class="k2">}</span> <span class="k1">else</span> <span class="k2">{</span>
<span class="c">//not inside</span>
<span class="k2">}</span>
</pre></div></div><p>
You don&#39;t actually need to use pow() - just multiply them with them selves, this was faster to write though <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jonatan Hedborg)</author>
		<pubDate>Wed, 28 Feb 2007 23:01:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>[edit] Yeah I&#39;m sleepy.  <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Onewing)</author>
		<pubDate>Wed, 28 Feb 2007 23:12:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
pow(c_x-test_x,2)
</p></div></div><p>

Argh! He asked how to do this effectively <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />.</p><p>You shouldn&#39;t use pow for integer powers, especially for the second power which is just one *. Well, the compiler could optimise it to * anyway, I guess... But that would be too much to count on.</p><p>[EDIT] Hm, did you edit, or is it my inability to read an entire post? <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /> Sorry either way <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jakub Wasilewski)</author>
		<pubDate>Wed, 28 Feb 2007 23:22:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You can do it pretty effectively with an octagon:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">if</span> <span class="k2">(</span><span class="k2">(</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_38.html" target="_blank">abs</a><span class="k2">(</span>x1-x2<span class="k2">)</span> <span class="k3">&lt;</span> d1<span class="k2">)</span><span class="k3">&amp;</span><span class="k3">&amp;</span><span class="k2">(</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_38.html" target="_blank">abs</a><span class="k2">(</span>y1-y2<span class="k2">)</span> <span class="k3">&lt;</span> d1<span class="k2">)</span><span class="k3">&amp;</span><span class="k3">&amp;</span><span class="k2">(</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_38.html" target="_blank">abs</a><span class="k2">(</span>x1-x2<span class="k2">)</span><span class="k3">+</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_38.html" target="_blank">abs</a><span class="k2">(</span>y1-y2<span class="k2">)</span> <span class="k3">&lt;</span> d2<span class="k2">)</span>
<span class="k1">return</span> <span class="k1">true</span><span class="k2">;</span>
</pre></div></div><p>

Just tweak the values of <tt>d1</tt> and <tt>d2</tt>, and you can make fast, effective, almost-circular collision detection. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kikaru)</author>
		<pubDate>Thu, 01 Mar 2007 00:12:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hm, that can actually be slower on new hardware, because of the inherent branching in abs. Four &quot;if&quot;s can be worse than three multiplications if branch prediction fails. Well, two multiplications, because you can store the attack radius squared as it&#39;s (I think) constant.:&#39;(
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jakub Wasilewski)</author>
		<pubDate>Thu, 01 Mar 2007 00:47:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code snippet"><div class="inner"><pre><span class="k1">const</span> <span class="k1">int</span> dx <span class="k3">=</span> c1_x <span class="k3">-</span> c2_x<span class="k2">;</span>
<span class="k1">const</span> <span class="k1">int</span> dy <span class="k3">=</span> c1_y <span class="k3">-</span> c2_y<span class="k2">;</span>
<span class="k1">const</span> <span class="k1">int</span> dist <span class="k3">=</span> c1_radius <span class="k3">+</span> c2_radius<span class="k2">;</span>

<span class="k1">if</span><span class="k2">(</span>dx <span class="k3">*</span> dx <span class="k3">+</span> dy <span class="k3">*</span> dy <span class="k3">&lt;</span> dist <span class="k3">*</span> dist<span class="k2">)</span>
  .. We have a collision<span class="k3">!</span> ..
</pre></div></div><p>
Circle collisions are the fastest of the collision detection schemes.  Don&#39;t use octagons.<br /><sub>

* fixed a typo having to do with squaring the radius(plural sp?) before adding them.
</sub>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Dustin Dettmer)</author>
		<pubDate>Thu, 01 Mar 2007 03:38:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>so c1_x would be for circle 1 and c2_x is for circle two?</p><p>I understand... sorta. <br />What if you want to do a collision veetween a circle and a rectangle?<br />would you just test the circle against the square?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Taiko Keiji)</author>
		<pubDate>Thu, 01 Mar 2007 05:03:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It&#39;s not really a &quot;collision&quot; test. More like a &quot;overlap&quot; test. But anyway...
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
would you just test the circle against the square?
</p></div></div><p>
Uhm, yeah. That&#39;s pretty much what you do. Only i have no idea how to do it <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" /><br />You probably check against the closest point of the square or test all lines. Dunno.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jonatan Hedborg)</author>
		<pubDate>Thu, 01 Mar 2007 05:20:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It&#39;s all the game designers fault.  I&#39;m gonna go curse him out now then continue trying to figure it out.:&#39;(
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Taiko Keiji)</author>
		<pubDate>Thu, 01 Mar 2007 05:27:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Hm, that can actually be slower on new hardware, because of the inherent branching in abs. Four &quot;if&quot;s can be worse than three multiplications if branch prediction fails. Well, two multiplications, because you can store the attack radius squared as it&#39;s (I think) constant.:&#39;(
</p></div></div><p>

Really?  Because abs can be written without any branches at all (provided your system has a CMP instruction)...</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="p">#include &lt;cmath&gt;</span></td></tr><tr><td class="number">2</td><td><span class="p">#include &lt;iostream&gt;</span></td></tr><tr><td class="number">3</td><td>&#160;</td></tr><tr><td class="number">4</td><td><span class="k1">using</span> <span class="k1">namespace</span> std<span class="k2">;</span></td></tr><tr><td class="number">5</td><td>&#160;</td></tr><tr><td class="number">6</td><td><span class="k1">namespace</span> <span class="k2">{</span></td></tr><tr><td class="number">7</td><td>  <span class="k1">template</span><span class="k3">&lt;</span><span class="k1">class</span> T&gt;</td></tr><tr><td class="number">8</td><td>  T myabs<span class="k2">(</span>T x<span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">9</td><td>    <span class="c">// !! makes sure this value is either zero or one for weird-behaving systems</span></td></tr><tr><td class="number">10</td><td>    <span class="k1">return</span> x <span class="k3">-</span> <span class="n">2</span> <span class="k3">*</span> <span class="k3">!</span><span class="k3">!</span><span class="k2">(</span>x <span class="k3">&lt;</span> <span class="n">0</span><span class="k2">)</span> <span class="k3">*</span> x<span class="k2">;</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="k2">}</span></td></tr><tr><td class="number">13</td><td>&#160;</td></tr><tr><td class="number">14</td><td><span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">15</td><td>  cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"abs(-31)    "</span> <span class="k3">&lt;</span><span class="k3">&lt;</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_38.html" target="_blank">abs</a><span class="k2">(</span><span class="k3">-</span><span class="n">31</span><span class="k2">)</span> <span class="k3">&lt;</span><span class="k3">&lt;</span> endl<span class="k2">;</span></td></tr><tr><td class="number">16</td><td>  cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"myabs(-31)  "</span> <span class="k3">&lt;</span><span class="k3">&lt;</span> myabs<span class="k2">(</span><span class="k3">-</span><span class="n">31</span><span class="k2">)</span> <span class="k3">&lt;</span><span class="k3">&lt;</span> endl<span class="k2">;</span></td></tr><tr><td class="number">17</td><td>  cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"abs(-3.1)   "</span> <span class="k3">&lt;</span><span class="k3">&lt;</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_38.html" target="_blank">abs</a><span class="k2">(</span><span class="k3">-</span><span class="n">3</span>.<span class="n">1</span><span class="k2">)</span> <span class="k3">&lt;</span><span class="k3">&lt;</span> endl<span class="k2">;</span></td></tr><tr><td class="number">18</td><td>  cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"myabs(-3.1) "</span> <span class="k3">&lt;</span><span class="k3">&lt;</span> myabs<span class="k2">(</span><span class="k3">-</span><span class="n">3</span>.<span class="n">1</span><span class="k2">)</span> <span class="k3">&lt;</span><span class="k3">&lt;</span> endl<span class="k2">;</span></td></tr><tr><td class="number">19</td><td>  <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">20</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>

Output
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.delorie.com/djgpp/doc/libc/libc_38.html" target="_blank">abs</a><span class="k2">(</span><span class="k3">-</span><span class="n">31</span><span class="k2">)</span>    <span class="n">31</span>
myabs<span class="k2">(</span><span class="k3">-</span><span class="n">31</span><span class="k2">)</span>  <span class="n">31</span>
<a href="http://www.delorie.com/djgpp/doc/libc/libc_38.html" target="_blank">abs</a><span class="k2">(</span><span class="k3">-</span><span class="n">3</span>.<span class="n">1</span><span class="k2">)</span>   <span class="n">3</span>.<span class="n">1</span>
myabs<span class="k2">(</span><span class="k3">-</span><span class="n">3</span>.<span class="n">1</span><span class="k2">)</span> <span class="n">3</span>.<span class="n">1</span>
</pre></div></div><p>

Whether or not that is more efficient is yet to be determined, though...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Carrus85)</author>
		<pubDate>Thu, 01 Mar 2007 06:17:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
provided your system has a CMP instruction
</p></div></div><p>

CMP usually doesn&#39;t work that way. It doesn&#39;t give you a 0 or 1 to store in a register and multiply by, it just sets some flags in a special flags register - which usually can&#39;t be the source/target of any computation. The conventional way to use the flags register is conditional jumps, aka branches.</p><p>But I admit, on modern architectures you can do it without branches in a multitude of ways. On newer x86, one way that immediately comes to mind is CMOV (however, CMOV might internally behave like a branch, with prediction, prediction misses and all). And for floating point, there is always FABS which is there from way back to the times of 8087 I think.</p><p>So, my bad. I didn&#39;t think it through properly (like the majority of my posts lately... something&#39;s wrong with me).</p><p>Anyway, there is no reason to sacrifice accuracy for an improbable speed-up (I don&#39;t think even FABS is faster than FMUL, but I might be wrong <i>again</i>), at least not until it becomes a bottleneck.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jakub Wasilewski)</author>
		<pubDate>Thu, 01 Mar 2007 07:33:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
CMP usually doesn&#39;t work that way. It doesn&#39;t give you a 0 or 1 to store in a register and multiply by, it just sets some flags in a special flags register - which usually can&#39;t be the source/target of any computation. The conventional way to use the flags register is conditional jumps, aka branches.
</p></div></div><p>

This is understood; although, it really depends on the system in question.  The point was that there are other ways to calculate abs that doesn&#39;t require branching, and therefore cannot suffer from branch prediction problems.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Carrus85)</author>
		<pubDate>Thu, 01 Mar 2007 08:56:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><a href="http://bob.allegronetwork.com/prog/tricks.html#abs">*cough*</a><br />Floats, as far as IEEE-whatever non-two&#39;s-compliment, as PCs are, can also be abs&#39;d by simply clearing the top bit (AFAIK).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Thu, 01 Mar 2007 09:08:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Circle collisions are the fastest of the collision detection schemes. Don&#39;t use octagons.
</p></div></div><p>
Depends. Octagons; no. But a bounding box before the actual circle may prove faster, especially since most box tests fail (so branch misprediction is not THAT big of a deal). I&#39;d say give it a try:
</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="k1">struct</span> SPHERE <span class="k2">{</span></td></tr><tr><td class="number">2</td><td>  <span class="k1">float</span> x<span class="k2">;</span></td></tr><tr><td class="number">3</td><td>  <span class="k1">float</span> y<span class="k2">;</span></td></tr><tr><td class="number">4</td><td>  <span class="k1">float</span> r<span class="k2">;</span></td></tr><tr><td class="number">5</td><td><span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">6</td><td>&#160;</td></tr><tr><td class="number">7</td><td><span class="p">#define noboxtest false; // set to true to benchmark without box test</span></td></tr><tr><td class="number">8</td><td><span class="k1">bool</span> boxtest<span class="k2">(</span><span class="k1">const</span> SPHERE<span class="k3">&amp;</span> a, <span class="k1">const</span> SPHERE<span class="k3">&amp;</span> b<span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">9</td><td>  <span class="k1">return</span> <span class="k2">(</span>a.x <span class="k3">+</span> a.r <span class="k3">&gt;</span> b.x <span class="k3">-</span> b.r<span class="k2">)</span> <span class="k3">&amp;</span><span class="k3">&amp;</span> <span class="k2">(</span>a.x <span class="k3">-</span> a.r <span class="k3">&lt;</span> b.x <span class="k3">+</span> b.r<span class="k2">)</span> <span class="k3">&amp;</span><span class="k3">&amp;</span></td></tr><tr><td class="number">10</td><td>         <span class="k2">(</span>a.y <span class="k3">+</span> a.r <span class="k3">&gt;</span> b.y <span class="k3">-</span> b.r<span class="k2">)</span> <span class="k3">&amp;</span><span class="k3">&amp;</span> <span class="k2">(</span>a.y <span class="k3">-</span> a.r <span class="k3">&lt;</span> b.y <span class="k3">+</span> b.r<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">11</td><td><span class="k2">}</span></td></tr><tr><td class="number">12</td><td>&#160;</td></tr><tr><td class="number">13</td><td><span class="k1">template</span><span class="k3">&lt;</span><span class="k1">class</span> T&gt; sqr<span class="k2">(</span>T a<span class="k2">)</span> <span class="k2">{</span> <span class="k1">return</span> a <span class="k3">*</span> a<span class="k2">;</span> <span class="k2">}</span></td></tr><tr><td class="number">14</td><td>&#160;</td></tr><tr><td class="number">15</td><td><span class="k1">bool</span> spheretest<span class="k2">(</span><span class="k1">const</span> SPHERE<span class="k3">&amp;</span> a, <span class="k1">const</span> SPHERE<span class="k3">&amp;</span> b<span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">16</td><td>  <span class="k1">return</span> sqr<span class="k2">(</span>a.x <span class="k3">-</span> b.x<span class="k2">)</span> <span class="k3">+</span> sqr<span class="k2">(</span>a.y <span class="k3">-</span> b.x<span class="k2">)</span> <span class="k3">&lt;</span> sqr<span class="k2">(</span>a.r <span class="k3">+</span> b.r<span class="k2">)</span><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="k1">bool</span> collision_test<span class="k2">(</span><span class="k1">const</span> SPHERE<span class="k3">&amp;</span> a, <span class="k1">const</span> SPHERE<span class="k3">&amp;</span> b<span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">20</td><td>  <span class="k1">if</span> <span class="k2">(</span>noboxtest <span class="k3">|</span><span class="k3">|</span> boxtest<span class="k2">(</span>a, b<span class="k2">)</span><span class="k2">)</span></td></tr><tr><td class="number">21</td><td>    <span class="k1">return</span> spheretest<span class="k2">(</span>a, b<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">22</td><td>  <span class="k1">return</span> <span class="k1">false</span><span class="k2">;</span></td></tr><tr><td class="number">23</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>
Now go and benchmark this thing, once with box test enabled and once without. See which one is faster with data that resembles what you expect.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Thu, 01 Mar 2007 17:45:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Collision detection is the hardest thing I&#39;ve done (using math) in programming to date. I don&#39;t know how to check collision with a circle, and all the code on this site is just a jumble of variables and numbers! When asked how to collide a circle with a rectangle the answer was to check collision with the closest part of the recatangle, but I don&#39;t even know how to find that! I&#39;m fairly good at math, but I haven&#39;t had the opportunity to learn any of this stuff yet. I&#39;M IGNORANT!!!!! Is there any website out there that goes over this kind of math in a simple, easy to understand way?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Black)</author>
		<pubDate>Mon, 05 Mar 2007 21:15:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>www.google.com
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (GullRaDriel)</author>
		<pubDate>Mon, 05 Mar 2007 22:00:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;d recommend buying a math textbook that is just past the last level you finished and reading.</p><p>Then repeat the operation (with higher-level textbooks) until you can fully understand what we&#39;re all talking about.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Dustin Dettmer)</author>
		<pubDate>Mon, 05 Mar 2007 22:29:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">bool</span> spheretest<span class="k2">(</span><span class="k1">const</span> SPHERE<span class="k3">&amp;</span> a, <span class="k1">const</span> SPHERE<span class="k3">&amp;</span> b<span class="k2">)</span> <span class="k2">{</span>
  <span class="k1">return</span> sqr<span class="k2">(</span>a.x <span class="k3">-</span> b.x<span class="k2">)</span> <span class="k3">+</span> sqr<span class="k2">(</span>a.y <span class="k3">-</span> b.x<span class="k2">)</span> <span class="k3">&lt;</span> sqr<span class="k2">(</span>a.r <span class="k3">+</span> b.r<span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
</p></div></div><p>

Don&#39;t use sqr() (as Dustin already pointed out above).</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">bool</span> spheretest<span class="k2">(</span><span class="k1">const</span> SPHERE<span class="k3">&amp;</span> a, <span class="k1">const</span> SPHERE<span class="k3">&amp;</span> b<span class="k2">)</span>
<span class="k2">{</span>
  <span class="k1">return</span> <span class="k2">(</span>a.x <span class="k3">-</span> b.x<span class="k2">)</span> <span class="k3">*</span> <span class="k2">(</span>a.x <span class="k3">-</span> b.x<span class="k2">)</span> <span class="k3">+</span> <span class="k2">(</span>a.y <span class="k3">-</span> b.x<span class="k2">)</span> <span class="k3">*</span> <span class="k2">(</span>a.y <span class="k3">-</span> b.x<span class="k2">)</span> <span class="k3">&lt;</span> <span class="k2">(</span>a.r <span class="k3">+</span> b.r<span class="k2">)</span> <span class="k3">*</span> <span class="k2">(</span>a.r <span class="k3">+</span> b.r<span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paul Pridham)</author>
		<pubDate>Tue, 06 Mar 2007 00:31:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Buy? A textbook? Those things are like, $40-60! I don&#39;t have that kind of money! I don&#39;t have any kind of money! I&#39;ve even lost all my Monopoly money!</p><p>As for using google, I&#39;m not even sure what this kind of math is called! Is it geometry or trigonometry? Or some wierd kind of quasipsychometry? Yes, I invented a word! Look, a whole post without a single period!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Black)</author>
		<pubDate>Tue, 06 Mar 2007 19:56:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
It&#39;s all the game designers fault
</p></div></div><p>
As programmer, you have the hardest job. Therefore, you should design the game to make it easier on yourself.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (James Stanley)</author>
		<pubDate>Tue, 06 Mar 2007 20:11:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Don&#39;t use sqr<b>t</b>() (as Dustin already pointed out above).
</p></div></div><p>

Fixed it for you. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /> Tobias&#39; example is perfectly fine.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Indeterminatus)</author>
		<pubDate>Tue, 06 Mar 2007 21:02:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Heh yea, noticed that afterwards... thanks (sorry Tobias!) <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paul Pridham)</author>
		<pubDate>Wed, 07 Mar 2007 07:58:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><pre>

Beginners, look for information on the Pythagoran theorem. 

In a right triangle (one with a 90 degree angle),
side lengths a, b, and c
(where c is the long side called the hypotenuse)

a^2 + b^2= c^2

1
|\
| \
|  \
|   \
|   c\
|a    \
|      \
|___b___\2

So if you can figure out the lengths of a and b, you know c.


This is actually the central concept in circular collision detection.
The distance from point 1 to point 2 is:

the square root of (x distance squared + y distance squared):
sqrt(              (2.x- 1.x)^2        + (2.x- 1.x)^2        )

</pre><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ben Delacob)</author>
		<pubDate>Sun, 11 Mar 2007 05:09:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Buy? A textbook? Those things are like, $40-60! I don&#39;t have that kind of money! I don&#39;t have any kind of money! I&#39;ve even lost all my Monopoly money!</p><p>As for using google, I&#39;m not even sure what this kind of math is called! Is it geometry or trigonometry? Or some wierd kind of quasipsychometry? Yes, I invented a word! Look, a whole post without a single period!
</p></div></div><p>
According to wikipedia, the Pythagorean Theorem is euclidean geometry.  In practice, I think I learned it in algebra class, though it was more important in trig class.  </p><p>There&#39;s lots of free resources on the web, but finding ones that are actually understandable, particularly for your level of knowledge, may take quite a bit of work.  I generally have the best results with programmer oriented sites and wikipedia.  Things that come up when I search for &quot;tutorial&quot; are also often useful.  A brief googling for online math textbook turned up a site with free PDFs of math textbooks (<a href="http://www.math.gatech.edu/~cain/textbooks/onlinebooks.html">http://www.math.gatech.edu/~cain/textbooks/onlinebooks.html</a>), but none looked appropriate for you.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (orz)</author>
		<pubDate>Sun, 11 Mar 2007 08:17:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Floats, as far as IEEE-whatever non-two&#39;s-compliment, as PCs are, can also be abs&#39;d by simply clearing the top bit (AFAIK).
</p></div></div><p>Apparently this isn&#39;t a good idea, because the processor&#39;s caching is separate for ints and floats, and whenever you reinterpret one, it has to wait ages flushing its cache.</p><p>For floats, &#39;fabs&#39; is probably implemented in the FPU and pretty fast. For ints, I tend to do this:
</p><div class="source-code snippet"><div class="inner"><pre><span class="c">//int y=abs(x);</span>
<span class="k1">int</span> y<span class="k3">=</span><span class="k2">(</span>x^<span class="k2">(</span><span class="k2">(</span><span class="k1">signed</span> <span class="k1">int</span><span class="k2">)</span>x&gt;&gt;31<span class="k2">)</span><span class="k2">)</span><span class="k3">+</span><span class="k2">(</span><span class="k2">(</span><span class="k1">unsigned</span> <span class="k1">int</span><span class="k2">)</span>x&gt;&gt;31<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
In two&#39;s complement arithmetic, you negate a number by twiddling all the bits and then adding one. In the above code, the XOR will twiddle all the bits if the top bit is set, and then add one if the top bit is set. It does rely on a few implementation-specific aspects of C though.</p><p>However, after all that, circle collision detection is probably going to be cheaper!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bruce Perry)</author>
		<pubDate>Sun, 11 Mar 2007 19:34:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Okay, I do know the Pythagoran theorem, but I don&#39;t know how to use it in programming, just the basic a^2 + b^2 = c^2. I&#39;m actually very good at math, if bored by it, once I&#39;ve had the chance to learn.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Black)</author>
		<pubDate>Tue, 13 Mar 2007 01:58:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Collision detection is the hardest thing I&#39;ve done (using math) in programming to date. I don&#39;t know how to check collision with a circle, and all the code on this site is just a jumble of variables and numbers! When asked how to collide a circle with a rectangle the answer was to check collision with the closest part of the recatangle, but I don&#39;t even know how to find that! I&#39;m fairly good at math, but I haven&#39;t had the opportunity to learn any of this stuff yet. I&#39;M IGNORANT!!!!! Is there any website out there that goes over this kind of math in a simple, easy to understand way?
</p></div></div><p>
Please cut down on the attitude a little. Just screaming that you&#39;re ignorant doesn&#39;t make it any likelier someone will really help you. I also recommend you read <a href="http://www.catb.org/~esr/faqs/smart-questions.html">this</a>.</p><p>OK, so if you know the pythagorean theorem, that&#39;s a start. You can use it to calculate the shortest distance between two points when given their carthesian coordinates (a.k.a. &quot;x&quot; and &quot;y&quot;): just think of c as the distance, and a and b as the x and y coordinates respectively. Solving for radius, this gives you:<br />sqrt(x ^ 2 + y ^ 2) = r<br />...which is, BTW, an equation for a circle about the origin.<br />Now to use this for sphere &lt;-&gt; sphere collision detection, you&#39;d simply check whether the sum of their radii is larger than the distance between their centers:<br />sqrt( (x2-x1) ^ 2 + (y2-y1) ^ 2 ) &lt; (r1 + r2)<br />Since squaring is cheaper than square-rooting, it is a good idea to use take the square of both sides of the equation (which is OK since we&#39;re not interested in negative results):<br />(x2-x1) ^ 2 + (y2-y1) ^ 2 &lt; r1 + r2<br />Since C doesn&#39;t come with a square operator, we need to use a function for that:
</p><div class="source-code snippet"><div class="inner"><pre>sqr<span class="k2">(</span>x2-x1<span class="k2">)</span> <span class="k3">+</span> sqr<span class="k2">(</span>y2-y1<span class="k2">)</span> <span class="k3">&lt;</span> r1 <span class="k3">+</span> r2
</pre></div></div><p>
Only problem is that C doesn&#39;t define a sqr() function either, but you can easily do that yourself. I already gave you the code.<br />The rectangle part is just about using bounding-box checks (basically, a series of 4 simple comparisons) BEFORE the above sphere test, to filter out the obvious non-collisions in a cheap way, avoiding most of the sphere tests (which involve expensive squaring).<br />In my example, I have used a C++ template for the sqr() function, so that it works with all data types that have a * operator.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Wed, 14 Mar 2007 15:09:37 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Wow, you just made that almost perfectly clear to me. What little I didn&#39;t get I&#39;ll get when I actually try to use it and have to think it through properly. Thank you Tobias Dammers!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Black)</author>
		<pubDate>Wed, 14 Mar 2007 17:55:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
(x2-x1) ^ 2 + (y2-y1) ^ 2 &lt; r1 + r2
</p></div></div><p>

Just a little correction (which was mentioned several times in this thread already, so it must&#39;ve slipped Tobias this time): If you decide to compare the squares, you have to square the entire thing, which means also the right side of the inequation <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p><tt>(x2-x1) ^ 2 + (y2-y1) ^ 2 &lt; (r1 + r2) ^ 2</tt>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Indeterminatus)</author>
		<pubDate>Wed, 14 Mar 2007 18:03:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I never would have caught that, so thanks to you too!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Black)</author>
		<pubDate>Thu, 15 Mar 2007 01:54:43 +0000</pubDate>
	</item>
</rss>

