public Vector2 CalculateForce(SimulatedObject targetObject) { var targetObjectPos = targetObject.position; var selfPos = simulatedObject.position; var r2 = (targetObjectPos - selfPos).sqrMagnitude; var forceDirection = (selfPos - targetObjectPos).normalized; var forceMagnitude = m_simulationManager.gravitationalConstant * simulatedObject.mass * targetObject.mass / r2; return(forceDirection * forceMagnitude); }
private void SimulateStep(SimulatedObject target) { if (m_simulationRegion && !m_simulationRegion.IsInside(target.position)) { return; } var force = Vector2.zero; foreach (var planet in m_planets.bodies) { force += planet.CalculateForce(target); } target.ApplyForce(force); }
private void CreateTruePath(SimulatedObject target) { var dt = Time.fixedDeltaTime; var time = 0.0f; // m_targetPath = ScriptableObject.CreateInstance<TruePath>(); m_player.BeginSimulation(); if (!Application.isPlaying) { foreach (var planet in FindObjectsOfType <CelestialBody>()) { planet.RegisterBody(); } } m_targetPath.Clear(); RecordState(target, time); while (time < m_maxTime) { SimulateStep(target); time += dt; target.UpdateObject(dt); RecordState(target, time); } if (!Application.isPlaying) { foreach (var planet in FindObjectsOfType <CelestialBody>()) { planet.UnregisterBody(); } } m_player.EndSimulation(); Debug.Log($"Created path with {m_targetPath.GetPositions().Length} points"); }
private void RecordState(SimulatedObject target, float time) { m_targetPath.AddPoint(target, time); }
public void AddPoint(SimulatedObject target, float time) { m_points.Add(new PathPoint(target.position, target.velocity, time)); }