Advertisement
Guest User

Initial velocity to move set distance with (de)acceleration

a guest
Mar 22nd, 2010
595
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 0.84 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
  5. </head>
  6. <body style="margin:0">
  7.     <div id="test" style="width:100px; height:100px; background:red"></div>
  8.     <script>
  9.    
  10.         var
  11.             uv = 0, // final velocity
  12.             d = 400, // distance
  13.             a = -2, // acceleration
  14.             iv = Math.sqrt( -2 * a * d ), // initialVelocity = sqrt( -2 * acceleration * distance );
  15.            
  16.             cv = iv, // current velocity
  17.             p = 0, // position
  18.            
  19.             f = function(){
  20.  
  21.                 var av = cv + (a / 2); // average velocity
  22.                 p += av;
  23.                 cv += a;
  24.            
  25.                 $('#test').css('margin-left', Math.round(p) );
  26.                 console.log(cv,p-d,$('#test').css('margin-left'));
  27.                 if(Math.abs(p-d)<.1) return clearInterval(loop);
  28.  
  29.             };
  30.        
  31.         loop = setInterval(f,30);
  32.    
  33.     </script>
  34. </body>
  35. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement