Overview

Unity Solar System Simulation is a real-time, mathematically accurate 3D simulation of our planetary system built using Unity 6 and C# scripting. The project focuses on rendering orbital mechanics, planetary bodies, and realistic lighting models leveraging Unity's Universal Render Pipeline (URP) and custom shaders.

"Simulating gravity and orbital paths in real-time showcases the beautiful intersection between astrophysics, mathematics, and high-performance Unity engine programming."

Key Features

Keplerian Orbit Physics

Calculates planetary coordinates dynamically in C# based on orbital eccentricity, inclination, and semi-major axes.

URP & Shader Graph

Utilizes Unity 6 URP Shader Graph for planetary atmospheres, solar bloom, and custom ring lighting.

Interactive UI Toolkit

Integrated dashboard for time warp scale, camera tracking, planet highlighting, and live telemetries.

Orbital Path Trails

Renders dynamic path trails in 3D space using Unity LineRenderer components tracing planetary orbits.

Technical Implementation

To model planet orbits, we solve Kepler's Equation at each frame step. Since Kepler's Equation has no analytical solution, we use Newton-Raphson iteration. The planetary parameters (semi-major axis, eccentricity, inclination, longitude of ascending node, argument of perihelion) are configured based on NASA's planetary data sheets.

Keplerian Orbit Position Solver (C#)

Here is a sample code snippet from the simulation logic showing how the 3D position is calculated in the orbital plane and then rotated into the ecliptic coordinate system:

using UnityEngine;

public class PlanetOrbit : MonoBehaviour
{
    [Header("Orbital Parameters")]
    public float semiMajorAxis = 10f;
    public float eccentricity = 0.0167f;
    public float orbitalPeriod = 365.25f;
    public float inclination = 0.0f;
    public float longAscNode = 0.0f;
    public float argPerihelion = 0.0f;

    private float meanAnomaly = 0.0f;

    void Update()
    {
        // Update Mean Anomaly based on time step
        float meanMotion = (2.0f * Mathf.PI) / orbitalPeriod;
        meanAnomaly += meanMotion * Time.deltaTime;
        meanAnomaly %= (2.0f * Mathf.PI);

        // Solve Kepler's Equation: E - e * sin(E) = M
        float E = SolveKepler(meanAnomaly, eccentricity);

        // Calculate coordinates in the orbital plane
        float x_orbital = semiMajorAxis * (Mathf.Cos(E) - eccentricity);
        float y_orbital = semiMajorAxis * Mathf.Sqrt(1.0f - eccentricity * eccentricity) * Mathf.Sin(E);

        // Convert to Radians
        float i_rad = inclination * Mathf.Deg2Rad;
        float Omega_rad = longAscNode * Mathf.Deg2Rad;
        float w_rad = argPerihelion * Mathf.Deg2Rad;

        // Rotate position to 3D ecliptic coordinates
        float cosO = Mathf.Cos(Omega_rad), sinO = Mathf.Sin(Omega_rad);
        float cosw = Mathf.Cos(w_rad), sinw = Mathf.Sin(w_rad);
        float cosi = Mathf.Cos(i_rad), sini = Mathf.Sin(i_rad);

        float x_ecl = x_orbital * (cosw * cosO - sinw * sinO * cosi) - y_orbital * (sinw * cosO + cosw * sinO * cosi);
        float y_ecl = x_orbital * (cosw * sinO + sinw * cosO * cosi) - y_orbital * (sinw * sinO - cosw * cosO * cosi);
        float z_ecl = x_orbital * (sinw * sini) + y_orbital * (cosw * sini);

        transform.localPosition = new Vector3(x_ecl, z_ecl, y_ecl); // Mapping Unity's coordinate system (Y-up)
    }

    private float SolveKepler(float M, float e)
    {
        float E = M;
        for (int step = 0; step < 5; ++step)
        {
            E = E - (E - e * Mathf.Sin(E) - M) / (1.0f - e * Mathf.Cos(E));
        }
        return E;
    }
}

Installation & Usage

Running the simulation locally requires Unity Hub and Unity Editor version 6 (6000.0+).

  • Clone the repository from GitHub: git clone https://github.com/mskreallabs/Unity-Solar-System-Simulation.git.
  • Open Unity Hub, click Add, and select the cloned project directory.
  • Ensure Unity 6 is selected as the editor version and launch the project.
  • In the project browser, navigate to Assets/Scenes/MainScene.unity and open it.
  • Press the Play button in the editor toolbar to run and test the simulation interface.