public OrbitMath.OrbitPrediction DynamicPrediction(OrbitMath.OrbitPrediction prediction, float mass = 1) { //Set Prediction to this type prediction.ChangeSystem(this); //Outside of system if (prediction.localPosition.magnitude > radiusOfInfluence && parentSystem) { return(parentSystem.DynamicPrediction(prediction, mass)); } //Inside of child system foreach (GravitySystem childSystem in childSystems) { if (childSystem.IsInSystem(prediction.time, prediction.localPosition)) { return(childSystem.DynamicPrediction(prediction, mass)); } } //Calculate Gravity prediction.localGravity = TotalGravity(prediction.time, prediction.localPosition, mass); return(prediction); }
public void DrawPathA(OrbitMath.OrbitPrediction[] predictions, int curI, int maxI) { OrbitMath.OrbitPrediction enterPrediction = predictions[curI]; Vector2 lastPosition = enterPrediction.gravitySystem.PointToWorld(enterPrediction.time, enterPrediction.localPosition); int switches = 0; for (int steps = 0; steps < ModuloDistance(curI, maxI, predictions.Length) - 1; steps++) { int i = (steps + curI) % predictions.Length; int prevI = (i - 1 + predictions.Length) % predictions.Length; if (predictions[prevI].gravitySystem != predictions[i].gravitySystem) { enterPrediction = predictions[i]; switches++; if (switches > 1) { break; } } Vector2 worldPositionL = enterPrediction.gravitySystem.PointToWorld(enterPrediction.time, predictions[i].localPosition); Vector2 worldPositionW = enterPrediction.gravitySystem.PointToWorld(predictions[i].time, predictions[i].localPosition); Vector2 worldPosition = showLocal ? worldPositionL : worldPositionW; if (predictions[prevI].gravitySystem == predictions[i].gravitySystem) { Debug.DrawLine(lastPosition, worldPosition, enterPrediction.gravitySystem.renderer.color); } lastPosition = worldPosition; } }
public void FixedUpdate() { //Predict path int currentPredictionCount = ModuloDistance(currentIndex, maxIndex, predictions.Length); int newPredictionCount = Mathf.Max(100 - currentPredictionCount, 10); if (ModuloDistance(currentIndex, maxIndex, predictions.Length) < predictions.Length - newPredictionCount - 2) { predictions = PredictPath(predictions, maxIndex, newPredictionCount); maxIndex = (maxIndex + newPredictionCount) % predictions.Length; } // currentIndex = (currentIndex + 1) % predictions.Length; OrbitMath.OrbitPrediction prediction = predictions[currentIndex]; transform.parent = prediction.gravitySystem.transform; transform.localPosition = prediction.localPosition; DrawPath(predictions, currentIndex, maxIndex); Grapher.Log(prediction.localGravity.magnitude, "Gravity", prediction.time); Grapher.Log(prediction.localGravity.magnitude, prediction.gravitySystem.name, prediction.gravitySystem.renderer.color, prediction.time); }
public void PretendAddVelocity(Vector2 velocity) { OrbitMath.OrbitPrediction[] path = new OrbitMath.OrbitPrediction[500]; path[0] = predictions[currentIndex].Clone(); path[0].localVelocity += velocity; path = PredictPath(path, 0, 499); DrawPath(path, 0, 499); }
public void ChangeSystem(GravitySystem newSystem) { // Same System if (gravitySystem == newSystem) { return; } if (!newSystem) { return; } OrbitPrediction newSystemPrediction = newSystem.GetPrediction(time); // From World Space if (!gravitySystem) { if (!newSystem.parentSystem) { //Sun gravitySystem = newSystem; return; } else { // localPosition -= newSystemPrediction.localPosition; localVelocity -= newSystemPrediction.localVelocity; gravitySystem = newSystem; return; } } // From ParentSystem if (gravitySystem == newSystem.parentSystem) { localPosition -= newSystemPrediction.localPosition; localVelocity -= newSystemPrediction.localVelocity; gravitySystem = newSystem; return; } // From Child System if (gravitySystem.parentSystem = newSystem) { OrbitMath.OrbitPrediction childPrediction = gravitySystem.GetPrediction(time); localPosition += childPrediction.localPosition; localVelocity += childPrediction.localVelocity; gravitySystem = newSystem; return; } gravitySystem = newSystem; }
public Vector2 PointFromParentSystem(float time, Vector2 localPosition) { OrbitMath.OrbitPrediction prediction = GetPrediction(time); if (parentSystem) { return(localPosition - prediction.localPosition); } else { return(localPosition); } }
public Vector2 PointToWorld(float time, Vector2 localPosition) { OrbitMath.OrbitPrediction prediction = GetPrediction(time); if (parentSystem) { return(parentSystem.PointToWorld(time, localPosition + prediction.localPosition)); } else { return(localPosition); } }
public void Start() { if (startPrediction != null && startPrediction.gravitySystem) { startPrediction.localPosition = startPrediction.gravitySystem.PointToSystem(0, transform.position); } predictions = new OrbitMath.OrbitPrediction[predictionCount]; for (int i = 0; i < predictions.Length; i++) { predictions[i] = new OrbitMath.OrbitPrediction(); } predictions[0] = startPrediction; }
public OrbitMath.OrbitPrediction CalculateNextPrediction(OrbitMath.OrbitPrediction currentPrediction) { OrbitMath.OrbitPrediction nextPrediction = currentPrediction.Clone(); float deltaTime = Time.fixedDeltaTime; nextPrediction.time += deltaTime; nextPrediction.localVelocity += nextPrediction.localGravity * deltaTime; nextPrediction.localPosition += nextPrediction.localVelocity * deltaTime; //TODO Collision nextPrediction = nextPrediction.gravitySystem.DynamicPrediction(nextPrediction, mass); return(nextPrediction); }
public Vector2 RelativeTimePositionToWorld(OrbitMath.OrbitPrediction curPrediciton, List <OrbitMath.OrbitPrediction> entryPredictions) { if (curPrediciton.gravitySystem.parentSystem == null) { return(curPrediciton.localPosition); } foreach (OrbitMath.OrbitPrediction entryPred in entryPredictions) { if (curPrediciton.gravitySystem == entryPred.gravitySystem) { Vector2 relativePosition = entryPred.gravitySystem.PointToParentSystem(entryPred.time, curPrediciton.localPosition); OrbitMath.OrbitPrediction relativePred = new OrbitMath.OrbitPrediction(curPrediciton.time, relativePosition, curPrediciton.localVelocity); relativePred.gravitySystem = entryPred.gravitySystem.parentSystem; return(RelativeTimePositionToWorld(relativePred, entryPredictions)); } } return(curPrediciton.gravitySystem.PointToWorld(entryPredictions[0].time, curPrediciton.localPosition)); }
public void DrawPath(OrbitMath.OrbitPrediction[] predictions, int curI, int maxI) { // Predictions in the same system are calculated relative to their entry points! List <OrbitMath.OrbitPrediction> entryPredictions = new List <OrbitMath.OrbitPrediction>(); Vector2 lastPosition = predictions[curI].gravitySystem.PointToWorld(predictions[curI].time, predictions[curI].localPosition); for (int steps = 0; steps < ModuloDistance(curI, maxI, predictions.Length) - 1; steps++) { //Get Index int i = (steps + curI) % predictions.Length; int prevI = (i - 1 + predictions.Length) % predictions.Length; // Current Prediction OrbitMath.OrbitPrediction curPrediction = predictions[i]; // Find Relative entry Predicion OrbitMath.OrbitPrediction entryPrediction = null; foreach (OrbitMath.OrbitPrediction pred in entryPredictions) { if (pred.gravitySystem == curPrediction.gravitySystem) { entryPrediction = pred; } } // No entry Prediction Found if (entryPrediction == null) { entryPrediction = curPrediction; entryPredictions.Add(curPrediction); } // Get Relative Position Vector2 relativePosition = RelativeTimePositionToWorld(curPrediction, entryPredictions); if (predictions[prevI].gravitySystem == predictions[i].gravitySystem) { Debug.DrawLine(lastPosition, relativePosition, curPrediction.gravitySystem.renderer.color); } lastPosition = relativePosition; } }
public void FixedUpdate() { OrbitMath.OrbitPrediction prediction = OrbitMath.GetStaticOrbitPrediction(Time.time, this); transform.localPosition = prediction.localPosition; }