Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions version2/Assets/Editor/MakeLayers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
using UnityEditor;

[InitializeOnLoad]
public class Tags {
public static class MakeLayers {

// STARTUP
static Tags() {
static MakeLayers() {
CreateLayer();
}


// Creates a new layer
static void CreateLayer() {
private static void CreateLayer() {
SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);

SerializedProperty it = tagManager.GetIterator();
Expand All @@ -24,17 +24,17 @@ static void CreateLayer() {
it.stringValue = "Camera Layer "+(a-7);
}
}
}

for(int i=0;i<32;i++) {
for(int b=0;b<32;b++) {
if(b != i && b != 0)
Physics.IgnoreLayerCollision (b, i, true);//Physics.GetIgnoreLayerCollision (b,i));
else if (b == 0)
Physics.IgnoreLayerCollision (b, i, false);
}
for(int i=0;i<32;i++) {
for(int b=0;b<32;b++) {
if(b != i && b != 0)
Physics.IgnoreLayerCollision (b, i, true);//Physics.GetIgnoreLayerCollision (b,i));
else if (b == 0)
Physics.IgnoreLayerCollision (b, i, false);
}
}
tagManager.ApplyModifiedProperties();
}

}
}
1 change: 0 additions & 1 deletion version2/Assets/Scripts/tests/BodyElements.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using Elements;

Expand Down
15 changes: 4 additions & 11 deletions version2/Assets/Scripts/tests/CreateCameras.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Elements;
using SimpleJSON;
using BodyElements;
using ImportData;
using System.IO;
using CustomMath;

[DisallowMultipleComponent]
public class CreateCameras : MonoBehaviour {

int camDepth = 20;
[SerializeField] private int camDepth = 20;
void Awake () {
for (int i=1; i<19; i++) {
Camera cam = new GameObject().AddComponent<Camera>();
cam.depth = camDepth - i;
cam.transform.parent = transform;
cam.transform.SetParent(transform, true);
cam.transform.position = new Vector3(0f,0f,0f);
cam.name = "Camera "+i;
cam.farClipPlane = 20000f;
Expand All @@ -24,7 +17,7 @@ void Awake () {
cam.clearFlags = CameraClearFlags.Depth; // Add as a camera layer
else
cam.clearFlags = CameraClearFlags.Nothing; // Background layer
cam.camera.cullingMask = 1 << i+7;
cam.cullingMask = 1 << i+7;
cam.gameObject.layer = i+7;
cam.tag = "MainCamera";
Rigidbody _cameraRigidbody = cam.gameObject.AddComponent<Rigidbody>();
Expand Down
2 changes: 0 additions & 2 deletions version2/Assets/Scripts/tests/CreateElements.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using UnityEngine;
using System.Collections;
using Elements;

public class CreateElements : MonoBehaviour {

Expand Down
163 changes: 131 additions & 32 deletions version2/Assets/Scripts/tests/CreateSolarSystems.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using Elements;
using SimpleJSON;
using System.Globalization;
using BodyElements;
using ImportData;
using System.IO;
using CustomMath;
using Elements;
using ImportData;
using SimpleJSON;


[System.Serializable]
Expand All @@ -23,6 +22,7 @@ public class PlanetList {
public string name;
public Planet key;
public GameObject value;
public Vector3d positionInOrbit;

}

Expand All @@ -31,9 +31,11 @@ public class MoonList {
public string name;
public Moon key;
public GameObject value;
public Vector3d positionInOrbit;

}

[DisallowMultipleComponent]
public class CreateSolarSystems : MonoBehaviour {

public List<StarList> stars = new List<StarList>();
Expand All @@ -44,8 +46,7 @@ public class CreateSolarSystems : MonoBehaviour {
string JSONData; // Holds the data.json file data

void Awake () {
JSONData = Data.data.ReadToEnd (); // Read from the data.json file
Data.data.Close ();
JSONData = Data.LoadJson(); // Read from the data.json file

importedData = JSON.Parse(JSONData); // Parse the data into a formatted string variable

Expand All @@ -57,12 +58,12 @@ void Awake () {
_star.Name = importedData ["star"] [iteratorA] ["name"];
_star.RightAscension = importedData ["star"] [iteratorA] ["rightAscension"];
_star.Declination = importedData ["star"] [iteratorA] ["declination"];
_star.Mass = new Element ("Stellar Mass", double.Parse(importedData ["star"] [iteratorA]["stellarMass"]), "Kg", 0.0d, "SI", "StarTography 1.0", _star.DateLastUpdated);
_star.Radius = new Element ("Stellar Radius", double.Parse(importedData ["star"] [iteratorA] ["stellarRadius"]), "meter", 0.0d, "SI", "StarTography 1.0", _star.DateLastUpdated);
_star.Mass = new Element ("Stellar Mass", ParseDouble(importedData ["star"] [iteratorA]["stellarMass"]), "Kg", 0.0d, "SI", "StarTography 1.0", _star.DateLastUpdated);
_star.Radius = new Element ("Stellar Radius", ParseDouble(importedData ["star"] [iteratorA] ["stellarRadius"]), "meter", 0.0d, "SI", "StarTography 1.0", _star.DateLastUpdated);
//_star.Radius.Value *= 100d; // An example of how to multiply by Stellar Radii
_star.Distance = new Element("Distance", double.Parse(importedData ["star"] [iteratorA] ["distance"]), "meter", 0.0d, "SI", "StarTography 1.0", _star.DateLastUpdated);
_star.Luminosity = new Element("Optical Magnitude", double.Parse(importedData ["star"] [iteratorA] ["opticalMagnitude"]), "lum", 0.0d, "SI", "StarTography 1.0", _star.DateLastUpdated);
_star.Temperature = new Element("Temperature", double.Parse(importedData ["star"] [iteratorA] ["temperature"]), "celcius", 0.0d, "SI", "StarTography 1.0", _star.DateLastUpdated);
_star.Distance = new Element("Distance", ParseDouble(importedData ["star"] [iteratorA] ["distance"]), "meter", 0.0d, "SI", "StarTography 1.0", _star.DateLastUpdated);
_star.Luminosity = new Element("Optical Magnitude", ParseDouble(importedData ["star"] [iteratorA] ["opticalMagnitude"]), "lum", 0.0d, "SI", "StarTography 1.0", _star.DateLastUpdated);
_star.Temperature = new Element("Temperature", ParseDouble(importedData ["star"] [iteratorA] ["temperature"]), "celcius", 0.0d, "SI", "StarTography 1.0", _star.DateLastUpdated);

_star.Distance.ToM();

Expand Down Expand Up @@ -93,10 +94,13 @@ void Awake () {


// Generate Planets
for (int iteratorB=0; iteratorB < importedData["star"][iteratorA]["planets"].Count; iteratorB++) {
JSONNode planetDataList = importedData["star"][iteratorA]["planets"];
for (int iteratorB=0; iteratorB < planetDataList.Count; iteratorB++) {
JSONNode planetData = planetDataList[iteratorB];
Planet _planet = new Planet();
_planet.Name = importedData["star"][iteratorA]["planets"][iteratorB]["name"];
_planet.DateLastUpdated = importedData["star"][iteratorA]["planets"][iteratorB]["dateLastUpdate"];
_planet.Name = planetData["name"];
_planet.DateLastUpdated = GetString(planetData, "dateLastUpdate", _star.DateLastUpdated);
SetOrbitElements(_planet, planetData, "Planet");

GameObject _planetSystem = new GameObject("Planetary System: "+_planet.Name);
_star.ChildPlanets.Add (_planetSystem);
Expand All @@ -106,12 +110,16 @@ void Awake () {
_planets_.name = _planet.Name;
_planets_.key = _planet;
_planets_.value = _planetSystem;
_planets_.positionInOrbit = CalculateOrbitPosition(_planet, iteratorB, planetDataList.Count);
planets.Add (_planets_);

for (int iteratorC=0; iteratorC < importedData["star"][iteratorA]["planets"][iteratorB]["moons"].Count; iteratorC++) {
JSONNode moonDataList = planetData["moons"];
for (int iteratorC=0; iteratorC < moonDataList.Count; iteratorC++) {
JSONNode moonData = moonDataList[iteratorC];
Moon _moon = new Moon();
_moon.Name = importedData["star"][iteratorA]["planets"][iteratorB]["moons"][iteratorC]["name"];
_moon.DateLastUpdated = importedData["star"][iteratorA]["planets"][iteratorB]["moons"][iteratorC]["dateLastUpdate"];
_moon.Name = moonData["name"];
_moon.DateLastUpdated = GetString(moonData, "dateLastUpdate", _planet.DateLastUpdated);
SetOrbitElements(_moon, moonData, "Moon");

GameObject _moonSystem = new GameObject("Moon System: "+_moon.Name);
_planet.ChildMoons.Add(_moonSystem);
Expand All @@ -123,13 +131,104 @@ void Awake () {
_moons_.name = _moon.Name;
_moons_.key = _moon;
_moons_.value = _moonSystem;
_moons_.positionInOrbit = CalculateOrbitPosition(_moon, iteratorC, moonDataList.Count);
moons.Add (_moons_);
}
}
}

}


private static string GetString(JSONNode source, string key, string fallback) {
JSONNode node = source[key];
if (HasJsonValue(node))
return node.Value;
return string.IsNullOrEmpty(fallback) ? "Unknown" : fallback;
}

private static void SetOrbitElements(OrbitElement orbitElement, JSONNode source, string bodyType) {
string lastUpdated = string.IsNullOrEmpty(orbitElement.DateLastUpdated) ? "Unknown" : orbitElement.DateLastUpdated;

orbitElement.SemiMajorAxis = CreateElement(source, "semiMajorAxis", bodyType + " Semi-major Axis", "au", lastUpdated);
orbitElement.Eccentricity = CreateElement(source, "eccentricity", bodyType + " Eccentricity", "ratio", lastUpdated);
orbitElement.Inclination = CreateElement(source, "inclination", bodyType + " Inclination", "degree", lastUpdated);
orbitElement.ArgumentOfPariapsis = CreateElement(source, "argumentOfPariapsis", bodyType + " Argument of Pariapsis", "degree", lastUpdated);
orbitElement.Longitude = CreateElement(source, "longitude", bodyType + " Longitude of Ascending Node", "degree", lastUpdated);
orbitElement.MeanAnomaly = CreateElement(source, "meanAnomaly", bodyType + " Mean Anomaly", "degree", lastUpdated);
orbitElement.Period = CreateElement(source, "orbitalPeriod", bodyType + " Orbital Period", "day", lastUpdated);
orbitElement.EccentricAnomaly = CreateElement(source, "eccentricAnomaly", bodyType + " Eccentric Anomaly", "degree", lastUpdated);
orbitElement.TrueAnomaly = CreateElement(source, "trueAnomaly", bodyType + " True Anomaly", "degree", lastUpdated);
}

private static Element CreateElement(JSONNode source, string key, string name, string measurement, string lastUpdated) {
JSONNode node = source[key];
if (!HasJsonValue(node))
return null;

return new Element(name, ParseDouble(node), measurement, 0.0d, "SI", "StarTography 1.0", lastUpdated);
}

private static bool HasJsonValue(JSONNode node) {
return node != null && !string.IsNullOrEmpty(node.Value);
}

private static double ParseDouble(JSONNode node) {
return double.Parse(node.Value, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture);
}

private static Vector3d CalculateOrbitPosition(OrbitElement orbitElement, int siblingIndex, int siblingCount) {
if (orbitElement == null || orbitElement.SemiMajorAxis == null)
return new Vector3d();

double semiMajorAxis = Maths.InMM(orbitElement.SemiMajorAxis);
double eccentricity = ClampEccentricity(GetElementValue(orbitElement.Eccentricity, 0d));
double inclination = GetElementValue(orbitElement.Inclination, 0d) * Maths.Deg2Rad;
double longitude = GetElementValue(orbitElement.Longitude, 0d) * Maths.Deg2Rad;
double argumentOfPariapsis = GetElementValue(orbitElement.ArgumentOfPariapsis, 0d) * Maths.Deg2Rad;
double trueAnomaly = GetOrbitAngleDegrees(orbitElement, siblingIndex, siblingCount) * Maths.Deg2Rad;

double orbitalRadius = semiMajorAxis * (1d - (eccentricity * eccentricity)) / (1d + (eccentricity * Math.Cos(trueAnomaly)));
double angleInOrbit = argumentOfPariapsis + trueAnomaly;

double cosLongitude = Math.Cos(longitude);
double sinLongitude = Math.Sin(longitude);
double cosInclination = Math.Cos(inclination);
double sinInclination = Math.Sin(inclination);
double cosAngle = Math.Cos(angleInOrbit);
double sinAngle = Math.Sin(angleInOrbit);

return new Vector3d(
orbitalRadius * ((cosLongitude * cosAngle) - (sinLongitude * sinAngle * cosInclination)),
orbitalRadius * ((sinLongitude * cosAngle) + (cosLongitude * sinAngle * cosInclination)),
orbitalRadius * (sinAngle * sinInclination)
);
}

private static double GetOrbitAngleDegrees(OrbitElement orbitElement, int siblingIndex, int siblingCount) {
if (orbitElement.TrueAnomaly != null)
return orbitElement.TrueAnomaly.Value;
if (orbitElement.MeanAnomaly != null)
return orbitElement.MeanAnomaly.Value;
if (orbitElement.EccentricAnomaly != null)
return orbitElement.EccentricAnomaly.Value;
if (siblingCount <= 0)
return 0d;

return (360d * siblingIndex) / siblingCount;
}

private static double GetElementValue(Element element, double fallback) {
return element == null ? fallback : element.Value;
}

private static double ClampEccentricity(double eccentricity) {
if (double.IsNaN(eccentricity))
return 0d;

return Math.Min(Math.Max(eccentricity, 0d), 0.999999d);
}

void Start() {


Expand All @@ -138,15 +237,15 @@ void Start() {
star.value.transform.position = star.positionInSpace;
GameObject _starGameObject = GameObject.CreatePrimitive(PrimitiveType.Sphere); // Create the Star object
_starGameObject.name = "Star: "+star.name;
_starGameObject.transform.parent = star.value.transform;
_starGameObject.transform.position = star.value.transform.position;
_starGameObject.transform.SetParent(star.value.transform, false);
_starGameObject.transform.localPosition = Vector3.zero;
SphereCollider sphereCollider = _starGameObject.GetComponent<SphereCollider>();
sphereCollider.isTrigger = false;
sphereCollider.radius = 1f;

GameObject _starDistanceColliders = new GameObject("Star: "+star.name+": Distance Colliders");
_starDistanceColliders.transform.parent = star.value.transform;
_starDistanceColliders.transform.position = star.value.transform.position;
_starDistanceColliders.transform.SetParent(star.value.transform, false);
_starDistanceColliders.transform.localPosition = Vector3.zero;

Element _starSize = new Element("Radius of the Star", star.key.Radius.Value, "stellarRadius", 0.0d, "si", "Allen's Astrophysical Quantities 4th Edition", star.key.DateLastUpdated);
Debug.Log ("Before: "+_starSize.Name+" "+_starSize.Value+" "+_starSize.Measurement);
Expand All @@ -166,8 +265,8 @@ void Start() {
colliderRadiusScale = 10f * Mathf.Exp ((i-3)/2f);

GameObject _starDistanceCollider = new GameObject("Star: "+star.name+": Distance Collider "+i);
_starDistanceCollider.transform.parent = _starDistanceColliders.transform;
_starDistanceCollider.transform.position = star.value.transform.position;
_starDistanceCollider.transform.SetParent(_starDistanceColliders.transform, false);
_starDistanceCollider.transform.localPosition = Vector3.zero;
_starDistanceCollider.transform.localScale = new Vector3(1f,1f,1f);
Rigidbody _sphereRigidbody = _starDistanceCollider.AddComponent<Rigidbody>();
_sphereRigidbody.useGravity = false;
Expand All @@ -180,12 +279,12 @@ void Start() {

// Prepare the planetary system parent
foreach (PlanetList planet in planets) {
planet.value.transform.parent = planet.key.ParentStar.transform;
planet.value.transform.position = planet.key.ParentStar.transform.position;
planet.value.transform.SetParent(planet.key.ParentStar.transform, false);
planet.value.transform.localPosition = planet.positionInOrbit;
GameObject _planetGameObject = GameObject.CreatePrimitive(PrimitiveType.Sphere); // Create the Planet object
_planetGameObject.name = "Planet: "+planet.name;
_planetGameObject.transform.parent = planet.value.transform;
_planetGameObject.transform.position = planet.value.transform.position;
_planetGameObject.transform.SetParent(planet.value.transform, false);
_planetGameObject.transform.localPosition = Vector3.zero;
SphereCollider sphereCollider = _planetGameObject.GetComponent<SphereCollider>();
sphereCollider.isTrigger = false;
sphereCollider.radius = 1f;
Expand All @@ -194,12 +293,12 @@ void Start() {

// Prepare the moon system parent
foreach (MoonList moon in moons) {
moon.value.transform.parent = moon.key.ParentPlanet.transform;
moon.value.transform.position = moon.key.ParentPlanet.transform.position;
moon.value.transform.SetParent(moon.key.ParentPlanet.transform, false);
moon.value.transform.localPosition = moon.positionInOrbit;
GameObject _moonGameObject = GameObject.CreatePrimitive(PrimitiveType.Sphere); // Create the Moon object
_moonGameObject.name = "Moon: "+moon.name;
_moonGameObject.transform.parent = moon.value.transform;
_moonGameObject.transform.position = moon.value.transform.position;
_moonGameObject.transform.SetParent(moon.value.transform, false);
_moonGameObject.transform.localPosition = Vector3.zero;
SphereCollider sphereCollider = _moonGameObject.GetComponent<SphereCollider>();
sphereCollider.isTrigger = false;
sphereCollider.radius = 1f;
Expand Down
Loading