In an javascript animation regarding Kepler's 2nd Law, problem with implementing changing velocity












0















I'm making an animation in javascript using HTML canvas to simulate Kepler's 2nd Law for a school Project. Here's a link to understand Kepler's Second Law. https://www.windows2universe.org/the_universe/uts/kepler2.html



Kepler's 2nd Law basically states that area of the triangles formed by two pairs of points in time are equal if the time difference between each point is equal.



Additionally, the velocity of the planet around the sun increases when it is closer and decreases when it is farther away. We can relate this by dividing some number by the distance from the sun squared or v = 50000/r^2.



For the animation, I'm using windowRequestAnimationFrame. To achieve the actual animation, I'm using a polar coordinate system. http://mathworld.wolfram.com/PolarCoordinates.html



I modulate the angle by multiplying it by the time object. Then I multiply it by the velocity.



Now on to the actual issue. To have a changing velocity which increases when the planet is closer to the sun and decreases when it is farther away, I need the polar angle to calculate it, however, I'm multiplying the velocity by this angle. How can update the velocity with the changing angle?



I feel as if there is a simple solution that I can't grasp. I have the link to my full code which is fully functional and can be run in a browser.
https://drive.google.com/file/d/1EoDgzoSVFDJ-hJMXuA47rBwjQ9vMQFbD/view?usp=sharing



Thank you for your consideration.



    // Initial Angle Values

var Ang1 = Math.PI;
var Ang2 = 2 * Math.PI;



// Initial Distances from the center of the ellipse

var distance = Distance(Ang1)
var distance2 = Distance(Ang2)

// X,Y coordinates derived from the distance using sin,cos functions

var line1_x = orbit.centerx +(distance * Math.cos(Ang1))
var line1_y = orbit.centery +(distance * Math.sin(Ang1))
var line3_x = orbit.centerx +(distance2 * Math.cos(Ang2))
var line3_y = orbit.centery + (distance2 * Math.sin(Ang2))


// Initial Distance from the sun using the distance formula
var focalDistance1 = coordDistance(line1_x,line1_y,sun_x,sun_y)
var focalDistance2 = coordDistance(line3_x,line3_y,sun_x,sun_y)


// Initial velocityies calculated using distance from the sun
var velocity1 = 50000/Math.pow(focalDistance1,2)
var velocity2 = 50000/Math.pow(focalDistance2,2)









// Multiplying the initial angles by time to animate and velocity to speed up
var orbitAngle = ((Ang1/60)*time.getSeconds() + (Ang1/60000)* time.getMilliseconds())* velocity1;
var orbitAngle3 = ((Ang2/60)*time.getSeconds() + (Ang2/60000)* time.getMilliseconds())* velocity2;

// The polar angle coords of the point ahead by the time interval, multiplying by time to animate and velocity to speed up
var orbitAngle2 = ( ((Ang1/60) * (time.getSeconds()+timeInterval)) + ((Ang1/60000)*(time.getMilliseconds()+timeInterval*1000))) * velocity1;
var orbitAngle4 = ( ((Ang2/60) * (time.getSeconds()+timeInterval)) + ((Ang2/60000)*(time.getMilliseconds()+timeInterval*1000))) * velocity2;








//Animating time interval positions using distance and angle
var distance = Distance(orbitAngle)
var distance2 = Distance(orbitAngle2)
var distance3 = Distance(orbitAngle3)

var distance4 = Distance(orbitAngle4)



var line1_x = orbit.centerx +(distance * Math.cos(orbitAngle))
var line1_y = orbit.centery +(distance * Math.sin(orbitAngle))
var line2_x = orbit.centerx +(distance2 * Math.cos(orbitAngle2))
var line2_y = orbit.centery + (distance2 * Math.sin(orbitAngle2))

var line3_x = orbit.centerx + (distance3 * Math.cos(orbitAngle3))
var line3_y = orbit.centery + (distance3 * Math.sin(orbitAngle3))
var line4_x = orbit.centerx +(distance4 * Math.cos(orbitAngle4))
var line4_y = orbit.centery + (distance4 * Math.sin(orbitAngle4))


var focalDistance1 = coordDistance(line1_x,line1_y,sun_x,sun_y);
var focalDistance2 = coordDistance(line3_x,line3_y,sun_x,sun_y);

var velocity1 = 50000/Math.pow(focalDistance1,2)
var velocity2 = 50000/Math.pow(focalDistance2,2)









share|improve this question



























    0















    I'm making an animation in javascript using HTML canvas to simulate Kepler's 2nd Law for a school Project. Here's a link to understand Kepler's Second Law. https://www.windows2universe.org/the_universe/uts/kepler2.html



    Kepler's 2nd Law basically states that area of the triangles formed by two pairs of points in time are equal if the time difference between each point is equal.



    Additionally, the velocity of the planet around the sun increases when it is closer and decreases when it is farther away. We can relate this by dividing some number by the distance from the sun squared or v = 50000/r^2.



    For the animation, I'm using windowRequestAnimationFrame. To achieve the actual animation, I'm using a polar coordinate system. http://mathworld.wolfram.com/PolarCoordinates.html



    I modulate the angle by multiplying it by the time object. Then I multiply it by the velocity.



    Now on to the actual issue. To have a changing velocity which increases when the planet is closer to the sun and decreases when it is farther away, I need the polar angle to calculate it, however, I'm multiplying the velocity by this angle. How can update the velocity with the changing angle?



    I feel as if there is a simple solution that I can't grasp. I have the link to my full code which is fully functional and can be run in a browser.
    https://drive.google.com/file/d/1EoDgzoSVFDJ-hJMXuA47rBwjQ9vMQFbD/view?usp=sharing



    Thank you for your consideration.



        // Initial Angle Values

    var Ang1 = Math.PI;
    var Ang2 = 2 * Math.PI;



    // Initial Distances from the center of the ellipse

    var distance = Distance(Ang1)
    var distance2 = Distance(Ang2)

    // X,Y coordinates derived from the distance using sin,cos functions

    var line1_x = orbit.centerx +(distance * Math.cos(Ang1))
    var line1_y = orbit.centery +(distance * Math.sin(Ang1))
    var line3_x = orbit.centerx +(distance2 * Math.cos(Ang2))
    var line3_y = orbit.centery + (distance2 * Math.sin(Ang2))


    // Initial Distance from the sun using the distance formula
    var focalDistance1 = coordDistance(line1_x,line1_y,sun_x,sun_y)
    var focalDistance2 = coordDistance(line3_x,line3_y,sun_x,sun_y)


    // Initial velocityies calculated using distance from the sun
    var velocity1 = 50000/Math.pow(focalDistance1,2)
    var velocity2 = 50000/Math.pow(focalDistance2,2)









    // Multiplying the initial angles by time to animate and velocity to speed up
    var orbitAngle = ((Ang1/60)*time.getSeconds() + (Ang1/60000)* time.getMilliseconds())* velocity1;
    var orbitAngle3 = ((Ang2/60)*time.getSeconds() + (Ang2/60000)* time.getMilliseconds())* velocity2;

    // The polar angle coords of the point ahead by the time interval, multiplying by time to animate and velocity to speed up
    var orbitAngle2 = ( ((Ang1/60) * (time.getSeconds()+timeInterval)) + ((Ang1/60000)*(time.getMilliseconds()+timeInterval*1000))) * velocity1;
    var orbitAngle4 = ( ((Ang2/60) * (time.getSeconds()+timeInterval)) + ((Ang2/60000)*(time.getMilliseconds()+timeInterval*1000))) * velocity2;








    //Animating time interval positions using distance and angle
    var distance = Distance(orbitAngle)
    var distance2 = Distance(orbitAngle2)
    var distance3 = Distance(orbitAngle3)

    var distance4 = Distance(orbitAngle4)



    var line1_x = orbit.centerx +(distance * Math.cos(orbitAngle))
    var line1_y = orbit.centery +(distance * Math.sin(orbitAngle))
    var line2_x = orbit.centerx +(distance2 * Math.cos(orbitAngle2))
    var line2_y = orbit.centery + (distance2 * Math.sin(orbitAngle2))

    var line3_x = orbit.centerx + (distance3 * Math.cos(orbitAngle3))
    var line3_y = orbit.centery + (distance3 * Math.sin(orbitAngle3))
    var line4_x = orbit.centerx +(distance4 * Math.cos(orbitAngle4))
    var line4_y = orbit.centery + (distance4 * Math.sin(orbitAngle4))


    var focalDistance1 = coordDistance(line1_x,line1_y,sun_x,sun_y);
    var focalDistance2 = coordDistance(line3_x,line3_y,sun_x,sun_y);

    var velocity1 = 50000/Math.pow(focalDistance1,2)
    var velocity2 = 50000/Math.pow(focalDistance2,2)









    share|improve this question

























      0












      0








      0








      I'm making an animation in javascript using HTML canvas to simulate Kepler's 2nd Law for a school Project. Here's a link to understand Kepler's Second Law. https://www.windows2universe.org/the_universe/uts/kepler2.html



      Kepler's 2nd Law basically states that area of the triangles formed by two pairs of points in time are equal if the time difference between each point is equal.



      Additionally, the velocity of the planet around the sun increases when it is closer and decreases when it is farther away. We can relate this by dividing some number by the distance from the sun squared or v = 50000/r^2.



      For the animation, I'm using windowRequestAnimationFrame. To achieve the actual animation, I'm using a polar coordinate system. http://mathworld.wolfram.com/PolarCoordinates.html



      I modulate the angle by multiplying it by the time object. Then I multiply it by the velocity.



      Now on to the actual issue. To have a changing velocity which increases when the planet is closer to the sun and decreases when it is farther away, I need the polar angle to calculate it, however, I'm multiplying the velocity by this angle. How can update the velocity with the changing angle?



      I feel as if there is a simple solution that I can't grasp. I have the link to my full code which is fully functional and can be run in a browser.
      https://drive.google.com/file/d/1EoDgzoSVFDJ-hJMXuA47rBwjQ9vMQFbD/view?usp=sharing



      Thank you for your consideration.



          // Initial Angle Values

      var Ang1 = Math.PI;
      var Ang2 = 2 * Math.PI;



      // Initial Distances from the center of the ellipse

      var distance = Distance(Ang1)
      var distance2 = Distance(Ang2)

      // X,Y coordinates derived from the distance using sin,cos functions

      var line1_x = orbit.centerx +(distance * Math.cos(Ang1))
      var line1_y = orbit.centery +(distance * Math.sin(Ang1))
      var line3_x = orbit.centerx +(distance2 * Math.cos(Ang2))
      var line3_y = orbit.centery + (distance2 * Math.sin(Ang2))


      // Initial Distance from the sun using the distance formula
      var focalDistance1 = coordDistance(line1_x,line1_y,sun_x,sun_y)
      var focalDistance2 = coordDistance(line3_x,line3_y,sun_x,sun_y)


      // Initial velocityies calculated using distance from the sun
      var velocity1 = 50000/Math.pow(focalDistance1,2)
      var velocity2 = 50000/Math.pow(focalDistance2,2)









      // Multiplying the initial angles by time to animate and velocity to speed up
      var orbitAngle = ((Ang1/60)*time.getSeconds() + (Ang1/60000)* time.getMilliseconds())* velocity1;
      var orbitAngle3 = ((Ang2/60)*time.getSeconds() + (Ang2/60000)* time.getMilliseconds())* velocity2;

      // The polar angle coords of the point ahead by the time interval, multiplying by time to animate and velocity to speed up
      var orbitAngle2 = ( ((Ang1/60) * (time.getSeconds()+timeInterval)) + ((Ang1/60000)*(time.getMilliseconds()+timeInterval*1000))) * velocity1;
      var orbitAngle4 = ( ((Ang2/60) * (time.getSeconds()+timeInterval)) + ((Ang2/60000)*(time.getMilliseconds()+timeInterval*1000))) * velocity2;








      //Animating time interval positions using distance and angle
      var distance = Distance(orbitAngle)
      var distance2 = Distance(orbitAngle2)
      var distance3 = Distance(orbitAngle3)

      var distance4 = Distance(orbitAngle4)



      var line1_x = orbit.centerx +(distance * Math.cos(orbitAngle))
      var line1_y = orbit.centery +(distance * Math.sin(orbitAngle))
      var line2_x = orbit.centerx +(distance2 * Math.cos(orbitAngle2))
      var line2_y = orbit.centery + (distance2 * Math.sin(orbitAngle2))

      var line3_x = orbit.centerx + (distance3 * Math.cos(orbitAngle3))
      var line3_y = orbit.centery + (distance3 * Math.sin(orbitAngle3))
      var line4_x = orbit.centerx +(distance4 * Math.cos(orbitAngle4))
      var line4_y = orbit.centery + (distance4 * Math.sin(orbitAngle4))


      var focalDistance1 = coordDistance(line1_x,line1_y,sun_x,sun_y);
      var focalDistance2 = coordDistance(line3_x,line3_y,sun_x,sun_y);

      var velocity1 = 50000/Math.pow(focalDistance1,2)
      var velocity2 = 50000/Math.pow(focalDistance2,2)









      share|improve this question














      I'm making an animation in javascript using HTML canvas to simulate Kepler's 2nd Law for a school Project. Here's a link to understand Kepler's Second Law. https://www.windows2universe.org/the_universe/uts/kepler2.html



      Kepler's 2nd Law basically states that area of the triangles formed by two pairs of points in time are equal if the time difference between each point is equal.



      Additionally, the velocity of the planet around the sun increases when it is closer and decreases when it is farther away. We can relate this by dividing some number by the distance from the sun squared or v = 50000/r^2.



      For the animation, I'm using windowRequestAnimationFrame. To achieve the actual animation, I'm using a polar coordinate system. http://mathworld.wolfram.com/PolarCoordinates.html



      I modulate the angle by multiplying it by the time object. Then I multiply it by the velocity.



      Now on to the actual issue. To have a changing velocity which increases when the planet is closer to the sun and decreases when it is farther away, I need the polar angle to calculate it, however, I'm multiplying the velocity by this angle. How can update the velocity with the changing angle?



      I feel as if there is a simple solution that I can't grasp. I have the link to my full code which is fully functional and can be run in a browser.
      https://drive.google.com/file/d/1EoDgzoSVFDJ-hJMXuA47rBwjQ9vMQFbD/view?usp=sharing



      Thank you for your consideration.



          // Initial Angle Values

      var Ang1 = Math.PI;
      var Ang2 = 2 * Math.PI;



      // Initial Distances from the center of the ellipse

      var distance = Distance(Ang1)
      var distance2 = Distance(Ang2)

      // X,Y coordinates derived from the distance using sin,cos functions

      var line1_x = orbit.centerx +(distance * Math.cos(Ang1))
      var line1_y = orbit.centery +(distance * Math.sin(Ang1))
      var line3_x = orbit.centerx +(distance2 * Math.cos(Ang2))
      var line3_y = orbit.centery + (distance2 * Math.sin(Ang2))


      // Initial Distance from the sun using the distance formula
      var focalDistance1 = coordDistance(line1_x,line1_y,sun_x,sun_y)
      var focalDistance2 = coordDistance(line3_x,line3_y,sun_x,sun_y)


      // Initial velocityies calculated using distance from the sun
      var velocity1 = 50000/Math.pow(focalDistance1,2)
      var velocity2 = 50000/Math.pow(focalDistance2,2)









      // Multiplying the initial angles by time to animate and velocity to speed up
      var orbitAngle = ((Ang1/60)*time.getSeconds() + (Ang1/60000)* time.getMilliseconds())* velocity1;
      var orbitAngle3 = ((Ang2/60)*time.getSeconds() + (Ang2/60000)* time.getMilliseconds())* velocity2;

      // The polar angle coords of the point ahead by the time interval, multiplying by time to animate and velocity to speed up
      var orbitAngle2 = ( ((Ang1/60) * (time.getSeconds()+timeInterval)) + ((Ang1/60000)*(time.getMilliseconds()+timeInterval*1000))) * velocity1;
      var orbitAngle4 = ( ((Ang2/60) * (time.getSeconds()+timeInterval)) + ((Ang2/60000)*(time.getMilliseconds()+timeInterval*1000))) * velocity2;








      //Animating time interval positions using distance and angle
      var distance = Distance(orbitAngle)
      var distance2 = Distance(orbitAngle2)
      var distance3 = Distance(orbitAngle3)

      var distance4 = Distance(orbitAngle4)



      var line1_x = orbit.centerx +(distance * Math.cos(orbitAngle))
      var line1_y = orbit.centery +(distance * Math.sin(orbitAngle))
      var line2_x = orbit.centerx +(distance2 * Math.cos(orbitAngle2))
      var line2_y = orbit.centery + (distance2 * Math.sin(orbitAngle2))

      var line3_x = orbit.centerx + (distance3 * Math.cos(orbitAngle3))
      var line3_y = orbit.centery + (distance3 * Math.sin(orbitAngle3))
      var line4_x = orbit.centerx +(distance4 * Math.cos(orbitAngle4))
      var line4_y = orbit.centery + (distance4 * Math.sin(orbitAngle4))


      var focalDistance1 = coordDistance(line1_x,line1_y,sun_x,sun_y);
      var focalDistance2 = coordDistance(line3_x,line3_y,sun_x,sun_y);

      var velocity1 = 50000/Math.pow(focalDistance1,2)
      var velocity2 = 50000/Math.pow(focalDistance2,2)






      javascript html canvas requestanimationframe






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 26 '18 at 5:21









      gh1234gh1234

      11




      11
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53475125%2fin-an-javascript-animation-regarding-keplers-2nd-law-problem-with-implementing%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53475125%2fin-an-javascript-animation-regarding-keplers-2nd-law-problem-with-implementing%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Create new schema in PostgreSQL using DBeaver

          Deepest pit of an array with Javascript: test on Codility

          Costa Masnaga