How to model the solar system or something similar

edited August 2016 in Questions about Code

I am wondering if it is possible to select a shape that is moving and create another shape that is binded to it and orbits it. I am attempting to make a simple flat solar system and having trouble getting moons to follow and track a sphere. Here is the code I am using at the moment. I would very much like to see how to add another small polygon to orbit the last made polygon. Or if there are any tutorials or videos dealing with parenting or something along those lines would also be helpful!

Thanks!

void setup() {
  background(0);
  size(1600, 800);
}

void draw() {
  if(mousePressed)
  background(0);

  pushMatrix();
  translate(width*0.5, height*0.5);
  rotate(frameCount / 50.0);
  polygon(0, 0, 80, 20);  // Icosahedron
  popMatrix();

  pushMatrix();
  translate(width*0.5, height*0.5);
  rotate(frameCount / 80.0);
  polygon(100, 100, 10, 10);  // Icosahedron
  popMatrix();

  pushMatrix();
  translate(width*.5, height*.5);
  rotate(frameCount / 30.0);
  polygon(70, 70, 5, 5);  // Icosahedron
  popMatrix();

}

void polygon(float x, float y, float radius, int npoints) {
  float angle = TWO_PI / npoints;
  beginShape();
  for (float a = 0; a < TWO_PI; a += angle) {
    float sx = x + cos(a) * radius;
    float sy = y + sin(a) * radius;
    vertex(sx, sy);
  }
  endShape(CLOSE);
}

Answers

  • Format you code. Edit your post, select code and hit ctrl+o.

    Kf

  • And please use a descriptive title for your query.

  • edited August 2016 Answer ✓

    Or if there are any tutorials or videos dealing with parenting or something along those lines would also be helpful!

    There probably are but they would probably use classes and recursion which you aren't ready for yet.

    Generally each parent is responsible for drawing its children. This sketch shows a 5 object system.

    PARENT  >>>>>>>  CHILDREN
    Sun  _________ Mars
             |____ Earth _______ Moon ____ Oribiter
    

    The code is deliberately verbose to help you see what is happening but also so you might add Venus yourself :)

    float sunX, sunY; // position on screen
    float marsDist, marsAng; // relativw to centre of the sun
    float earthDist, earthAng; // relativw to centre of the sun
    float moonDist, moonAng; // relative to centre of eartn
    float oribiterDist, orbiterAng; // relative to centre of moon
    
    void setup() {
      size(400, 400);
      marsDist = 160;
      marsAng = PI/4;
      earthDist = 120;
      earthAng = PI;
      moonDist = 22;
      moonAng = 0;
      oribiterDist = 8;
      orbiterAng = 0;
      noStroke();
    }
    
    void draw() {
      sunX = 170;  //just off display centre to 
      sunY = 170;  // that it works anywhere
      marsAng += 0.008;
      earthAng += 0.006;
      moonAng += 0.02;
      orbiterAng += 0.1;
      background(0);
      displaySystem();
    }
    
    void displaySystem() {
      // Draw the topmost parent object
      pushMatrix();
      translate(sunX, sunY);
      fill(200, 200, 0);
      ellipse(0, 0, 50, 50);
      // Draw its children
      displayMars();
      displayEarth();
      popMatrix();
    }
    
    void displayMars() {
      pushMatrix();
      translate(marsDist * cos(marsAng), marsDist * sin(marsAng));
      fill(220, 100, 0);
      ellipse(0, 0, 15, 15);
      popMatrix();
    }
    
    void displayEarth() {
      pushMatrix();
      translate(earthDist * cos(earthAng), earthDist * sin(earthAng));
      fill(0, 128, 0);
      ellipse(0, 0, 20, 20);
      // Draw its child
      displayMoon();
      popMatrix();
    }
    
    void displayMoon() {
      pushMatrix();
      translate(moonDist * cos(moonAng), moonDist * sin(moonAng));
      fill(160);
      ellipse(0, 0, 5, 5);
      // Draw its child
      displayOrbiter();
      popMatrix();
    }
    
    void displayOrbiter() {
      pushMatrix();
      translate(oribiterDist * cos(orbiterAng), oribiterDist * sin(orbiterAng));
      fill(255);
      ellipse(0, 0, 2, 2);
      popMatrix();
    }
    
  • BTW I have modified the topic title for you. I leave formatting the code to you :)

  • Possible - instead of using rotate you could use a for loop with cos and sin and calc the positions of the planet

    Store those

    And use them when calc the pos of the moon

    Maybe make a class skyObject or so to hold the data in an ArrayList

  • Thank you so much quark! I am going to be working to modify this a bit and see what I can do my next challenge is to attempt to map an image for each object rather than a color.

Sign In or Register to comment.