2D gravity nearly correct simulation using p5js
Just some fun project in free time where i wanted to learn p5js and wanted to create this 3-body-problem.
Mass and Gravity are tone down
If i made a mistake or want to know anything, feel free to open an issue
Only Sun Excerting Gravity (Fixed Position of Planets)

Gravity Exerting by Planets as well (Random Position of Planets)

This project is a 2D simulation of a solar system using p5.js.
- Planets are initialized at random angles and distances.
- A trailing effect for planets to visualize their paths.
- The simulation speed can be adjusted for a faster or slower experience.
Just if anyone wants to recreate this. This simulation is based on fundamental concepts of classical mechanics, particularly Newton's Law of Universal Gravitation and the principles of orbital motion.
-
Gravitational Force: [ F = \frac{G \cdot m_1 \cdot m_2}{r^2} ]
Where:
- ( F ) is the gravitational force,
- ( G ) is the gravitational constant,
- ( m_1 ) and ( m_2 ) are the masses of the objects,
- ( r ) is the distance between the centers of the two masses.
-
Velocity and Orbital Motion: The velocity required for a planet to maintain its orbit can be derived from the gravitational force equation. For a circular orbit, the gravitational force provides the necessary centripetal force:
[ v = \sqrt{\frac{G \cdot M}{r}} ]
Where:
- ( v ) is the orbital velocity,
- ( M ) is the mass of the central body (sun),
- ( r ) is the distance from the center of the mass to the orbiting planet.
-
Planet Class: Represents each planet with properties for position, mass, radius, color, velocity, and a trail of previous positions. The
updatemethod calculates the new position based on gravitational forces. -
Gravity Calculation: The
gravitymethod computes the gravitational force exerted on the planet by another mass, adjusting its velocity accordingly. -
Setup and Draw Functions:
- The
setupfunction initializes the canvas and creates the sun and planets. - The
drawfunction updates and displays the planets in each frame, allowing for smooth animation.
- The
- Clone the repository to your local machine.
- Open the
index.htmlfile in your web browser. - Adjust the speed and observe the planets' movements! (Wow ...in the voice of Owen Wilson)
This project is licensed under the MIT License. Feel free to modify and use the code as you wish.