# Active Processing program to simulate a planet orbiting around a sun

**URL:** <https://discourse.processing.org/t/active-processing-program-to-simulate-a-planet-orbiting-around-a-sun/30462>\
**Category:** Coding Questions\
**Created:** [June 3, 2021, 5:00am UTC](https://discourse.processing.org/t/active-processing-program-to-simulate-a-planet-orbiting-around-a-sun/30462 "2021-06-03T05:00:26Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![matto](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@matto](https://discourse.processing.org/u/matto)\
**Post date:** [June 3, 2021, 5:00am UTC](https://discourse.processing.org/t/active-processing-program-to-simulate-a-planet-orbiting-around-a-sun/30462/1 "2021-06-03T05:00:26Z")

</div>

Am trying to write a code to make my planet increase and decrease with respect to PI/2 and 3\*PI/2 as it orbits round the sun please any tips

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [June 3, 2021, 5:29am UTC](https://discourse.processing.org/t/active-processing-program-to-simulate-a-planet-orbiting-around-a-sun/30462/2 "2021-06-03T05:29:33Z")

</div>

What have you got so far?

Seriously. Post your code.

Without knowing what you already have working, we are at a total loss as to how to help you.

Are you trying to increase the radius of the planets? Are you trying to have elliptical orbits? I have no idea what you have already.

Post your code. Tell us what works, and what doesn’t.

---

<div class="post-metadata">

**Author:** ![KumuPaul](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kumupaul/32/13072_2.png) [@KumuPaul](https://discourse.processing.org/u/KumuPaul)\
**Post date:** [June 3, 2021, 6:44am UTC](https://discourse.processing.org/t/active-processing-program-to-simulate-a-planet-orbiting-around-a-sun/30462/3 "2021-06-03T06:44:55Z")

</div>

Orbital mechanics are great fun!

I’m not sure about your reference to `PI / 2` and `3 * PI / 2`. How does this relate to orbits?

Here’s a quick brain dump of things you should be aware of:

- 2-body orbital motion is elliptical with the parent body at one of the foci of the ellipse.
- A body orbiting another obeys [Kepler’s Laws of Planetary Motion](https://en.wikipedia.org/wiki/Kepler%27s_laws_of_planetary_motion).
- Orbits are frequently parameterized based on the following:
  - The angle between some reference direction and the “ascending node” which is the point at which the orbit crosses from below to above the “celestial plane” which is the plane that you think of as “level”
  - The angle of inclination, which is how tilted the orbit is relative to the “celestial plane”
  - The angle of periapse, which is the angle in the plane of the orbit between the ascending node and the point where the satellite is closest to it’s parent
  - The eccentricity, which is how elongated the orbit is (0 is circular, 1 is parabolic, in between 0 and 1 is elliptical, \> 1 is hyperbolic)
  - The semi major axis (which is half the long diameter of the ellipse, although the meaning for hyperbolic orbits is different)

- These are called [“orbital elements”](https://en.wikipedia.org/wiki/Orbital_elements).
- The position of a satellite within it’s orbit is denoted by it’s “True Anomaly” which is the angle between the point of periapsis as the satellites current position.
- The True Anomaly varies **non-linearly** because a satellite moves quickly when it is close to it’s parent and more slowly when it is far away.
- However True Anomaly can be related to the “Mean Anomaly”, which basically denotes what fraction it’s orbit a satellite has progressed through in terms of an angle, via [Kepler’s Equation](https://en.wikipedia.org/wiki/Kepler%27s_equation).
- Solving Kepler’s equation to go from Mean Anomaly to True Anomaly is a bit complicated because it doesn’t have a simple algebraic solution so you wind up using either an approximation or iteration (see Newton’s Method).

I have a sketch that demonstrates some of these principles, however, I must apologize because it is implemented in p5.js not processing, and it is incomplete and uncommented: [Orbit Diagram - OpenProcessing](https://openprocessing.org/sketch/1136125)

---

<div class="post-metadata">

**Author:** ![matto](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@matto](https://discourse.processing.org/u/matto)\
**Post date:** [June 6, 2021, 7:28pm UTC](https://discourse.processing.org/t/active-processing-program-to-simulate-a-planet-orbiting-around-a-sun/30462/4 "2021-06-06T19:28:28Z")

</div>

Hey man i was able to figure it out thanks

---

<div class="post-metadata">

**Author:** ![MacJ](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@MacJ](https://discourse.processing.org/u/MacJ)\
**Post date:** [June 6, 2021, 10:04pm UTC](https://discourse.processing.org/t/active-processing-program-to-simulate-a-planet-orbiting-around-a-sun/30462/5 "2021-06-06T22:04:19Z")

</div>

Hello can you help me with this same question

---

<div class="post-metadata">

**Author:** ![MacJ](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@MacJ](https://discourse.processing.org/u/MacJ)\
**Post date:** [June 6, 2021, 10:10pm UTC](https://discourse.processing.org/t/active-processing-program-to-simulate-a-planet-orbiting-around-a-sun/30462/6 "2021-06-06T22:10:05Z")

</div>

Hello can I get help with the same question

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [June 6, 2021, 11:39pm UTC](https://discourse.processing.org/t/active-processing-program-to-simulate-a-planet-orbiting-around-a-sun/30462/7 "2021-06-06T23:39:21Z")

</div>

THE SAME REPLY APPLIES TO YOU.

What have you got so far?

Seriously. Post your code.

Without knowing what you already have working, we are at a total loss as to how to help you.

Are you trying to increase the radius of the planets? Are you trying to have elliptical orbits? I have no idea what you have already.

Post your code. Tell us what works, and what doesn’t.

---

<div class="post-metadata">

**Author:** ![MacJ](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@MacJ](https://discourse.processing.org/u/MacJ)\
**Post date:** [June 7, 2021, 1:47am UTC](https://discourse.processing.org/t/active-processing-program-to-simulate-a-planet-orbiting-around-a-sun/30462/8 "2021-06-07T01:47:17Z")

</div>

final int SUN\_DIAM = 50;  
final int ORBIT\_X = 150;  
final int ORBIT\_Y = 35;  
final float SPEED = 0.03;  
float angle = 0;  
float planetDiam = 25;  
float xPos, yPos;

void setup () {  
size(500, 500);  
}

void draw () {  
background(0);  
fill(255, 255, 0);  
circle(width/2, height/2, SUN\_DIAM);

xPos = ORBIT\_X \* cos(angle) + width/2;  
yPos = ORBIT\_Y \* sin(angle) + height/2;

fill(120);  
circle(xPos, yPos, planetDiam);

angle = angle + SPEED;  
planetDiam = 10 \* cos(angle) + 15;  
}

this is my code so far

---

<div class="post-metadata">

**Author:** ![MacJ](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@MacJ](https://discourse.processing.org/u/MacJ)\
**Post date:** [June 7, 2021, 1:49am UTC](https://discourse.processing.org/t/active-processing-program-to-simulate-a-planet-orbiting-around-a-sun/30462/9 "2021-06-07T01:49:13Z")

</div>

I want to make the diameter of the planet change, depending on angle using a sin or cos function. Thank you !

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [June 7, 2021, 2:04am UTC](https://discourse.processing.org/t/active-processing-program-to-simulate-a-planet-orbiting-around-a-sun/30462/10 "2021-06-07T02:04:01Z")

</div>

> [@MacJ](#):
>
> planetDiam = 10 \* cos(angle) + 15;

This makes the diameter of your planet change, depending on the angle, using a cos() function. You have what you want already. I have no idea what more you want.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 7, 2021, 2:14am UTC](https://discourse.processing.org/t/active-processing-program-to-simulate-a-planet-orbiting-around-a-sun/30462/11 "2021-06-07T02:14:54Z")

</div>

@MacJ ,

You already have a topic on this here and tagged as homework:  
_[Scale using cos/sin](https://discourse.processing.org/t/scale-using-cos-sin/30542)_

Our forum:  
[https://discourse.processing.org/faq#keep-tidy](https://discourse.processing.org/faq#keep-tidy)

Asks that you:

> Don’t cross-post the same thing in multiple topics

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 7, 2021, 10:39am UTC](https://discourse.processing.org/t/active-processing-program-to-simulate-a-planet-orbiting-around-a-sun/30462/12 "2021-06-07T10:39:15Z")

</div>

Hello,

An example:

```auto
void setup() 
  {
  size(300, 300);
  }

void draw() 
  {
  background(0);
  
  // This advances every frame by TWO_PI/360
  float angle = frameCount*TAU/360; //TAU = TWO_PI radians
  
  //Useful:
  println(frameCount, angle, degrees(angle));
  
  float planetDiam = 150 + 100 * cos(angle);
  circle(width/2, height/2, planetDiam);
  }

```

You may find some of this useful in projects.

`:)`
