# Gravity in a solar system

**URL:** https://discourse.processing.org/t/gravity-in-a-solar-system/23299
**Category:** Coding Questions
**Created:** [August 17, 2020, 12:40am UTC](https://discourse.processing.org/t/gravity-in-a-solar-system/23299 "2020-08-17T00:40:24Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Cybernet](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@Cybernet](https://discourse.processing.org/u/Cybernet)
#### Post date: [August 17, 2020, 12:40am UTC](https://discourse.processing.org/t/gravity-in-a-solar-system/23299/1 "2020-08-17T00:40:24Z")

</div>

I’m trying to simulate a solar system type thing where on planet orbits around a sun. The planet has an initial velocity of x+2. However, it always seems to follow this long elliptical orbit.

I’m trying to get it so that after a while, it’s orbit soon smooths out into more of a round circle orbit, but I can’t figure out how.

```auto
PVector pos = new PVector(100, 100);
PVector dir = new PVector(2, 0);

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

void draw()
{
  background(255);
  
  fill(0);
  ellipse(300, 300, 50, 50);
  
  noFill();
  stroke(2);
  ellipse(pos.x, pos.y, 40, 40);
  
  PVector grav = new PVector(300 - pos.x, 300 - pos.y);
  grav.normalize();
  grav.div(dist(300, 300, pos.x, pos.y) / 15);
  
  dir.add(grav);
  pos.add(dir);
}

```

So right now, it follows a highly elliptical orbit, and orbits like that forever, but I want it to eventually go to a circle orbit. Any ideas?

---

<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: [August 17, 2020, 12:47am UTC](https://discourse.processing.org/t/gravity-in-a-solar-system/23299/2 "2020-08-17T00:47:31Z")

</div>

Hello,

I can share this most excellent resource:  
[https://natureofcode.com/book/](https://natureofcode.com/book/)

`:)`

---

<div class="post-metadata">

### Author: ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)
#### Post date: [August 17, 2020, 2:33am UTC](https://discourse.processing.org/t/gravity-in-a-solar-system/23299/3 "2020-08-17T02:33:28Z")

</div>

I suspect that orbits of astronomical objects become more circular over time due to gravitational interactions with objects other than the one they’re orbiting. So why not try to put one more in the system to see what happens?

---

<div class="post-metadata">

### Author: ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)
#### Post date: [August 17, 2020, 4:21am UTC](https://discourse.processing.org/t/gravity-in-a-solar-system/23299/4 "2020-08-17T04:21:11Z")

</div>

I mean something like this.  
https://www.openprocessing.org/sketch/83881/embed/?

---

<div class="post-metadata">

### Author: ![Cowhead](https://avatars.discourse-cdn.com/v4/letter/c/8491ac/32.png) [@Cowhead](https://discourse.processing.org/u/Cowhead)
#### Post date: [August 17, 2020, 6:42am UTC](https://discourse.processing.org/t/gravity-in-a-solar-system/23299/5 "2020-08-17T06:42:07Z")

</div>

if you spawn the orbiting body at 300, 100 instead it will be more circular, and change the dir to 3, 0 instead

---

<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: [August 17, 2020, 2:36pm UTC](https://discourse.processing.org/t/gravity-in-a-solar-system/23299/6 "2020-08-17T14:36:11Z")

</div>

@Cybernet,

With a bit of math you can plug in some “exact” values for your` PVector dir` (velocity) to give you a circular orbit:

> **[Centripetal force](https://en.wikipedia.org/wiki/Centripetal_force)**
>
> A centripetal force (from Latin centrum, "center" and petere, "to seek") is a force that makes a body follow a curved path. The direction of the centripetal force is always orthogonal to the motion of the body and towards the fixed point of the instantaneous center of curvature of the path. Isaac Newton described it as "a force by which bodies are drawn or impelled, or in any way tend, towards a point as to a centre". In the theory of Newtonian mechanics, gravity provides the centripetal fo One...

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b369948f359200028aec765e3cb07512832a2e23.png)

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/0351bb1f1c7ffbbcde068c5d45ea203015dd39f1.png)

Give it some thought.

I won’t give you the answer… the best part of this for me was solving this from an understanding of the fundamentals.

Circular orbits first… elliptical orbits later.

`:)`

Additional references:

- [Kepler’s laws of planetary motion](https://en.wikipedia.org/wiki/Kepler%27s_laws_of_planetary_motion)
- [Episode 9: Moving In Circles - The Mechanical Universe](https://www.youtube.com/watch?v=MSUkQXA5_bU)
- [Episode 21: Kepler’s Three Laws - The Mechanical Universe](https://www.youtube.com/watch?v=uJvOGp1wzTI)
- [The Mechanical Universe (Playlist)](https://www.youtube.com/watch?v=XtMmeAjQTXc&list=PL8_xPU5epJddRABXqJ5h5G0dk-XGtA5cZ)
