<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Line Collision / Shot Detection System</title>
		<link>http://www.allegro.cc/forums/view/612333</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Fri, 05 Apr 2013 18:37:33 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Greetings!</p><p>I&#39;m developing a Top-View 2D Multiplayer Shooter in C and I&#39;m having serious problems with the Shot Detection System.</p><p>I would really enjoy a major help concerning the whole problem that I&#39;ve posted in the stackoverflow website:</p><p><a href="http://stackoverflow.com/questions/15722996/c-and-allegro-how-to-detect-shots-in-a-top-view-2d-shooter">http://stackoverflow.com/questions/15722996/c-and-allegro-how-to-detect-shots-in-a-top-view-2d-shooter</a></p><p>The answer they gave to me involving atan2() from the C standard math library is mysterious to me and I&#39;m looking forward to expand my knowledge in Geometry and Mathematics to fully understand it.</p><p>Nevertheless, I can draw a line between the two players in this way:</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number"> 1</span><a href="http://www.allegro.cc/manual/al_draw_line"><span class="a">al_draw_line</span></a><span class="k2">(</span>defaultPlayer1.pos<span class="k2">[</span><span class="n">0</span><span class="k2">]</span>, defaultPlayer1.pos<span class="k2">[</span><span class="n">1</span><span class="k2">]</span>, defaultPlayer2.pos<span class="k2">[</span><span class="n">0</span><span class="k2">]</span>, defaultPlayer2.pos<span class="k2">[</span><span class="n">1</span><span class="k2">]</span>, <a href="http://www.allegro.cc/manual/al_map_rgb"><span class="a">al_map_rgb</span></a><span class="k2">(</span><span class="n">0</span>, <span class="n">255</span>, <span class="n">0</span><span class="k2">)</span>, <span class="n">2</span><span class="k2">)</span><span class="k2">;</span>
</div></div><p>

It would be rather easy if I could detect when the first button of the mouse is pressed OVER the line (When the weapon is above the line between the two players). In this way I could detect the shot. </p><p>But the problem with this idea is: al_draw_line() does not return anything, I can&#39;t find a way to test if something interacts with the line drawn. </p><p>Anyway, you all are experts in the Allegro library and I am just a programmer beginning to deal with game concepts and the Allegro library. It would be really really helpful if you could point me an example of a solution to this top-view problem.</p><p>Thank you very much!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (EricsonWillians)</author>
		<pubDate>Tue, 02 Apr 2013 13:09:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I guess you mean that the image rotates as you move your mouse sideways?  You&#39;ve already passed the angle in radians to al_draw_rotated_bitmap(), so you get the difference between player2 y and player1 y (to make a vector) to pass as the first argument to atan2(), then do the differences in x to get the second argument.  If the returned value (an angle in radians) is &quot;close enough&quot; to the angle you drew the sprite at, it collides.  The &quot;close enough&quot; part is to account for the size of the enemy (after all, he&#39;s not an infinitely small point).</p><p>The distance between the players can be gotten by the good old Pythagorian theorom to see if it&#39;s within range or not.  If you want to get fancy, you could associate this distance with the &quot;good enough&quot; value by various ways so the closer the enemy was, it&#39;d be easier to hit him, just like you&#39;d expect.</p><p>I wish I was articulate. <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" /></p><p>The guy who answered you on stackoverflow is <a href="https://www.allegro.cc/members/matthew.leverton">Matthew</a>, the guy who runs this site.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Arthur Kalliokoski)</author>
		<pubDate>Tue, 02 Apr 2013 14:20:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/612333/979588#target">EricsonWillians</a> said:</div><div class="quote"><p> It would be rather easy if I could detect when the first button of the mouse is pressed OVER the line (When the weapon is above the line between the two players). &gt;  In this way I could detect the shot. But the problem with this idea is: al_draw_line() does not return anything, I can&#39;t find a way to test if something interacts with the line drawn. </p></div></div><p>You should definitely read-up on arc-tangents (and therefore the <span class="source-code"><a href="http://www.delorie.com/djgpp/doc/libc/libc_50.html" target="_blank">atan2</a></span> function) and the angles they gave you in return. You aren&#39;t expected to do anything with returns of a <i>drawing</i> functions after all (except for operation status check of course).</p><p>Basically Matthew suggested to calculate 2 angles:</p><p> - The angle your main character is facing when Mouse button 1 is pressed (you SHOULD already have it, as AK already pointed out, since you&#39;re already drawing a rotated bitmap of the player.</p><p> - The angle (range) between the player and the target(s)</p><p>Comparing the two should get you your &quot;collision&quot;, and therefore your hit status.</p><p>Of course, as everybody already mentioned it, you HAVE to count in eventual obstacles, which involves checking the &quot;line&quot; you mention in your post from start to end.</p><p>And if an obiject is impenetrabel along this trajectory, you should stop checking and resolve the collision.</p><p>Try to sketch up a simple diagram on paper so you can visualize the trajectories and relative angles, I&#39;m sure it would help you a lot.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (pkrcel)</author>
		<pubDate>Tue, 02 Apr 2013 14:50:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>First of all, I thank you two for the excellent replies. I haven&#39;t completely understand it yet, but now I have stronger clues. </p><p>Yes, I have the angle when the left mouse button is pressed; as you&#39;ve said, I draw the rotated bitmap already. I do so here:</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number"> 1</span><a href="http://www.allegro.cc/manual/al_draw_rotated_bitmap"><span class="a">al_draw_rotated_bitmap</span></a><span class="k2">(</span>playerTux, defaultPlayer1.size <span class="k3">/</span> <span class="n">2</span>, defaultPlayer1.size <span class="k3">/</span> <span class="n">2</span>, 
<span class="number"> 2</span>defaultPlayer1.pos<span class="k2">[</span><span class="n">0</span><span class="k2">]</span>, defaultPlayer1.pos<span class="k2">[</span><span class="n">1</span><span class="k2">]</span>, mousePos <span class="k3">/</span> mouseSensitivity, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
</div></div><p>

For you to understand it more clearly, the angle in this case is a division of mousePos with a constant called mouseSensitivity (Giving the fact that it is a shooter game, I found a way to give to the user the option of choosing between 4 sensitivities.)</p><p>The rotation of the bitmap involves a curious solution that I&#39;ve found:</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="k1">if</span> <span class="k2">(</span>ev.type <span class="k3">=</span><span class="k3">=</span> ALLEGRO_EVENT_MOUSE_AXES<span class="k2">)</span> <span class="k2">{</span>
<span class="number">  2</span>        
<span class="number">  3</span><span class="c">// This says that the variable mousePos equals to the movement of the mouse</span>
<span class="number">  4</span><span class="c">// in the x axis, which is something necessary to change the angle of the image. </span>
<span class="number">  5</span>
<span class="number">  6</span>      mousePos <span class="k3">=</span> ev.mouse.x<span class="k2">;</span>
<span class="number">  7</span>      
<span class="number">  8</span>      
<span class="number">  9</span><span class="c">// I had several problems with the image stoping to rotate when the mouse</span>
<span class="number"> 10</span><span class="c">// goes off the screen area. So this basically warps the mouse cursor to the </span>
<span class="number"> 11</span><span class="c">// beginning of the screen when it goes off it, and vice-versa. To examine</span>
<span class="number"> 12</span><span class="c">// how this works, put a comment in the "al_hide_mouse_cursor()" up there.</span>
<span class="number"> 13</span>      
<span class="number"> 14</span>          <span class="k1">if</span> <span class="k2">(</span>mousePos <span class="k3">&gt;</span><span class="k3">=</span> screenWidth <span class="k3">-</span> <span class="n">1</span><span class="k2">)</span> <span class="k2">{</span>  <span class="c">// If the mouse cursor goes off the screen (right),</span>
<span class="number"> 15</span>            <a href="http://www.allegro.cc/manual/al_set_mouse_xy"><span class="a">al_set_mouse_xy</span></a><span class="k2">(</span>display, <span class="n">1</span>, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span> <span class="k2">}</span>  <span class="c">// warp it to the beginning of the screen.</span>
<span class="number"> 16</span>          <span class="k1">if</span> <span class="k2">(</span>mousePos <span class="k3">&lt;</span> <span class="n">1</span><span class="k2">)</span> <span class="k2">{</span>  <span class="c">// If the mouse cursor goes off the screen (left),</span>
<span class="number"> 17</span>            <a href="http://www.allegro.cc/manual/al_set_mouse_xy"><span class="a">al_set_mouse_xy</span></a><span class="k2">(</span>display, screenWidth <span class="k3">-</span> <span class="n">1</span>, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span> <span class="k2">}</span> <span class="k2">}</span>  <span class="c">// warp it to the end of the screen.</span>
</div></div><p>

This &quot;mouse cursor teleportation&quot; solved perfectly an old problem of mine (Maybe there&#39;s a clever way to solve this, but if it is, I don&#39;t know it yet).</p><p>The &quot;mouseSensitivity&quot; is declared together with the mousePos in this way:</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number"> 1</span><span class="k1">float</span> mousePos <span class="k3">=</span> <span class="n">0</span>.<span class="n">0</span><span class="k2">;</span>
<span class="number"> 2</span><span class="k1">float</span> mouseSensitivity <span class="k3">=</span> LOW_MOUSE_SENSITIVITY<span class="k2">;</span>
</div></div><p>

LOW_MOUSE_SENSITIVITY is a constant, defined together with these ones:</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number"> 1</span><span class="p">#define PLAYER_ROTATION_FACTOR      81.2</span>
<span class="number"> 2</span><span class="p">#define VERY_LOW_MOUSE_SENSITIVITY       PLAYER_ROTATION_FACTOR * 2</span>
<span class="number"> 3</span><span class="p">#define LOW_MOUSE_SENSITIVITY         PLAYER_ROTATION_FACTOR</span>
<span class="number"> 4</span><span class="p">#define MEDIUM_MOUSE_SENSITIVITY      PLAYER_ROTATION_FACTOR / 2</span>
<span class="number"> 5</span><span class="p">#define HIGH_MOUSE_SENSITIVITY          PLAYER_ROTATION_FACTOR / 2 / 2</span>
</div></div><p>

This &quot;rotation factor&quot; is a solution to a terrible problem. When I&#39;ve put the mouse.x as an angle argument to draw the rotated bitmap; even with the pivot placed properly, the bitmap didn&#39;t rotate completely (360), and then, by testing endlessly, I found that when I divided the mousePos with this value (81.2), the turn was complete (And therefore, mathematically speaking, I could double it&#39;s value to get a slower turn, but a still complete turn). I don&#39;t know if this all is clear enough, but it&#39;s working perfectly.</p><p>The thing is, thanks to this odd division, the angle does not go from 0 to 360; instead, it goes from 0.000000 to 6.299999. If I print this angle multiplied by something closer to 28 I can get the 360, but it&#39;s irrelevant. Now I&#39;ve come to my actual problem, and I&#39;m trying to apply this atan2() like this:</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number"> 1</span>  <span class="k1">double</span> playerPosArray1X<span class="k2">[</span><span class="n">2</span><span class="k2">]</span> <span class="k3">=</span> <span class="k2">{</span>defaultPlayer1.pos<span class="k2">[</span><span class="n">0</span><span class="k2">]</span>, defaultPlayer2.pos<span class="k2">[</span><span class="n">0</span><span class="k2">]</span><span class="k2">}</span><span class="k2">;</span>
<span class="number"> 2</span>  <span class="k1">double</span> playerPosArray1Y<span class="k2">[</span><span class="n">2</span><span class="k2">]</span> <span class="k3">=</span> <span class="k2">{</span>defaultPlayer1.pos<span class="k2">[</span><span class="n">1</span><span class="k2">]</span>, defaultPlayer2.pos<span class="k2">[</span><span class="n">1</span><span class="k2">]</span><span class="k2">}</span><span class="k2">;</span>
<span class="number"> 3</span>  <span class="k1">double</span> angleCalculation <span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_50.html" target="_blank">atan2</a><span class="k2">(</span>playerPosArray1Y, playerPosArray1X<span class="k2">)</span><span class="k2">;</span>
</div></div><p>

And then, inside the loop:</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number"> 1</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"Angle Calculation: %f\n"</span>, angleCalculation<span class="k2">)</span><span class="k2">;</span>
</div></div><p>

But I receive the following error in terminal:</p><pre class="terminal">main.c: In function ‘main’:
main.c:84:2: error: incompatible type for argument 1 of ‘atan2’
In file included from /usr/include/math.h:71:0,
                 from main.c:22:
/usr/include/x86_64-linux-gnu/bits/mathcalls.h:61:1: note: expected ‘double’ but argument is of type ‘double *’
main.c:84:2: error: incompatible type for argument 2 of ‘atan2’
In file included from /usr/include/math.h:71:0,
                 from main.c:22:
/usr/include/x86_64-linux-gnu/bits/mathcalls.h:61:1: note: expected ‘double’ but argument is of type ‘double *’
make: *** [main] Error 1</pre><p>

So, I can&#39;t pass an array as an argument of atan2(), and I&#39;m still quite lost. But I&#39;ll keep studying... After all, that&#39;s what game development is all about, right? Solving terrible problems in the most efficient way <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />.</p><p>Thank you very much again!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (EricsonWillians)</author>
		<pubDate>Wed, 03 Apr 2013 11:24:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/612333/979693#target">EricsonWillians</a> said:</div><div class="quote"><p> The thing is, thanks to this odd division, the angle does not go from 0 to 360; instead, it goes from 0.000000 to 6.299999. If I print this angle multiplied by something closer to 28 I can get the 360</p></div></div><p>It&#39;s giving you the angle in <a href="http://en.wikipedia.org/wiki/Radians">radians</a>, not degrees.  If you multiply that by 180.0/M_PI (which is close to 28) then you get degrees.  As I said before, you&#39;re passing radians to al_draw_bitmap(), but I guess you didn&#39;t know it.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p> expected ‘double’ but argument is of type ‘double *’</p></div></div><p>So put an asterisk in front, to dereference the pointers.
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">double</span> angleCalculation <span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_50.html" target="_blank">atan2</a><span class="k2">(</span><span class="k3">*</span>playerPosArray1Y, <span class="k3">*</span>playerPosArray1X<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Arthur Kalliokoski)</author>
		<pubDate>Wed, 03 Apr 2013 11:32:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/612333/979694#target">Arthur Kalliokoski</a> said:</div><div class="quote"><p> So put an asterisk in front, to dereference the pointers.</p></div></div><p>I wouldn&#39;t do that, he is passing dereferences <b>Array Pointers</b> (yeah blame me for this <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" /> ) to the atan2 function this way....while he SHOULD get the angle between the first two values, this is not formally correct, is it?</p><p>You should pass to <span class="source-code"><a href="http://www.delorie.com/djgpp/doc/libc/libc_50.html" target="_blank">atan2</a></span> only the vectors you want to calculate the angle in respect to the x-axis...in this case you should pass the DIFFERENCE between X and Y coords of the two objects.</p><p>Ericson, I encourage you once more to read up some math text about angles, tangents and the like...you basically have discovered the ratio between a circumference and his diameter..namely PI....but you should have known this beforehand  (!)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (pkrcel)</author>
		<pubDate>Wed, 03 Apr 2013 12:35:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I highly recommend that you brush up on your trigonometry and linear algebra, as these are heavily relied on in game development. Nonetheless, this is essentially what you want to do:</p><p>The problem is a variant of line-segment &amp; circle collision detection. Let&#39;s say you have two players in the following configuration (top-down 2D view):<br /><span class="remote-thumbnail"><span class="json">{"name":"607363","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/7\/e7ae43296c4fc9ac3aff7b59ca982dc8.png","w":512,"h":512,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/7\/e7ae43296c4fc9ac3aff7b59ca982dc8"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/e/7/e7ae43296c4fc9ac3aff7b59ca982dc8-240.jpg" alt="607363" width="240" height="240" /></span></p><p>Let&#39;s represent our players as circles (we want them to have some sort of area, it&#39;s much easier to shoot at a circle than it is to shoot at an infinitely small point). Player 1 is aiming toward player 2. Therefore, the player is rotated at some angle; let&#39;s call this angle <img class="math" src="http://www.allegro.cc/images/tex/2/5/2554a2bb846cffd697389e5dc8912759-96.png" alt="&lt;math&gt;\theta&lt;/math&gt;" />. We can also represent this angle&#39;s direction as a unit vector (that is a vector that strictly has a magnitude of 1). The unit vector (we&#39;ll call this <img class="math" src="http://www.allegro.cc/images/tex/9/e/9e3669d19b675bd57058fd4664205d2a-96.png" alt="&lt;math&gt;v&lt;/math&gt;" />) is composed of an x component and a y component, which can be derived from the angle we have:</p><p><img class="math" src="http://www.allegro.cc/images/tex/5/4/5400ce6af1699118519046de98258e20-96.png" alt="&lt;math&gt;x = \cos(\theta)&lt;/math&gt;" /><br /><img class="math" src="http://www.allegro.cc/images/tex/d/d/ddb9ca9f137220138067f4876d780719-96.png" alt="&lt;math&gt;y = \sin(\theta)&lt;/math&gt;" /></p><p>When player 1 shoots, the bullet will travel along this vector (direction).<br /><span class="remote-thumbnail"><span class="json">{"name":"607364","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/f\/8\/f8402053e091769c4088ac0a25db6cad.png","w":512,"h":512,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/f\/8\/f8402053e091769c4088ac0a25db6cad"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/f/8/f8402053e091769c4088ac0a25db6cad-240.jpg" alt="607364" width="240" height="240" /></span></p><p>We want the bullet to travel some given distance, or <b>range</b>. This is easily accomplished by multiplying the x and y components of our unit vector by our desired range.</p><p><img class="math" src="http://www.allegro.cc/images/tex/6/f/6f05e137fa4dbc2be8860d068081f667-96.png" alt="&lt;math&gt;range * v&lt;/math&gt;" /><br /><span class="remote-thumbnail"><span class="json">{"name":"607365","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/f\/a\/fab385dcbe0a8772e400a77907dca1d6.png","w":512,"h":512,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/f\/a\/fab385dcbe0a8772e400a77907dca1d6"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/f/a/fab385dcbe0a8772e400a77907dca1d6-240.jpg" alt="607365" width="240" height="240" /></span></p><p>We have all of the information to represent the line-segment that is the bullet&#39;s trajectory. A line-segment is simply composed of two points. The first point is player 1&#39;s position, and the second point is player 1&#39;s position <i>plus</i> our scaled unit vector. Now this becomes a <a href="http://doswa.com/2009/07/13/circle-segment-intersectioncollision.html">line-segment &amp; circle collision check</a>:<br /><span class="remote-thumbnail"><span class="json">{"name":"607366","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/6\/166b8e0d9c5497dde1e9566d8b71eb22.png","w":512,"h":512,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/6\/166b8e0d9c5497dde1e9566d8b71eb22"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/1/6/166b8e0d9c5497dde1e9566d8b71eb22-240.jpg" alt="607366" width="240" height="240" /></span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (atlasc1)</author>
		<pubDate>Wed, 03 Apr 2013 22:57:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It&#39;s worth mentioning that, when dealing with vectors, it&#39;s usually best not to resort to explicitly using trigonometric functions (e.g. sin,cos) unless you absolutely must. You will usually get a much cleaner and more performant solution if you stick to vectors.</p><p>It&#39;s actually extremely rare that you really need trig functions. For example, if you wanted to calculate whether the line between two players is within some angle of a players facing, you could achieve this using unit vectors and dot products. Instead of defining the tolerance (or field of view or spray or whatever you want to call it) in units of theta, you would need to define it in units of cos(theta). Which is fine - in actual fact it usually simplifies things a lot.</p><p>To illustrate what I mean, suppose you have two players: p1, p2.</p><p>Let D = (p2.position - p1.position)</p><p>Let v = D / |D|   (unit vector from p1 -&gt; p2)</p><p>Let u = p1.facing / |p1.facing|  (unit vector of p1&#39;s facing)</p><p>Now, testing whether p2 falls within the field of view of p1 is the following simple test:</p><p>  |u.v|  &gt; k   </p><p>k is a value between -1 and 1.  This is, in my opinion, much more meaningful than radians or degrees, but if you really must convert, then k = cos(theta).</p><p>Note how not once have I needed to use cos or sin explicitly, and I most certainly have not had to touch the filthy atan2!</p><p>Using atan2 is usually a sign that you&#39;ve gone wrong somewhere or, at the very least, there is a much cleaner way of doing it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (james_lohr)</author>
		<pubDate>Thu, 04 Apr 2013 02:31:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Interesting James, but</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/612333/979744#target">James Lohr</a> said:</div><div class="quote"><p> k is a value between -1 and 1.  This is, in my opinion, much more meaningful than radians or degrees, but if you really must convert, then k = cos(theta).</p></div></div><p>wouldn&#39;t that formally be <tt>u·v = cos(theta)</tt> and then you have to define a sort of kmin and kmax?</p><p>so that <tt>kmin &lt; u·v &lt; kmax</tt> defines you <i>spray</i> ?</p><p>(of course being this simmetric one can compare only the abs values but this is not really important)</p><p>And given that, why is it so wrong to rely on trigonometric functions for something simple as this?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (pkrcel)</author>
		<pubDate>Thu, 04 Apr 2013 11:51:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yeah, there was supposed to be an || around the u.v. Thanks for pointing this out pkrcel, I&#39;ve now fixed it.</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/612333/979773#target">pkrcel</a> said:</div><div class="quote"><p>And given that, why is it so wrong to rely on trigonometric functions for something simple as this?</p></div></div><p>

Sure, computing k from theta is no big deal, especially considering that it&#39;s likely to be a one-off calculation. However, I would still recommend sticking to k on principle, since it will reveal opportunities for simplifications when you start doing more complicated things later on.  As soon as you start talking in angles, you will invariably end up going back and forth between the two domains and introducing all sorts of unnecessary complexity along the way.</p><p>My point was more about not approaching the problem from an &quot;angles&quot; perspective in the first place. For example, one might be tempted to start with an atan2 to calculate the angle between p1 and p2, and then work from there. This is usually a bad idea. Sure, it does the job, but it is not mathematically elegant.</p><p>That said, this was just an example and not <i>that</i> big a deal. It&#39;s when you start relying upon trigonometric functions in your core collision code that it can become a <i>really</i> big deal, both in terms of performance and in terms of introducing unnecessary complexity.</p><p>Don&#39;t get me wrong, I&#39;m a massive fan of trig functions. I use them all over the place, particularly when slapping something together quickly. However, when I&#39;m solving a difficult problem or writing code that matters (either because it has the potential to be complex, or because it needs to be fast) I will make a real effort to avoid trig functions.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (james_lohr)</author>
		<pubDate>Thu, 04 Apr 2013 22:21:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/612333/979786#target">James Lohr</a> said:</div><div class="quote"><p> it can become a really big deal, both in terms of performance and in terms of introducing unnecessary complexity</p></div></div><p>I ask out of curiosity, but...vector math is not that simple compared to computing angles, and having to normalize most vectors you should be doing a good number of divisions, are the trig functions implementation so slow in these regards?</p><p>Note: of course I reckon converting back and forth between cartesian and polar domains is definitely sub-obtimal.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (pkrcel)</author>
		<pubDate>Fri, 05 Apr 2013 01:10:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/612333/979827#target">pkrcel</a> said:</div><div class="quote"><p>vector math is not that simple compared to computing angles</p></div></div><p>

It probably depends on the problem. Certainly in 2D, I would argue that in the majority of cases, vector math is more simple. </p><p>For example, show me the derivation, using trigonometry, for determining which side of a line-segment a point is on.   There is no reason even to enter the &quot;polar domain&quot;, but I have seen people do it and get into a horrible tangle.  </p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>and having to normalize most vectors you should be doing a good number of divisions, are the trig functions implementation so slow in these regards?</p></div></div><p>

Quite often you&#39;ll find there are tricks to avoid sqrts and divisions. Generally they are more forthcoming than in the equivalent trigonometric solution.</p><p>Though I suppose performance is probably not the strongest justification, as one could use lookup tables for either solution.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (james_lohr)</author>
		<pubDate>Fri, 05 Apr 2013 02:13:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/612333/979849#target">James Lohr</a> said:</div><div class="quote"><p> vector math is not that simple compared to computing angles</p><p> It probably depends on the problem. Certainly in 2D, I would argue that in the majority of cases, vector math is more simple. </p></div></div><p>Well.....I think that in 3D(+) vector math is a huge boon since you&#39;re mostly going matrices(tensors+)...diffcult to visualize but in the end more procedural.<br />Anyway it&#39;s not really my trade....</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p> For example, show me the derivation, using trigonometry, for determining which side of a line-segment a point is on.   There is no reason even to enter the &quot;polar domain&quot;, but I have seen people do it and get into a horrible tangle.  </p></div></div><p>I do not understand the question in the first place ( <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" /> ) ...you mean determine on which &quot;half&quot; of segment and arbitrary point is located? ...or you mean determine in which region of space the point lies in regards of the line?</p><p>Sorry for being dumb but I fail to see trigonometry in there but only coordinates check...</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p> Though I suppose performance is probably not the strongest justification, as one could use lookup tables for either solution.</p></div></div><p>I didn&#39;t hear something about that since my long gone days on late-age microcontrollers.... <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (pkrcel)</author>
		<pubDate>Fri, 05 Apr 2013 18:37:33 +0000</pubDate>
	</item>
</rss>
