public static void CalculateBothPrediction() { if (BothAttr == null || BothAttr.Count == 0 || BothAttr[0].validPrediction) { return; } for (int i = 0; i < numPre; i += stepInc) { for (int j = 0; j < BothAttr.Count; ++j) { //calculate prediction step including the movement of attractor objects if (i == 0) { BothAttr[j].calc_pos = BothAttr[j].transform.position; BothAttr[j].calc_vel = BothAttr[j].rb.velocity; BothAttr[j].prediction.Clear(); BothAttr[j].validPrediction = true; BothAttr[j].stepsSinceLastPrediction = 0; } AttractorReturn force = Attractor.getFutureAttractionVectorSteps(BothAttr[j], BothAttr[j].calc_pos, i); BothAttr[j].calc_vel += (force.force / BothAttr[j].rb.mass) * (Time.fixedDeltaTime * stepInc); BothAttr[j].calc_pos += BothAttr[j].calc_vel * (Time.fixedDeltaTime * stepInc); BothAttr[j].prediction.Add(new PredictionStep(BothAttr[j].calc_pos, BothAttr[j].calc_vel, force.strongest)); } } }
public void CalculatePrediction() { if (validPrediction) { //already calculated return; } CalculateBothPrediction(); if (myAttractor == null) { int startIndex = 0; Vector2 pos; Vector2 vel; if (!invalidPrediction && prediction.Count != 0) { if (stepsSinceLastPrediction < stepInc) { prediction[0].pos = transform.position; return; } //else if (stepInc != 1) { //prediction.RemoveAt(0); //} } int preindex = stepsSinceLastPrediction / stepInc; if (!invalidPrediction && prediction.Count != 0 && preindex < prediction.Count && ((prediction.Count != (numPre / stepInc) && !IntersectionDetected) || (Vector2.SqrMagnitude(prediction[preindex - 1] - rb.position) <= errorThresholdSquared))) { startIndex = prediction.Count - preindex; pos = prediction[prediction.Count - 1].pos; vel = prediction[prediction.Count - 1].vel; prediction.RemoveRange(0, preindex); } else { prediction.Clear(); pos = rb.position; vel = rb.velocity; } IntersectionDetected = false; for (int i = startIndex * stepInc, count = 0; i < numPre && count < maxPredictionsPerUpdate; i += stepInc, ++count) { //calculate prediction step AttractorReturn force = Attractor.getFutureAttractionVectorSteps(this, pos, i); vel += (force.force / rb.mass) * (Time.fixedDeltaTime * stepInc); pos += vel * (Time.fixedDeltaTime * stepInc); //check for intersection with a planet foreach (Attractor att in Attractor.attractors) { float distSquared = Vector2.SqrMagnitude(pos - att.getPosInPhysicsSteps(i)); float attScale = att.transform.localScale.x / 2; if (distSquared <= attScale * attScale) { IntersectionDetected = true; break; } } if (IntersectionDetected) { break; } prediction.Add(new PredictionStep(pos, vel, force.strongest)); } } validPrediction = true; invalidPrediction = false; stepsSinceLastPrediction = 0; }