Exemple #1
0
        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);
        }
Exemple #2
0
        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);
        }
Exemple #3
0
        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");
        }
Exemple #4
0
 private void RecordState(SimulatedObject target, float time)
 {
     m_targetPath.AddPoint(target, time);
 }
Exemple #5
0
 public void AddPoint(SimulatedObject target, float time)
 {
     m_points.Add(new PathPoint(target.position, target.velocity, time));
 }