Basic orbiting planets in P5.js












0












$begingroup$


I started studying Javascript about a week ago and I would like some opinions and advice on my code. This program just creates a canvas and instanciates 5 planets that orbit around a sun, giving them each a different speed, color, and size.



It uses the p5.js library, the code can be run in the p5.js web editor here: https://editor.p5js.org/ (You might want to expand the preview panel on the right after pasting the code, before you run the script.)



The code is is different files(class Orbiter, extra math functions and main code) but I'll put it here in one go:



// Converts from degrees to radians.
Math.radians = function(degrees) {
return degrees * Math.PI / 180;
};

// Converts from radians to degrees.
Math.degrees = function(radians) {
return radians * 180 / Math.PI;
};


class Orbiter {

constructor(rad, orbitAlt, x = 0, y = 0, orbitAngle = 0, orbitAngleMod = 1, colorR=255, colorG=255, colorB=255, colorA=255){
this.orbitAngle = orbitAngle; // Angle formed by the radius of the orbit and the x plane.
this.orbitAngleMod = orbitAngleMod; // Increment/decrement of orbitAngle
this.rad = rad; // Radius
this.orbitAlt = orbitAlt; // Distance to the orbited object's position (Alt for altitude)

// Position
this.x = x;
this.y = y;

// Color variables
this.colorR = colorR;
this.colorG = colorG;
this.colorB = colorB;
this.colorA = colorA;
}

orbit(object){
this.x = object.x + this.orbitAlt * cos(Math.radians(this.orbitAngle));
this.y = object.y + this.orbitAlt * sin(Math.radians(this.orbitAngle));
this.orbitAngle = this.orbitAngle + this.orbitAngleMod;

// Reset the angle to 0 after a complete revolution to avoid an ever increasing value.
if(this.orbitAngle >= 360){
this.orbitAngle = 0;
}
}

display(){
noStroke();
fill(this.colorR, this.colorG, this.colorB, this.colorA);
return ellipse(this.x, this.y, this.rad, this.rad);
}


}

let planets = ;
let sun = new Orbiter(100, 0);

function setup(){
createCanvas(windowWidth-3, windowHeight-3);
frameRate(144);

// Set up the Sun's colors and coordinates
sun.colorR = 255;
sun.colorG = 200;
sun.colorB = 0;
sun.x = windowWidth/2;
sun.y = windowHeight/2;

// Instantiate 5 planets
for(i = 0; i < 5; i++){
planets[i] = new Orbiter(5 + i * 15, 110 + i*70);
planets[i].orbitAngleMod= 1.4 - i/7;
planets[i].orbitAngle= i*5;

planets[i].colorR = i * 50 + 5;
planets[i].colorG = 255 - planets[i].colorR;
planets[i].colorB = 255 - planets[i].colorR;
}
}

function draw(){
background(0, 10, 40);

for(planet of planets){
planet.orbit(sun);
planet.display();
sun.display()
}

}


I'd be really grateful if you could give me some feedback on the structure of my code, whether it's in line with Javascript best practices, or in general if there's anything you see in there that you think is just wrong or should be written in a different way. Anything that you think could help me write better code: I don't know what I don't know!



Thanks in advance!










share|improve this question









$endgroup$

















    0












    $begingroup$


    I started studying Javascript about a week ago and I would like some opinions and advice on my code. This program just creates a canvas and instanciates 5 planets that orbit around a sun, giving them each a different speed, color, and size.



    It uses the p5.js library, the code can be run in the p5.js web editor here: https://editor.p5js.org/ (You might want to expand the preview panel on the right after pasting the code, before you run the script.)



    The code is is different files(class Orbiter, extra math functions and main code) but I'll put it here in one go:



    // Converts from degrees to radians.
    Math.radians = function(degrees) {
    return degrees * Math.PI / 180;
    };

    // Converts from radians to degrees.
    Math.degrees = function(radians) {
    return radians * 180 / Math.PI;
    };


    class Orbiter {

    constructor(rad, orbitAlt, x = 0, y = 0, orbitAngle = 0, orbitAngleMod = 1, colorR=255, colorG=255, colorB=255, colorA=255){
    this.orbitAngle = orbitAngle; // Angle formed by the radius of the orbit and the x plane.
    this.orbitAngleMod = orbitAngleMod; // Increment/decrement of orbitAngle
    this.rad = rad; // Radius
    this.orbitAlt = orbitAlt; // Distance to the orbited object's position (Alt for altitude)

    // Position
    this.x = x;
    this.y = y;

    // Color variables
    this.colorR = colorR;
    this.colorG = colorG;
    this.colorB = colorB;
    this.colorA = colorA;
    }

    orbit(object){
    this.x = object.x + this.orbitAlt * cos(Math.radians(this.orbitAngle));
    this.y = object.y + this.orbitAlt * sin(Math.radians(this.orbitAngle));
    this.orbitAngle = this.orbitAngle + this.orbitAngleMod;

    // Reset the angle to 0 after a complete revolution to avoid an ever increasing value.
    if(this.orbitAngle >= 360){
    this.orbitAngle = 0;
    }
    }

    display(){
    noStroke();
    fill(this.colorR, this.colorG, this.colorB, this.colorA);
    return ellipse(this.x, this.y, this.rad, this.rad);
    }


    }

    let planets = ;
    let sun = new Orbiter(100, 0);

    function setup(){
    createCanvas(windowWidth-3, windowHeight-3);
    frameRate(144);

    // Set up the Sun's colors and coordinates
    sun.colorR = 255;
    sun.colorG = 200;
    sun.colorB = 0;
    sun.x = windowWidth/2;
    sun.y = windowHeight/2;

    // Instantiate 5 planets
    for(i = 0; i < 5; i++){
    planets[i] = new Orbiter(5 + i * 15, 110 + i*70);
    planets[i].orbitAngleMod= 1.4 - i/7;
    planets[i].orbitAngle= i*5;

    planets[i].colorR = i * 50 + 5;
    planets[i].colorG = 255 - planets[i].colorR;
    planets[i].colorB = 255 - planets[i].colorR;
    }
    }

    function draw(){
    background(0, 10, 40);

    for(planet of planets){
    planet.orbit(sun);
    planet.display();
    sun.display()
    }

    }


    I'd be really grateful if you could give me some feedback on the structure of my code, whether it's in line with Javascript best practices, or in general if there's anything you see in there that you think is just wrong or should be written in a different way. Anything that you think could help me write better code: I don't know what I don't know!



    Thanks in advance!










    share|improve this question









    $endgroup$















      0












      0








      0





      $begingroup$


      I started studying Javascript about a week ago and I would like some opinions and advice on my code. This program just creates a canvas and instanciates 5 planets that orbit around a sun, giving them each a different speed, color, and size.



      It uses the p5.js library, the code can be run in the p5.js web editor here: https://editor.p5js.org/ (You might want to expand the preview panel on the right after pasting the code, before you run the script.)



      The code is is different files(class Orbiter, extra math functions and main code) but I'll put it here in one go:



      // Converts from degrees to radians.
      Math.radians = function(degrees) {
      return degrees * Math.PI / 180;
      };

      // Converts from radians to degrees.
      Math.degrees = function(radians) {
      return radians * 180 / Math.PI;
      };


      class Orbiter {

      constructor(rad, orbitAlt, x = 0, y = 0, orbitAngle = 0, orbitAngleMod = 1, colorR=255, colorG=255, colorB=255, colorA=255){
      this.orbitAngle = orbitAngle; // Angle formed by the radius of the orbit and the x plane.
      this.orbitAngleMod = orbitAngleMod; // Increment/decrement of orbitAngle
      this.rad = rad; // Radius
      this.orbitAlt = orbitAlt; // Distance to the orbited object's position (Alt for altitude)

      // Position
      this.x = x;
      this.y = y;

      // Color variables
      this.colorR = colorR;
      this.colorG = colorG;
      this.colorB = colorB;
      this.colorA = colorA;
      }

      orbit(object){
      this.x = object.x + this.orbitAlt * cos(Math.radians(this.orbitAngle));
      this.y = object.y + this.orbitAlt * sin(Math.radians(this.orbitAngle));
      this.orbitAngle = this.orbitAngle + this.orbitAngleMod;

      // Reset the angle to 0 after a complete revolution to avoid an ever increasing value.
      if(this.orbitAngle >= 360){
      this.orbitAngle = 0;
      }
      }

      display(){
      noStroke();
      fill(this.colorR, this.colorG, this.colorB, this.colorA);
      return ellipse(this.x, this.y, this.rad, this.rad);
      }


      }

      let planets = ;
      let sun = new Orbiter(100, 0);

      function setup(){
      createCanvas(windowWidth-3, windowHeight-3);
      frameRate(144);

      // Set up the Sun's colors and coordinates
      sun.colorR = 255;
      sun.colorG = 200;
      sun.colorB = 0;
      sun.x = windowWidth/2;
      sun.y = windowHeight/2;

      // Instantiate 5 planets
      for(i = 0; i < 5; i++){
      planets[i] = new Orbiter(5 + i * 15, 110 + i*70);
      planets[i].orbitAngleMod= 1.4 - i/7;
      planets[i].orbitAngle= i*5;

      planets[i].colorR = i * 50 + 5;
      planets[i].colorG = 255 - planets[i].colorR;
      planets[i].colorB = 255 - planets[i].colorR;
      }
      }

      function draw(){
      background(0, 10, 40);

      for(planet of planets){
      planet.orbit(sun);
      planet.display();
      sun.display()
      }

      }


      I'd be really grateful if you could give me some feedback on the structure of my code, whether it's in line with Javascript best practices, or in general if there's anything you see in there that you think is just wrong or should be written in a different way. Anything that you think could help me write better code: I don't know what I don't know!



      Thanks in advance!










      share|improve this question









      $endgroup$




      I started studying Javascript about a week ago and I would like some opinions and advice on my code. This program just creates a canvas and instanciates 5 planets that orbit around a sun, giving them each a different speed, color, and size.



      It uses the p5.js library, the code can be run in the p5.js web editor here: https://editor.p5js.org/ (You might want to expand the preview panel on the right after pasting the code, before you run the script.)



      The code is is different files(class Orbiter, extra math functions and main code) but I'll put it here in one go:



      // Converts from degrees to radians.
      Math.radians = function(degrees) {
      return degrees * Math.PI / 180;
      };

      // Converts from radians to degrees.
      Math.degrees = function(radians) {
      return radians * 180 / Math.PI;
      };


      class Orbiter {

      constructor(rad, orbitAlt, x = 0, y = 0, orbitAngle = 0, orbitAngleMod = 1, colorR=255, colorG=255, colorB=255, colorA=255){
      this.orbitAngle = orbitAngle; // Angle formed by the radius of the orbit and the x plane.
      this.orbitAngleMod = orbitAngleMod; // Increment/decrement of orbitAngle
      this.rad = rad; // Radius
      this.orbitAlt = orbitAlt; // Distance to the orbited object's position (Alt for altitude)

      // Position
      this.x = x;
      this.y = y;

      // Color variables
      this.colorR = colorR;
      this.colorG = colorG;
      this.colorB = colorB;
      this.colorA = colorA;
      }

      orbit(object){
      this.x = object.x + this.orbitAlt * cos(Math.radians(this.orbitAngle));
      this.y = object.y + this.orbitAlt * sin(Math.radians(this.orbitAngle));
      this.orbitAngle = this.orbitAngle + this.orbitAngleMod;

      // Reset the angle to 0 after a complete revolution to avoid an ever increasing value.
      if(this.orbitAngle >= 360){
      this.orbitAngle = 0;
      }
      }

      display(){
      noStroke();
      fill(this.colorR, this.colorG, this.colorB, this.colorA);
      return ellipse(this.x, this.y, this.rad, this.rad);
      }


      }

      let planets = ;
      let sun = new Orbiter(100, 0);

      function setup(){
      createCanvas(windowWidth-3, windowHeight-3);
      frameRate(144);

      // Set up the Sun's colors and coordinates
      sun.colorR = 255;
      sun.colorG = 200;
      sun.colorB = 0;
      sun.x = windowWidth/2;
      sun.y = windowHeight/2;

      // Instantiate 5 planets
      for(i = 0; i < 5; i++){
      planets[i] = new Orbiter(5 + i * 15, 110 + i*70);
      planets[i].orbitAngleMod= 1.4 - i/7;
      planets[i].orbitAngle= i*5;

      planets[i].colorR = i * 50 + 5;
      planets[i].colorG = 255 - planets[i].colorR;
      planets[i].colorB = 255 - planets[i].colorR;
      }
      }

      function draw(){
      background(0, 10, 40);

      for(planet of planets){
      planet.orbit(sun);
      planet.display();
      sun.display()
      }

      }


      I'd be really grateful if you could give me some feedback on the structure of my code, whether it's in line with Javascript best practices, or in general if there's anything you see in there that you think is just wrong or should be written in a different way. Anything that you think could help me write better code: I don't know what I don't know!



      Thanks in advance!







      javascript beginner object-oriented






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 14 mins ago









      ClepsydClepsyd

      286




      286






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          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: "196"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2fcodereview.stackexchange.com%2fquestions%2f211796%2fbasic-orbiting-planets-in-p5-js%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 Code Review Stack Exchange!


          • 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.


          Use MathJax to format equations. MathJax reference.


          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%2fcodereview.stackexchange.com%2fquestions%2f211796%2fbasic-orbiting-planets-in-p5-js%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

          How to make a Squid Proxy server?

          Is this a new Fibonacci Identity?

          19世紀