# Solar system with multiple planets

**URL:** <https://discourse.processing.org/t/solar-system-with-multiple-planets/25671>\
**Category:** Coding Questions\
**Created:** [November 24, 2020, 7:00pm UTC](https://discourse.processing.org/t/solar-system-with-multiple-planets/25671 "2020-11-24T19:00:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![N\_G](https://avatars.discourse-cdn.com/v4/letter/n/e47774/32.png) [@N\_G](https://discourse.processing.org/u/N_G)\
**Post date:** [November 24, 2020, 7:00pm UTC](https://discourse.processing.org/t/solar-system-with-multiple-planets/25671/1 "2020-11-24T19:00:56Z")

</div>

Hi,  
I’m just starting out with processing and I’m trying to build a little solar system. My problem is that I don’t know how to add another planet (like mars or venus).  
Because if I just ad the planet to my code it will rotate around the planet that came before.  
Here is my code:  
(It’s spanish luna=moon, tierra=earth, sol=sun, luz=light)

int a=200;  
int h=hour();  
PImage background;

PImage sol;  
PShape PSol;

PImage tierra;  
PShape PTierra;

PImage luna;  
PShape PLuna;

void setup() {  
size(1080, 2075, P3D);  
//background = loadImage(“fondoX.jpg”);  
[//background.resize](https://background.resize)(1080, 2075);

//setup sol  
sol=loadImage(“sol2k.jpg”);  
sol.resize(1080, 2075);  
PSol= createShape(SPHERE, 70);  
PSol.setTexture(sol);  
PSol.setStroke(false);

//setup tierra  
tierra=loadImage(“tierra3.jpg”);  
tierra.resize(1080, 2075);  
PTierra= createShape(SPHERE, 35);  
PTierra.setTexture(tierra);  
PTierra.setStroke(false);

//setup luna  
luna=loadImage(“luna2k.jpg”);  
luna.resize(1080, 2075);  
PLuna= createShape(SPHERE, 11);  
PLuna.setTexture(luna);  
PLuna.setStroke(false);  
}

void draw() {

//background(background);  
background(#020B22);  
noFill();  
noStroke();  
sphereDetail(10);

//sol  
translate(540, 1000, 0);  
rotateY(a \* 0.003);  
//rotateY(mouseX \* 0.007);  
shape(PSol);

//luz  
pointLight(255, 255, 255, 0, 0, 0);  
translate(0, 0, 300);  
//noLights();  
ambientLight(45, 65, 75);  
pointLight(255, 255, 255, 0, 0, -150);

//tierra  
translate(230, 0, 0);  
rotateY(a \* 0.02);  
// rotateY(mouseX \* 0.02);  
shape(PTierra);

//luna  
translate(90, 0, 0);  
shape(PLuna);

a=a+1;  
}

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 24, 2020, 8:47pm UTC](https://discourse.processing.org/t/solar-system-with-multiple-planets/25671/2 "2020-11-24T20:47:31Z")

</div>

you can isolate the movement (of the planets from each other) by using pushMatrix() and popMatrix() around each planet’s code section

```auto
//sol
pushMatrix(); // store a blank / fresh situation of the Matrix
translate(540, 1000, 0); // change Matrix
rotateY(a * 0.003);
//rotateY(mouseX * 0.007);
shape(PSol);
popMatrix(); // restore the fresh Matrix from before 

```

---

<div class="post-metadata">

**Author:** ![N\_G](https://avatars.discourse-cdn.com/v4/letter/n/e47774/32.png) [@N\_G](https://discourse.processing.org/u/N_G)\
**Post date:** [November 25, 2020, 8:21pm UTC](https://discourse.processing.org/t/solar-system-with-multiple-planets/25671/3 "2020-11-25T20:21:33Z")

</div>

Thanks!  
It’s working now and I added a few more planets.
