OrbitingBody CreateNewOrbit(string name, string parent, OrbitProperties props) { OrbitingBody script; GameObject newOrbit; newOrbit = (GameObject)Instantiate(orbitPrefab, transform.position, transform.rotation); newOrbit.name = name + "Orbit"; if (parent == "") { newOrbit.transform.parent = transform; } else { // Moons and other bodies orbiting around greater masses will be added here. GameObject newParent = GameObject.Find(parent + "Orbit/BodyContainer"); newOrbit.transform.parent = newParent.transform; } script = newOrbit.GetComponent<OrbitingBody>(); script.eccentricity = props.eccentricity; script.semiMajorAxis = props.semiMajorAxis; script.periodInMinutes = props.period; return script; }
OrbitingBody CreateNewOrbit(string name, string parent, OrbitProperties props) { OrbitingBody script; GameObject newOrbit; newOrbit = (GameObject)Instantiate(orbitPrefab, transform.position, transform.rotation); newOrbit.name = name + "Orbit"; if (parent == "") { newOrbit.transform.parent = transform; } else { // Moons and other bodies orbiting around greater masses will be added here. GameObject newParent = GameObject.Find(parent + "Orbit/BodyContainer"); newOrbit.transform.parent = newParent.transform; } script = newOrbit.GetComponent <OrbitingBody>(); script.eccentricity = props.eccentricity; script.semiMajorAxis = props.semiMajorAxis; script.periodInMinutes = props.period; return(script); }
// This method will be used to create star system manually, or to customize it. public void AddBodyTo(string parentBody, BodyProperties body, OrbitProperties orbit, string bodyName) { // This will be added to the root of the system, // Stars and main planets will belong to this level. GameObject newBody = CreateNewBody(bodyName, body); OrbitingBody orbitScript = CreateNewOrbit(bodyName, parentBody, orbit); orbitScript.AddBody(newBody, body); }
// This method will be used to create star system manually, or to customize it. public void AddBodyTo(string parentBody, BodyProperties body, OrbitProperties orbit, string bodyName) { // This will be added to the root of the system, // Stars and main planets will belong to this level. GameObject newBody = CreateNewBody(bodyName, body); OrbitingBody orbitScript = CreateNewOrbit(bodyName, parentBody, orbit); orbitScript.AddBody(newBody, body); }
void Update() { //make sure the prediction is up to date att.CalculatePrediction(); //make sure we have the correct number of lines //set the lines verticies List <Vector3> positions = new List <Vector3>(); Attractor currentSOI = null; int lineIndex = -1; //TODO: count rotation around each body and stop drawing the line if it passes a set threshold float lastAngle = calcAngle(0, att.getOrbitingBody()); float totalAngle = 0; bool stop = false; for (int i = 0; i < att.prediction.Count; i += linePointInc) { if (currentSOI != att.prediction[i].strongestAttractor || i == att.prediction.Count - 1 || stop) { setLineVerts(lineIndex, positions); ++lineIndex; validateLineIndex(lineIndex); if (positions.Count != 0) { if (currentSOI != att.prediction[i].strongestAttractor) { WidgetDisplay.getWidgetDisplay().DisplayWidget(TransferNodeWidget, positions[positions.Count - 1], new Vector2(widgetSize, widgetSize), lineColors[lineIndex % lineColors.Length].startColor); } //draw apoapsis and periapsis OrbitProperties properties = att.getOrbitProperties(currentSOI, i - positions.Count); if (properties != null) { Vector2 apoapsisPos = att.prediction[Attracted.getPredictionIndexFromFutureSteps(properties.stepsToApoapsis)]; Vector2 periapsisPos = att.prediction[Attracted.getPredictionIndexFromFutureSteps(properties.stepsToPeriapsis)]; apoapsisPos = currentSOI.transformPositionReletiveSteps(apoapsisPos, Attracted.getFutureStepsFromPredictionIndex(properties.stepsToApoapsis)); periapsisPos = currentSOI.transformPositionReletiveSteps(periapsisPos, Attracted.getFutureStepsFromPredictionIndex(properties.stepsToPeriapsis)); if (properties.stepsToApoapsis != 0) { WidgetDisplay.getWidgetDisplay().DisplayWidget(ApoapsisWidget, apoapsisPos, new Vector2(widgetSize, widgetSize), lineColors[(lineIndex - 1) % lineColors.Length].startColor); } if (properties.stepsToPeriapsis != 0) { WidgetDisplay.getWidgetDisplay().DisplayWidget(PeriapsisWidget, periapsisPos, new Vector2(widgetSize, widgetSize), lineColors[(lineIndex - 1) % lineColors.Length].startColor); } } } currentSOI = att.prediction[i].strongestAttractor; positions.Clear(); lastAngle = calcAngle(i, currentSOI); totalAngle = 0; if (stop) { break; } } Vector2 pos = currentSOI.transformPositionReletiveSteps(att.prediction[i], Attracted.getFutureStepsFromPredictionIndex(i)); if (i == att.prediction.Count - 1 && att.IntersectionDetected) { WidgetDisplay.getWidgetDisplay().DisplayWidget(CollisionWidget, pos, new Vector2(widgetSize, widgetSize), Color.red); } float angle = calcAngle(i, currentSOI); if (Mathf.Abs(angle - lastAngle) < Mathf.PI) { totalAngle += Mathf.Abs(angle - lastAngle); } lastAngle = angle; if (totalAngle >= (Mathf.PI * 2) * (maxRotationsPerPlanet)) { stop = true; } positions.Add(pos); } //setLineVerts(lineIndex, positions); clearAllLinesAfterIndex(lineIndex - 1); }
public OrbitProperties getOrbitProperties(Attractor orbitingBody, int startingIndex) { if (orbitingBody == null) { return(null); } int soiEnterIndex = startingIndex; bool found = false; for (int i = soiEnterIndex; i < prediction.Count; ++i) { if (prediction[i].strongestAttractor == orbitingBody) { soiEnterIndex = i; found = true; break; } } if (!found) { // we do not pass through this attractors soi in the prediction return(null); } int periapsisIndex = soiEnterIndex; bool periapsisFound = false; int apoapsisIndex = soiEnterIndex; bool apoapsisFound = false; float distSQ1 = 0; float distSQ2 = 0; float distSQ3 = 0; //find apoapsis and periapsis for (int i = soiEnterIndex; i < prediction.Count; ++i) { if (distSQ1 != 0) { if ((periapsisFound && apoapsisFound) || prediction[i].strongestAttractor != orbitingBody) { break; } if (distSQ1 > distSQ2 && distSQ2 < distSQ3) { //periapsis found periapsisFound = true; periapsisIndex = i - 1; } if (!apoapsisFound) { apoapsisIndex = i - 1; if (distSQ1 < distSQ2 && distSQ2 > distSQ3) { //apoapsis found apoapsisFound = true; } } } distSQ1 = distSQ2; distSQ2 = distSQ3; distSQ3 = Vector2.SqrMagnitude(prediction[i] - orbitingBody.getPosInPhysicsSteps(getFutureStepsFromPredictionIndex(i))); } OrbitProperties properties = new OrbitProperties(); properties.stepsToApoapsis = getFutureStepsFromPredictionIndex(apoapsisIndex); properties.stepsToPeriapsis = getFutureStepsFromPredictionIndex(periapsisIndex); properties.ApoapsisHeight = Vector2.Distance(prediction[apoapsisIndex], orbitingBody.getPosInPhysicsSteps(getFutureStepsFromPredictionIndex(apoapsisIndex))); properties.PeriapsisHeight = Vector2.Distance(prediction[periapsisIndex], orbitingBody.getPosInPhysicsSteps(getFutureStepsFromPredictionIndex(periapsisIndex))); properties.orbitingBody = orbitingBody; if (!apoapsisFound) { properties.stepsToApoapsis = 0; } if (!periapsisFound) { properties.stepsToPeriapsis = 0; } return(properties); }