{"id":99,"date":"2010-06-12T14:56:49","date_gmt":"2010-06-12T09:26:49","guid":{"rendered":"http:\/\/techtwaddle.net\/?p=99"},"modified":"2011-01-31T22:19:20","modified_gmt":"2011-01-31T16:49:20","slug":"applications-the-mathematics-of-movement-part-3","status":"publish","type":"post","link":"https:\/\/techtwaddle.co.in\/blog\/2010\/06\/12\/applications-the-mathematics-of-movement-part-3\/","title":{"rendered":"Applications: The Mathematics of Movement, Part 3"},"content":{"rendered":"<p>Previously: <a href=\"http:\/\/geekswithblogs.net\/TechTwaddle\/archive\/2010\/05\/23\/applications-the-mathematics-of-movement-part-1.aspx\">Part 1<\/a>, <a href=\"http:\/\/geekswithblogs.net\/TechTwaddle\/archive\/2010\/06\/06\/applications-the-mathematics-of-movement-part-2.aspx\">Part 2<\/a><\/p>\n<p align=\"justify\">As promised in the <a href=\"http:\/\/geekswithblogs.net\/TechTwaddle\/archive\/2010\/06\/06\/applications-the-mathematics-of-movement-part-2.aspx\">previous post<\/a>, this post will cover two variations of the marble move program. The first one, Infinite Move, keeps the marble moving towards the click point, rebounding it off the screen edges and changing its direction when the user clicks again. The second version, Finite Move, is the same as first except that the marble does not move forever. It moves towards the click point, rebounds off the screen edges and slowly comes to rest. The amount of time that it moves depends on the distance between the click point and marble.<\/p>\n<p>\n<strong>Infinite Move<\/strong><\/p>\n<p align=\"justify\">This case is simple (actually both cases are simple). In this case all we need is the direction information which is exactly what the unit vector stores. So when the user clicks, you calculate the unit vector towards the click point and then keep updating the marbles position like crazy. And, of course, there is no stop condition. There&rsquo;s a little more additional code in the bounds checking conditions. Whenever the marble goes off the screen boundaries, we need to reverse its direction.&nbsp; Here is the code for mouse up event and UpdatePosition() method,<\/p>\n<p><font color=\"#000080\"><font color=\"#408080\">\/\/stores the unit vector       <br \/>\n<\/font>double unitX = 0, unitY = 0; <\/font><\/p>\n<p><font color=\"#000080\">double speed = 6; <\/font><\/p>\n<p><font color=\"#000080\"><font color=\"#408080\">\/\/speed times the unit vector       <br \/>\n<\/font>double incrX = 0, incrY = 0; <\/font><\/p>\n<p><font color=\"#000080\">private void Form1_MouseUp(object sender, MouseEventArgs e)     <br \/>\n{      <br \/>\n&nbsp;&nbsp;&nbsp; double x = e.X &#8211; marble1.x;      <br \/>\n&nbsp;&nbsp;&nbsp; double y = e.Y &#8211; marble1.y; <\/font><\/p>\n<p><font color=\"#000080\"><font color=\"#408080\">&nbsp;&nbsp;&nbsp; \/\/calculate distance between click point and current marble position       <br \/>\n<\/font>&nbsp;&nbsp;&nbsp; double lenSqrd = x * x + y * y;      <br \/>\n&nbsp;&nbsp;&nbsp; double len = Math.Sqrt(lenSqrd); <\/font><\/p>\n<p><font color=\"#000080\">&nbsp;<font color=\"#408080\">&nbsp;&nbsp; \/\/unit vector along the same direction (from marble towards click point)       <br \/>\n<\/font>&nbsp;&nbsp;&nbsp; unitX = x \/ len;      <br \/>\n&nbsp;&nbsp;&nbsp; unitY = y \/ len; <\/font><\/p>\n<p><font color=\"#000080\">&nbsp;&nbsp;&nbsp; timer1.Enabled = true;     <br \/>\n} <\/font><\/p>\n<p><font color=\"#000080\">private void UpdatePosition()     <br \/>\n{      <br \/>\n<\/font><font color=\"#000080\"><font color=\"#408080\">&nbsp;&nbsp;&nbsp; \/\/amount by which to increment marble position     <br \/>\n<\/font>&nbsp;&nbsp;&nbsp; incrX = speed * unitX;    <br \/>\n&nbsp;&nbsp;&nbsp; incrY = speed * unitY; <\/font><\/p>\n<p><font color=\"#000080\">&nbsp;&nbsp;&nbsp; marble1.x += incrX;     <br \/>\n&nbsp;&nbsp;&nbsp; marble1.y += incrY; <\/font><\/p>\n<p><font color=\"#000080\"><font color=\"#408080\">&nbsp;&nbsp;&nbsp; \/\/check for bounds       <br \/>\n<\/font>&nbsp;&nbsp;&nbsp; if ((int)marble1.x &lt; MinX + marbleWidth \/ 2)      <br \/>\n&nbsp;&nbsp;&nbsp; {      <br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; marble1.x = MinX + marbleWidth \/ 2;      <br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unitX *= -1;      <br \/>\n&nbsp;&nbsp;&nbsp; }      <br \/>\n&nbsp;&nbsp;&nbsp; else if ((int)marble1.x &gt; (MaxX &#8211; marbleWidth \/ 2))      <br \/>\n&nbsp;&nbsp;&nbsp; {      <br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; marble1.x = MaxX &#8211; marbleWidth \/ 2;      <br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unitX *= -1;      <br \/>\n&nbsp;&nbsp;&nbsp; } <\/font><\/p>\n<p><font color=\"#000080\">&nbsp;&nbsp;&nbsp; if ((int)marble1.y &lt; MinY + marbleHeight \/ 2)     <br \/>\n&nbsp;&nbsp;&nbsp; {      <br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; marble1.y = MinY + marbleHeight \/ 2;      <br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unitY *= -1;      <br \/>\n&nbsp;&nbsp;&nbsp; }      <br \/>\n&nbsp;&nbsp;&nbsp; else if ((int)marble1.y &gt; (MaxY &#8211; marbleHeight \/ 2))      <br \/>\n&nbsp;&nbsp;&nbsp; {      <br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; marble1.y = MaxY &#8211; marbleHeight \/ 2;      <br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unitY *= -1;      <br \/>\n&nbsp;&nbsp;&nbsp; } <\/font><\/p>\n<p><font color=\"#000080\">}<\/font><\/p>\n<p align=\"justify\">So whenever the user clicks we calculate the unit vector along that direction and also the amount by which the marble position needs to be incremented. The speed in this case is fixed at 6. You can experiment with different values. And under bounds checking, whenever the marble position goes out of bounds along the <strong>x<\/strong> or <strong>y<\/strong> direction we reverse the direction of the unit vector along that direction. Here&rsquo;s a video of it running;<\/p>\n<p><object width=\"425\" height=\"344\"><param value=\"http:\/\/www.youtube.com\/v\/Vf03EVbLHgI&amp;hl=en_US&amp;fs=1&amp;color1=0x3a3a3a&amp;color2=0x999999\" name=\"movie\" \/><param value=\"true\" name=\"allowFullScreen\" \/><param value=\"always\" name=\"allowscriptaccess\" \/><\/object><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Finite Move<\/strong><\/p>\n<p align=\"justify\">The code for finite move is almost exactly same as that of Infinite Move, except for the difference that the speed is not fixed and there is an end condition, so the marble comes to rest after a while. Code follows,<\/p>\n<p><font color=\"#000080\"><font color=\"#408080\">\/\/unit vector along the direction of click point       <br \/>\n<\/font>double unitX = 0, unitY = 0; <\/font><\/p>\n<p><font color=\"#000080\"><font color=\"#408080\">\/\/speed of the marble       <br \/>\n<\/font>double speed = 0; <\/font><\/p>\n<p><font color=\"#000080\">private void Form1_MouseUp(object sender, MouseEventArgs e)     <br \/>\n{      <br \/>\n&nbsp;&nbsp;&nbsp; double x = 0, y = 0;      <br \/>\n&nbsp;&nbsp;&nbsp; double lengthSqrd = 0, length = 0; <\/font><\/p>\n<p><font color=\"#000080\">&nbsp;&nbsp;&nbsp; x = e.X &#8211; marble1.x;     <br \/>\n&nbsp;&nbsp;&nbsp; y = e.Y &#8211; marble1.y;      <br \/>\n&nbsp;&nbsp;&nbsp; lengthSqrd = x * x + y * y; <\/font><\/p>\n<p><font color=\"#000080\"><font color=\"#408080\">&nbsp;&nbsp;&nbsp; \/\/length in pixels (between click point and current marble pos)       <br \/>\n<\/font>&nbsp;&nbsp;&nbsp; length = Math.Sqrt(lengthSqrd); <\/font><\/p>\n<p><font color=\"#000080\"><font color=\"#408080\">&nbsp;&nbsp;&nbsp; \/\/unit vector along the same direction as vector(x, y)       <br \/>\n<\/font>&nbsp;&nbsp;&nbsp; unitX = x \/ length;      <br \/>\n&nbsp;&nbsp;&nbsp; unitY = y \/ length; <\/font><\/p>\n<p><font color=\"#000080\">&nbsp;&nbsp;&nbsp; speed = length \/ 12; <\/font><\/p>\n<p><font color=\"#000080\">&nbsp;&nbsp;&nbsp; timer1.Enabled = true;     <br \/>\n} <\/font><\/p>\n<p><font color=\"#000080\">private void UpdatePosition()     <br \/>\n{      <br \/>\n&nbsp;&nbsp;&nbsp; marble1.x += speed * unitX;      <br \/>\n&nbsp;&nbsp;&nbsp; marble1.y += speed * unitY; <\/font><\/p>\n<p><font color=\"#000080\"><font color=\"#408080\">&nbsp;&nbsp;&nbsp; \/\/check for bounds       <br \/>\n<\/font>&nbsp;&nbsp;&nbsp; if ((int)marble1.x &lt; MinX + marbleWidth \/ 2)      <br \/>\n&nbsp;&nbsp;&nbsp; {      <br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; marble1.x = MinX + marbleWidth \/ 2;      <br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unitX *= -1;      <br \/>\n&nbsp;&nbsp;&nbsp; }      <br \/>\n&nbsp;&nbsp;&nbsp; else if ((int)marble1.x &gt; (MaxX &#8211; marbleWidth \/ 2))      <br \/>\n&nbsp;&nbsp;&nbsp; {      <br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; marble1.x = MaxX &#8211; marbleWidth \/ 2;      <br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unitX *= -1;      <br \/>\n&nbsp;&nbsp;&nbsp; } <\/font><\/p>\n<p><font color=\"#000080\">&nbsp;&nbsp;&nbsp; if ((int)marble1.y &lt; MinY + marbleHeight \/ 2)     <br \/>\n&nbsp;&nbsp;&nbsp; {      <br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; marble1.y = MinY + marbleHeight \/ 2;      <br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unitY *= -1;      <br \/>\n&nbsp;&nbsp;&nbsp; }      <br \/>\n&nbsp;&nbsp;&nbsp; else if ((int)marble1.y &gt; (MaxY &#8211; marbleHeight \/ 2))      <br \/>\n&nbsp;&nbsp;&nbsp; {      <br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; marble1.y = MaxY &#8211; marbleHeight \/ 2;      <br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unitY *= -1;      <br \/>\n&nbsp;&nbsp;&nbsp; } <\/font><\/p>\n<p><font color=\"#000080\"><font color=\"#408080\">&nbsp;&nbsp;&nbsp; \/\/reduce speed by 3% in every loop       <br \/>\n<\/font>&nbsp;&nbsp;&nbsp; speed = speed * 0.97f;      <br \/>\n&nbsp;&nbsp;&nbsp; if ((int)speed &lt;= 0)      <br \/>\n&nbsp;&nbsp;&nbsp; {      <br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; timer1.Enabled = false;      <br \/>\n&nbsp;&nbsp;&nbsp; } <\/font><\/p>\n<p><font color=\"#000080\">}<\/font><\/p>\n<p align=\"justify\">So the only difference is that the speed is calculated as a function of length when the mouse up event occurs. Again, this can be experimented with. Bounds checking is same as before. In the update and draw cycle, we reduce the speed by 3% in every cycle. Since <font color=\"#000080\">speed<\/font> is calculated as a function of <font color=\"#000080\">length<\/font>, <font color=\"#000080\">speed = length\/12<\/font>, the amount of time it takes <font color=\"#000080\">speed<\/font> to reach zero is directly proportional to <font color=\"#000080\">length<\/font>. Note that the speed is in &lsquo;pixels per 40ms&rsquo; because the timeout value of the timer is 40ms.&nbsp; The readability can be improved by representing speed in &lsquo;pixels per second&rsquo;. This would require you to add some more calculations to the code, which I leave out as an exercise. Here&rsquo;s a video of this second version,<\/p>\n<p><object width=\"425\" height=\"344\"><param value=\"http:\/\/www.youtube.com\/v\/is6iySs4D80&amp;hl=en_US&amp;fs=1&amp;color1=0x3a3a3a&amp;color2=0x999999\" name=\"movie\" \/><param value=\"true\" name=\"allowFullScreen\" \/><param value=\"always\" name=\"allowscriptaccess\" \/><\/object><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Previously: Part 1, Part 2 As promised in the previous post, this post will cover two variations of the marble move program. The first one, Infinite Move, keeps the marble moving towards the click point, rebounding it off the screen edges and changing its direction when the user clicks again. The second version, Finite Move, &hellip; <a href=\"https:\/\/techtwaddle.co.in\/blog\/2010\/06\/12\/applications-the-mathematics-of-movement-part-3\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Applications: The Mathematics of Movement, Part 3<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1ktFF-1B","_links":{"self":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/99"}],"collection":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/comments?post=99"}],"version-history":[{"count":2,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":137,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/99\/revisions\/137"}],"wp:attachment":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/media?parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/categories?post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/tags?post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}