//get attraction vector for all objects public static AttractorReturn getAttractionVector(Attracted obj) { Vector2 totalForce = new Vector2(0, 0); if (attractors == null) { return(new AttractorReturn(new Vector2(0, 0), null)); } Attractor strongest = null; float strongestForce = 0; foreach (Attractor a in attractors) { if (obj.gameObject != a.gameObject) { Vector2 force = ((Vector2)a.transform.position - (Vector2)obj.transform.position).normalized; float forceMag = a.getAttractionForce(obj); force *= forceMag; totalForce += force; if (forceMag > strongestForce) { strongestForce = forceMag; strongest = a; } } } return(new AttractorReturn(totalForce, strongest)); }
public static AttractorReturn getFutureAttractionVectorSeconds(Attracted obj, Vector2 pos, float seconds) { Vector2 totalForce = new Vector2(0, 0); if (attractors == null) { return(new AttractorReturn(new Vector2(0, 0), null)); } Attractor strongest = null; float strongestForce = 0; foreach (Attractor a in attractors) { if (obj.gameObject != a.gameObject) { Vector2 force = (a.getPosInSeconds(seconds) - pos).normalized; float forceMag = a.getAttractionForceInFutureSeconds(pos, obj.rb.mass, seconds); force *= forceMag; totalForce += force; if (forceMag > strongestForce) { strongestForce = forceMag; strongest = a; } } } return(new AttractorReturn(totalForce, strongest)); }
//get attraction force from this object public float getAttractionForce(Attracted other) { float massProduct = rb.mass * other.rb.mass; Vector2 pos = transform.position; Vector2 otherPos = other.transform.position; float distanceSq = Vector2.SqrMagnitude(pos - otherPos); return(Gravity.G * (massProduct / distanceSq)); }
private float calcAngle(int index, Attractor orbitingBody) { if (att.prediction.Count == 0) { return(0); } int steps = Attracted.getFutureStepsFromPredictionIndex(index); Vector2 vec = orbitingBody.getPosInPhysicsSteps(steps) - att.prediction[index]; return(Mathf.Atan2(vec.y, vec.x)); }
void Start() { att = GetComponent <Attracted>(); lr = GetComponent <LineRenderer>(); }
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); }
void Start() { att = GetComponent <Attracted>(); }
protected override void Init() { att = GetComponent <Attracted>(); }