using UnityEngine; // PlanetController.cs // This script is attached to each celestial body (e.g., Sun, Earth, Moon) in the Unity scene. // It holds the physical properties like mass and current kinematic state. // The NBodySimulator will directly update its position, velocity, and acceleration. public class PlanetController : MonoBehaviour { [Tooltip("Mass of the celestial body in kilograms (kg). Use real-world values.")] public double mass; // Using double for higher precision with large astronomical masses [Tooltip("Initial position of the celestial body in meters (m) relative to the system's origin (0,0,0).")] [SerializeField] private Vector3d initialPosition_m; // NOW USING Vector3d [Tooltip("Initial velocity of the celestial body in meters per second (m/s). Use real-world values.")] [SerializeField] private Vector3d initialVelocity_m_per_s; // NOW USING Vector3d // Current position of the planet in meters. Managed by NBodySimulator. [HideInInspector] // Hide in Inspector as it's managed by the simulation public Vector3d currentPosition_m; // NOW USING Vector3d // Current velocity of the planet in meters per second. Managed by NBodySimulator. [HideInInspector] // Hide in Inspector as it's managed by the simulation public Vector3d currentVelocity_m_per_s; // NOW USING Vector3d // Current acceleration of the planet in meters per second squared (m/s^2). // This is accumulated by NBodySimulator from all gravitational forces. [HideInInspector] // Hide in Inspector as it's managed by the simulation public Vector3d currentAcceleration_m_per_s_sq; // NOW USING Vector3d /// /// Initializes the planet's state with its initial position and velocity. /// Called by NBodySimulator at the start of the simulation. /// public void Initialize() { currentPosition_m = initialPosition_m; currentVelocity_m_per_s = initialVelocity_m_per_s; currentAcceleration_m_per_s_sq = Vector3d.zero; // Start with zero acceleration } // Resets the accumulated acceleration to zero for the next simulation step. // This is called at the beginning of each simulation step by NBodySimulator. public void ResetAcceleration() { currentAcceleration_m_per_s_sq = Vector3d.zero; } // Updates the planet's velocity and position using the Velocity Verlet integration method. // This is called by NBodySimulator. // dt is the simulation time step in seconds. public void UpdateKinematics(float dt) { // Velocity Verlet Integration // 1. Update position: x(t+dt) = x(t) + v(t)*dt + 0.5*a(t)*dt^2 currentPosition_m += currentVelocity_m_per_s * dt + 0.5 * currentAcceleration_m_per_s_sq * dt * dt; // 2. Calculate new acceleration (this is done in NBodySimulator after all forces are summed) // 3. Update velocity: v(t+dt) = v(t) + 0.5*(a(t) + a(t+dt))*dt // (The a(t+dt) part will be added in NBodySimulator after all forces for the next step are calculated) } // This is the second part of the Velocity Verlet integration, called after // all forces for the new position (t+dt) have been calculated in NBodySimulator. public void FinalizeVelocity(Vector3d newAcceleration_m_per_s_sq, float dt) { // v(t+dt) = v(t) + 0.5*(a(t) + a(t+dt))*dt currentVelocity_m_per_s += 0.5 * (currentAcceleration_m_per_s_sq + newAcceleration_m_per_s_sq) * dt; currentAcceleration_m_per_s_sq = newAcceleration_m_per_s_sq; // Store new acceleration for next step's initial 'a(t)' } } Planetary Instability Model PIM - Copyright (C) 2025 James Pacha