using System; using UnityEngine; // Required for implicit conversion to/from Unity's Vector3 // Vector3d.cs // A custom struct for 3D vectors using double-precision floating-point numbers. // This is crucial for maintaining accuracy in large-scale astronomical simulations. [Serializable] // Make it serializable so it can be used in the Inspector public struct Vector3d { public double x; public double y; public double z; public Vector3d(double x, double y, double z) { this.x = x; this.y = y; this.z = z; } // Returns the squared magnitude (length) of the vector. public double sqrMagnitude { get { return x * x + y * y + z * z; } } // Returns the magnitude (length) of the vector. public double magnitude { get { return Math.Sqrt(sqrMagnitude); } } // Returns a normalized version of the vector (unit vector). public Vector3d normalized { get { double mag = magnitude; if (mag == 0) return zero; // Avoid division by zero return new Vector3d(x / mag, y / mag, z / mag); } } // Static property for a zero vector. public static Vector3d zero { get { return new Vector3d(0, 0, 0); } } // Static property for a one vector. public static Vector3d one { get { return new Vector3d(1, 1, 1); } } // Operator overloads for common vector arithmetic public static Vector3d operator +(Vector3d a, Vector3d b) { return new Vector3d(a.x + b.x, a.y + b.y, a.z + b.z); } public static Vector3d operator -(Vector3d a, Vector3d b) { return new Vector3d(a.x - b.x, a.y - b.y, a.z - b.z); } public static Vector3d operator *(Vector3d a, double d) { return new Vector3d(a.x * d, a.y * d, a.z * d); } public static Vector3d operator *(double d, Vector3d a) { return new Vector3d(a.x * d, a.y * d, a.z * d); } public static Vector3d operator /(Vector3d a, double d) { if (d == 0) throw new DivideByZeroException("Cannot divide Vector3d by zero."); return new Vector3d(a.x / d, a.y / d, a.z / d); } public static Vector3d operator -(Vector3d a) { return new Vector3d(-a.x, -a.y, -a.z); } // Dot product public double Dot(Vector3d other) { return x * other.x + y * other.y + z * other.z; } // Cross product public Vector3d Cross(Vector3d other) { return new Vector3d( y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x ); } // Implicit conversion from Vector3d to Unity's Vector3 (for rendering/Unity API compatibility) public static implicit operator Vector3(Vector3d v) { return new Vector3((float)v.x, (float)v.y, (float)v.z); } // Implicit conversion from Unity's Vector3 to Vector3d public static implicit operator Vector3d(Vector3 v) { return new Vector3d(v.x, v.y, v.z); } public override string ToString() { return $"({x:F6}, {y:F6}, {z:F6})"; } public override bool Equals(object obj) { if (!(obj is Vector3d)) return false; Vector3d other = (Vector3d)obj; return x == other.x && y == other.y && z == other.z; } public override int GetHashCode() { return x.GetHashCode() ^ y.GetHashCode() << 2 ^ z.GetHashCode() >> 2; } public static bool operator ==(Vector3d a, Vector3d b) { return a.Equals(b); } public static bool operator !=(Vector3d a, Vector3d b) { return !a.Equals(b); } } Planetary Instability Model PIM - Copyright (C) 2025 James Pacha