# Coding Sun, Moon and Earth realistically

**URL:** <https://discourse.processing.org/t/coding-sun-moon-and-earth-realistically/38008>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [July 17, 2022, 6:37pm UTC](https://discourse.processing.org/t/coding-sun-moon-and-earth-realistically/38008 "2022-07-17T18:37:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Beppo](https://avatars.discourse-cdn.com/v4/letter/b/b77776/32.png) [@Beppo](https://discourse.processing.org/u/Beppo)\
**Post date:** [July 17, 2022, 6:37pm UTC](https://discourse.processing.org/t/coding-sun-moon-and-earth-realistically/38008/1 "2022-07-17T18:37:35Z")

</div>

Hello, for a scool project I have to code the in the reality existing system containing sun, moon and earth as realistically as possible. In my opinion the three bodies should move witch my current code as they do in reality. I put the real values into the equation of motion. What did I do wrong? I would really appreciate any tips or help. Thank you!  
Here is the link of my current code: [p5.js Web Editor](https://editor.p5js.org/AndreScherl/sketches/O3PIoHAr8)

---

<div class="post-metadata">

**Author:** ![Divitiacus](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@Divitiacus](https://discourse.processing.org/u/Divitiacus)\
**Post date:** [July 18, 2022, 4:09pm UTC](https://discourse.processing.org/t/coding-sun-moon-and-earth-realistically/38008/2 "2022-07-18T16:09:07Z")

</div>

Your moon and earth flying away, so it looks like your attractive gravitational force is not strong enough.  
I would adjust it to a point that the earth gets the right trajectory.

---

<div class="post-metadata">

**Author:** ![Pollisco](https://avatars.discourse-cdn.com/v4/letter/p/b5a626/32.png) [@Pollisco](https://discourse.processing.org/u/Pollisco)\
**Post date:** [July 21, 2022, 3:23am UTC](https://discourse.processing.org/t/coding-sun-moon-and-earth-realistically/38008/3 "2022-07-21T03:23:33Z")

</div>

Right now you probably only need to increase the starting velocity of the earth to put it on orbit, also you’d need to change the position and starting velocity of the moon as well, because it looks like the starting position is inside the earth. I wouldn’t know where to apply the changes in your code, but i’m working on a similar demo, and thats all i needed to make mine follow an orbit. At the end of the day you’d probably need a lot of fine tuning if you want a consistent orbit tho. Gool luck, I hope I helped.

---

<div class="post-metadata">

**Author:** ![Beppo](https://avatars.discourse-cdn.com/v4/letter/b/b77776/32.png) [@Beppo](https://discourse.processing.org/u/Beppo)\
**Post date:** [September 11, 2022, 12:38pm UTC](https://discourse.processing.org/t/coding-sun-moon-and-earth-realistically/38008/4 "2022-09-11T12:38:31Z")

</div>

Hi Pollisco, the email with your message landed in my spam ordner, thats why i am aswering so late, neverthanless thanks for replying , could you maybe send me a link so I could have a look at your code and compare it to mine, maybe it helps me. See you, Beppo

---

<div class="post-metadata">

**Author:** ![Beppo](https://avatars.discourse-cdn.com/v4/letter/b/b77776/32.png) [@Beppo](https://discourse.processing.org/u/Beppo)\
**Post date:** [October 11, 2022, 2:30pm UTC](https://discourse.processing.org/t/coding-sun-moon-and-earth-realistically/38008/5 "2022-10-11T14:30:57Z")

</div>

Hello, could you maybe send me a link of your code, maybe it helps me, thanks

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [October 11, 2022, 3:00pm UTC](https://discourse.processing.org/t/coding-sun-moon-and-earth-realistically/38008/6 "2022-10-11T15:00:21Z")

</div>

Hello @Beppo

> [@Beppo](#):
>
> could you maybe send me a link of your code

Since this is a project for school, please read the HOMEWORK POLICY guidelines in link below:

[https://discourse.processing.org/faq#homework](https://discourse.processing.org/faq#homework)

In short, it is against the forum guidelines to provide full code solutions.

However, on the youtube CODING TRAIN channel there are several (at least 4) tutorials available.  
I have linked 2 below. The first is using processing, and the second is p5.js:

[![](https://img.youtube.com/vi/l8SiJ-RmeHU/hqdefault.jpg "Coding Challenge #7: Solar System in Processing - Part 1 (2D)") ](https://www.youtube.com/watch?v=l8SiJ-RmeHU)

[![](https://img.youtube.com/vi/EpgB3cNhKPM/maxresdefault.jpg "2.5 Gravitational Attraction - The Nature of Code") ](https://www.youtube.com/watch?v=EpgB3cNhKPM)

Good luck!  
🤓
