Files
ScienceLab.TypesOfMotion/Assets/Scripts/MarsmoonsOrbit.cs
2026-02-18 00:08:49 +02:00

162 lines
4.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MarsmoonsOrbit : MonoBehaviour
{
public Transform mars;
public MoonType moonType = MoonType.Phobos;
public bool autoCalculateOrbit = true;
public float manualOrbitRadius = 9.4f;
public float orbitInclination = 1.08f;
public float startAngleDegrees = 0f;
public float timeScale = 10f;
public bool showDebugInfo = false;
private const float MARS_RADIUS_KM = 3390f;
private const float PHOBOS_DISTANCE_KM = 9376f;
private const float PHOBOS_PERIOD_HOURS = 7.65f;
private const float DEIMOS_DISTANCE_KM = 23463f;
private const float DEIMOS_PERIOD_HOURS = 30.3f;
private float orbitRadius;
private float angularSpeed;
private float currentAngle = 0f;
private float orbitCount = 0f;
private float debugTimer = 0f;
public enum MoonType
{
Phobos,
Deimos
}
void Start()
{
if (mars == null)
{
mars = GameObject.Find("Mars")?.transform;
if (mars == null)
{
enabled = false;
return;
}
}
CalculateOrbit();
SetInitialPosition();
}
void CalculateOrbit()
{
float marsRadius = mars.localScale.x / 2f;
if (autoCalculateOrbit)
{
if (moonType == MoonType.Phobos)
{
orbitRadius = marsRadius * (PHOBOS_DISTANCE_KM / MARS_RADIUS_KM);
float period = PHOBOS_PERIOD_HOURS * 3600f;
angularSpeed = (2f * Mathf.PI / period) * timeScale;
orbitInclination = 1.08f;
}
else
{
orbitRadius = marsRadius * (DEIMOS_DISTANCE_KM / MARS_RADIUS_KM);
float period = DEIMOS_PERIOD_HOURS * 3600f;
angularSpeed = (2f * Mathf.PI / period) * timeScale;
orbitInclination = 1.79f;
}
}
else
{
orbitRadius = manualOrbitRadius;
float period = (moonType == MoonType.Phobos ? PHOBOS_PERIOD_HOURS : DEIMOS_PERIOD_HOURS) * 3600f;
angularSpeed = (2f * Mathf.PI / period) * timeScale;
}
}
void SetInitialPosition()
{
currentAngle = startAngleDegrees * Mathf.Deg2Rad;
UpdatePosition();
}
void Update()
{
if (mars == null) return;
currentAngle += angularSpeed * Time.deltaTime;
if (currentAngle >= 2f * Mathf.PI)
{
currentAngle -= 2f * Mathf.PI;
orbitCount++;
if (showDebugInfo)
{
Debug.Log(moonType + " orbit " + orbitCount);
}
}
UpdatePosition();
if (showDebugInfo)
{
debugTimer += Time.deltaTime;
if (debugTimer >= 10f)
{
debugTimer = 0f;
float progress = (currentAngle / (2f * Mathf.PI)) * 100f;
Debug.Log(moonType + ": R=" + orbitRadius.ToString("F2") + " P=" + progress.ToString("F1") + "%");
}
}
}
void UpdatePosition()
{
float x = Mathf.Cos(currentAngle) * orbitRadius;
float z = Mathf.Sin(currentAngle) * orbitRadius;
float inclinationRad = orbitInclination * Mathf.Deg2Rad;
float y = z * Mathf.Sin(inclinationRad);
z = z * Mathf.Cos(inclinationRad);
transform.position = mars.position + new Vector3(x, y, z);
}
void OnDrawGizmos()
{
if (mars == null) return;
Gizmos.color = moonType == MoonType.Phobos ? new Color(1f, 0.5f, 0.5f, 0.5f) : new Color(0.5f, 0.7f, 1f, 0.5f);
int segments = 64;
Vector3 prevPos = GetOrbitPoint(0);
for (int i = 1; i <= segments; i++)
{
Vector3 newPos = GetOrbitPoint(i * 2f * Mathf.PI / segments);
Gizmos.DrawLine(prevPos, newPos);
prevPos = newPos;
}
}
Vector3 GetOrbitPoint(float angle)
{
float x = Mathf.Cos(angle) * orbitRadius;
float z = Mathf.Sin(angle) * orbitRadius;
float inclinationRad = orbitInclination * Mathf.Deg2Rad;
float y = z * Mathf.Sin(inclinationRad);
z = z * Mathf.Cos(inclinationRad);
return mars.position + new Vector3(x, y, z);
}
}