<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>moving character diagonally, instead of walking x direction first, then y...</title>
		<link>http://www.allegro.cc/forums/view/588108</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Fri, 20 Oct 2006 10:18:16 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hey guys,<br />I&#39;ve made more progress.  Little Link can now move to the spot where the user left clicks.  However, it&#39;s not the most sophisticated way.  As it stands, I compute how many units on the x axis he has to move, then how many y.  Then, I make him move, say +5 units on x axis, and then -5 on y.  So, Little Link walks to the right, then up.  </p><p>I would like to change this so that he moves diagonally to that point.  If user has left-clicked in northeast direction, I want him to move directly in that direction.  Hope I&#39;m being clear here.</p><p>In updatesprite I have this:
</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td>...</td></tr><tr><td class="number">2</td><td>..</td></tr><tr><td class="number">3</td><td>.</td></tr><tr><td class="number">4</td><td>        <span class="c">//Note: num2 = 3;</span></td></tr><tr><td class="number">5</td><td>&#160;</td></tr><tr><td class="number">6</td><td>        <span class="c">//northeast</span></td></tr><tr><td class="number">7</td><td>        <span class="k1">case</span> <span class="n">5</span><span class="k2">:</span></td></tr><tr><td class="number">8</td><td>            mario-&gt;x <span class="k3">+</span><span class="k3">=</span> num2<span class="k2">;</span></td></tr><tr><td class="number">9</td><td>            mario-&gt;y <span class="k3">-</span><span class="k3">=</span> num2<span class="k2">;</span></td></tr><tr><td class="number">10</td><td>            <span class="k1">break</span><span class="k2">;</span></td></tr><tr><td class="number">11</td><td>            </td></tr><tr><td class="number">12</td><td>        <span class="c">//souteast    </span></td></tr><tr><td class="number">13</td><td>        <span class="k1">case</span> <span class="n">6</span><span class="k2">:</span></td></tr><tr><td class="number">14</td><td>             mario-&gt;x <span class="k3">+</span><span class="k3">=</span> num2<span class="k2">;</span></td></tr><tr><td class="number">15</td><td>             mario-&gt;y <span class="k3">+</span><span class="k3">=</span> num2<span class="k2">;</span></td></tr><tr><td class="number">16</td><td>             <span class="k1">break</span><span class="k2">;</span></td></tr><tr><td class="number">17</td><td>             </td></tr><tr><td class="number">18</td><td>        <span class="c">//southwest</span></td></tr><tr><td class="number">19</td><td>        <span class="k1">case</span> <span class="n">7</span><span class="k2">:</span></td></tr><tr><td class="number">20</td><td>             mario-&gt;x <span class="k3">-</span><span class="k3">=</span> num2<span class="k2">;</span></td></tr><tr><td class="number">21</td><td>             mario-&gt;y <span class="k3">+</span><span class="k3">=</span> num2<span class="k2">;</span></td></tr><tr><td class="number">22</td><td>             <span class="k1">break</span><span class="k2">;</span></td></tr><tr><td class="number">23</td><td>        </td></tr><tr><td class="number">24</td><td>        <span class="c">//northwest</span></td></tr><tr><td class="number">25</td><td>        <span class="k1">case</span> <span class="n">8</span><span class="k2">:</span></td></tr><tr><td class="number">26</td><td>             mario-&gt;x <span class="k3">-</span><span class="k3">=</span> num2<span class="k2">;</span></td></tr><tr><td class="number">27</td><td>             mario-&gt;y <span class="k3">-</span><span class="k3">=</span> num2<span class="k2">;</span></td></tr><tr><td class="number">28</td><td>             <span class="k1">break</span><span class="k2">;</span></td></tr></tbody></table></div></div><p>

Part of calculate_position:
</p><div class="source-code snippet"><div class="inner"><pre>     <span class="k1">if</span><span class="k2">(</span>mario-&gt;x <span class="k3">&lt;</span> x<span class="k2">)</span>  <span class="c">//&amp;&amp; (mario-&gt;y &gt; y))</span>
     <span class="k2">{</span>          
          <span class="k1">if</span><span class="k2">(</span>mario-&gt;y <span class="k3">&gt;</span> y<span class="k2">)</span>
          <span class="k2">{</span>
                      <span class="c">//Link will move northeast</span>
                      ydir <span class="k3">=</span> <span class="n">5</span><span class="k2">;</span>
                      ynew <span class="k3">=</span> mario-&gt;y <span class="k3">-</span> y<span class="k2">;</span>                      
          <span class="k2">}</span>
...
..
.
</pre></div></div><p>

In while loop in main, if left-click is detected, do this:
</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td>...</td></tr><tr><td class="number">2</td><td>..</td></tr><tr><td class="number">3</td><td>.</td></tr><tr><td class="number">4</td><td>            <span class="k1">else</span> <span class="k1">if</span><span class="k2">(</span>mario-&gt;dir <span class="k3">=</span><span class="k3">=</span> <span class="n">5</span><span class="k2">)</span></td></tr><tr><td class="number">5</td><td>                        ynew <span class="k3">=</span> ynew<span class="k3">/</span><span class="n">3</span><span class="k2">;</span></td></tr><tr><td class="number">6</td><td>                                  </td></tr><tr><td class="number">7</td><td>                    <span class="k1">for</span><span class="k2">(</span>y1 <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> y1<span class="k3">&lt;</span>ynew<span class="k2">;</span> y1<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span></td></tr><tr><td class="number">8</td><td>                    <span class="k2">{</span></td></tr><tr><td class="number">9</td><td>                           erasesprite<span class="k2">(</span><a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>, mario<span class="k2">)</span><span class="k2">;</span>            </td></tr><tr><td class="number">10</td><td>                          updatesprite<span class="k2">(</span>mario<span class="k2">)</span><span class="k2">;</span> </td></tr><tr><td class="number">11</td><td>                          move_character<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">12</td><td>                          <a href="http://www.allegro.cc/manual/acquire_screen" target="_blank"><span class="a">acquire_screen</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">13</td><td>                          draw_character_mario<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span> </td></tr><tr><td class="number">14</td><td>                      <a href="http://www.allegro.cc/manual/release_screen" target="_blank"><span class="a">release_screen</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">15</td><td>                      <a href="http://www.allegro.cc/manual/rest" target="_blank"><span class="a">rest</span></a><span class="k2">(</span><span class="n">50</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">16</td><td>                    <span class="k2">}</span></td></tr></tbody></table></div></div><p>

OK, so variable names might not make sense since I&#39;m changing code, i.e. Mario should be Link, ynew will be changed to new_dir if I can make this diagonal thing work.</p><p>If I run this code, Link walks a perfect diagonal line, even though the cursor might be might be ten units to the left.</p><p>Make sense?  Hope so; if not, ask away.  Or just give me general advice at least <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (red-dragon)</author>
		<pubDate>Thu, 19 Oct 2006 04:06:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Try vector movement. Do a forums search for vector movement.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Rick)</author>
		<pubDate>Thu, 19 Oct 2006 04:19:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Simple trig should give you the direction angle, from there you can calculate deltax and deltay.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Steve Terry)</author>
		<pubDate>Thu, 19 Oct 2006 04:22:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code snippet"><div class="inner"><pre><span class="k1">double</span> x, y<span class="k2">;</span>
<span class="k1">double</span> direction, speed<span class="k2">;</span>
...
x <span class="k3">+</span><span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_728.html" target="_blank">sin</a><span class="k2">(</span>direction<span class="k2">)</span> <span class="k3">*</span> speed<span class="k2">;</span>
y <span class="k3">+</span><span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_113.html" target="_blank">cos</a><span class="k2">(</span>direction<span class="k2">)</span> <span class="k3">*</span> speed<span class="k2">;</span>
</pre></div></div><p>
direction ranges 0 to (2 * M_PI)<br />speed ranges 0 to some big number (probably less then 5 or so)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Thu, 19 Oct 2006 05:02:55 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>hey Steve, it&#39;s been a long time for me since I did simple trig.</p><p>Dustin, thanks for the help.  Though for the sin, cos, and M_PI (that&#39;s pi, right, ha?), is there a library I need to include?  If I&#39;m missing the point, please elaborate.  Really appreciate your help, thanks.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (red-dragon)</author>
		<pubDate>Thu, 19 Oct 2006 09:14:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Use
</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#include &lt;cmath&gt;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Thu, 19 Oct 2006 09:52:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Dustin,<br />I&#39;ve put this code in, but it&#39;s not working properly.  At best, when I clicked northeast, Link walked straight then a little curve.  Weird.</p><p>So, here come the questions:<br />double x, y: this is Link&#39;s x, y coords, right?</p><p>&quot;direction rangest 0 to (2 * M_PI)&quot;... I&#39;m a bit lost here.  In my code I have the normal 8 directions.  </p><p>Sorry if I seem a bit dense.  Explain away.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (red-dragon)</author>
		<pubDate>Thu, 19 Oct 2006 22:48:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>sin and cos return a number ranging 0 to 1<br />sin and cos take an input value ranging 0 to (2 * M_PI). *<br />If you input 0 for sin you get 1 and cos you get 0.  If you imagine the value of 0 as &#39;going right&#39;, this is correct.  The x should increment and the y should not.<br />If you input (M_PI / 2) sin will give you 0 and cos will give you 1.  Remember we&#39;re incrementing x by sin and y by cos.  So this means &#39;go up&#39;.<br />If you keep going in increments of (M_PI / 4), you&#39;ll head in four straight forward directions, right, up, left, and down.</p><p>The beauty of this system is that it takes care of all the in-between values, so (M_PI / 4) will go up and right.</p><p>Be careful though, cause the screen is actually upside down (ie quadrant 4).  Up might easily come out as down.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
&quot;direction rangest 0 to (2 * M_PI)&quot;... I&#39;m a bit lost here. In my code I have the normal 8 directions.
</p></div></div><p>
ok so if directions is a value ranging 0 to 8, you can pass it into sin and cos like so:</p><p>x += sin(directions * (2 * M_PI) / 8) * <i>some_value</i>;<br />y += cos(directions * (2 * M_PI) / 8) * <i>some_valie</i>;</p><p>[disclaimer]
</p><ul><li><p>you can actually pass values above (2 * M_PI) and below 0 and sin / cos will be just fine with it, but its easier conceptually to pretend they can only be in that range.</p></li></ul><p>[edit]<br />I mixed up the return values for sin / cos, they&#39;re wrong.</p><p>Owell, hopefully this will clarify why you do that anyway.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Thu, 19 Oct 2006 23:20:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Can you post your code? or perhaps a binary? so we can see what you are doing.<br />I am not sure what you really need, but if I am not wrong, perhaps the <a href="http://www.purplemath.com/modules/distform.htm">Distance formula between 2 points</a> is what you need.<br />Like I said, post your code or a binary file! that will help a lot!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paul whoknows)</author>
		<pubDate>Thu, 19 Oct 2006 23:25:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Can you post your code? or perhaps a binary? so we can see what you are doing.<br />I am not sure what you really need, but if I am not wrong, perhaps the Distance formula between 2 points [purplemath.com] is what you need.<br />Like I said, post your code or a binary file! that will help a lot!
</p></div></div><p>
I believe he wants to move his character by an arbitrary angle, which the distance formula cant do.</p><p>Distance formula is usefull for spherical collision detection.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Thu, 19 Oct 2006 23:28:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>sorry!, I meant the secant line between 2 points, using it little link would walk in a straight line from his current position to where the user left clicks.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paul whoknows)</author>
		<pubDate>Fri, 20 Oct 2006 00:06:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ah yes, it would be usefull for that too <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Fri, 20 Oct 2006 01:57:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Paul,<br />are you talking about the d = sqrt(((x2-x1)^2) + ((y2-y1)^2)) formula?</p><p>ok, so that gives me d.... where would I go from here?  ...what do I do with it?  how will little ol&#39; link walk diagonally?  </p><p>I have code right here:  <a href="http://www.allegro.cc/files/attachment/590335">code</a><br />It might not compile right now, but only kuz I&#39;m cleaning code up a bit.  He moves in x, then y.  You&#39;ll see.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (red-dragon)</author>
		<pubDate>Fri, 20 Oct 2006 02:30:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>double x, y;<br />double direction, speed;<br />...<br />x += sin(direction) * speed;<br />y += cos(direction) * speed;</p><p>Here all you need to determine is direction which you can get in radians if you use the nice little function atan2.</p><p>#include &lt;math.h&gt;<br />double direction = atan2(mouse_y - characterY, mouse_x - characerX);</p><p>Hope that helps, basically atan2 will return in radians the angle between two given points.  Of course give a good speed variable, something less than 1.0 that will move your character smoothly between the two points.  Now you have an angle in radians you can also convert that to a fixed point degree angle to pass into rotate_sprite so your character rotates at that angle as well.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Steve Terry)</author>
		<pubDate>Fri, 20 Oct 2006 03:45:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
d = sqrt(((x2-x1)^2) + ((y2-y1)^2)) formula?
</p></div></div><p>
Instead, you should use this code:
</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#ifdef MSVC</span>
<span class="p">#define hypot _hypot</span>
<span class="p">#endif</span>
...
d <span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_466.html" target="_blank">hypot</a><span class="k2">(</span>x2 <span class="k3">-</span> x1, y2 <span class="k3">-</span> y1<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
Some CPUs may implement hypot as an instruction which would be way faster then the 6 or so instructions of the pre-mentioned method.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Fri, 20 Oct 2006 07:43:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Paul,<br />are you talking about the d = sqrt(((x2-x1)^2) + ((y2-y1)^2)) formula?
</p></div></div><p>

No no, I meant the secant line between 2 points<br /> </p><p><span class="remote-thumbnail"><span class="json">{"name":"96215157b42adf1a03b4da860ca80770.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/e\/de5da42bd94caec2d100fcc40dcfb8ef.png","w":261,"h":39,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/e\/de5da42bd94caec2d100fcc40dcfb8ef"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/d/e/de5da42bd94caec2d100fcc40dcfb8ef-240.jpg" alt="96215157b42adf1a03b4da860ca80770.png" width="240" height="35" /></span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paul whoknows)</author>
		<pubDate>Fri, 20 Oct 2006 10:18:16 +0000</pubDate>
	</item>
</rss>
