private void FixedUpdate()
    {
        if (isSimulating)
        {
            //Calc normal direction
            Vector3 heading   = fixedBody.transform.position - orbitingBody.transform.position;
            float   distance  = heading.magnitude;
            Vector3 direction = heading / distance;

            double gravForce = GRAV_CONSTANT * ((massA * massB) / Mathf.Pow(distance, 2));

            //Display normal and forward vectors
            lrForVector.SetPosition(0, transform.position);
            lrForVector.SetPosition(1, orbitingBody.transform.InverseTransformDirection(orbitingBody.rb.velocity));

            //Direction vector between planetoid and "sun"
            lrNorVector.SetPosition(0, transform.position);
            lrNorVector.SetPosition(1, direction * (float)gravForce);

            //Add acceleration force to orbiting object
            orbitingBody.rb.AddForce(direction * (float)gravForce, ForceMode.Acceleration);

            //Update current infos
            float speed = orbitingBody.GetComponent <Rigidbody>().velocity.magnitude;

            cnvLvlManager.UpdateTxtCurDistance($"Distance : {distance.ToString("0.00")}");
            cnvLvlManager.UpdateTxtCurVelocity($"Vitesse : {speed.ToString("0.00")}");
            cnvLvlManager.UpdateTxtCurGravForce($"Force Grav. : {gravForce.ToString("0.00")}");
        }
    }
Example #2
0
    public void SetUpBody(GenerationSettings.PlanetSettings planetSettings, bool setPositions = true, CelestialBody newOrbitalParent = null)
    {
        // this is called when a celestial body is wanted to be set up as a planet

        GenericSetup(planetSettings);

        drawPath   = planetSettings.drawPath;
        pathLength = planetSettings.pathLength;

        if (setPositions)
        {
            // place the planet away from the star
            Vector3 direction = new Vector3(Mathf.Cos(planetSettings.offsetAngle * Mathf.Deg2Rad), 0, Mathf.Sin(planetSettings.offsetAngle * Mathf.Deg2Rad));
            transform.position = newOrbitalParent.transform.position + (direction * planetSettings.distance);
        }

        // set up the initial velocity
        // so that the planet will fall into a stable orbit around its parent
        // we do this even if we don't want to reset the position to calculate the effect
        // of any changes to the orbit eccentricity
        if (newOrbitalParent != null)
        {
            initialVelocity   = planetSettings.eccentricity * CalculateInitialVelocity(newOrbitalParent.transform);
            orbitalParent     = newOrbitalParent;
            orbitalParentMass = orbitalParent.GetComponent <CelestialBody>().mass;
        }
    }
 private void RefreshView()
 {
     _isRefreshing = true;
     gameObject.SetActive(_currentTarget != null);
     if (_currentTarget != null)
     {
         FollowToggle.isOn         = IsFollowingCurrentTarget();
         NameText.text             = _currentTarget.name;
         AttractorNameText.text    = _currentTarget.AttractorRef == null ? "[null]" : _currentTarget.AttractorRef.name;
         VelocityValue.Value       = (float)_currentTarget.Velocity.magnitude;
         RelVelocityValue.Value    = (float)_currentTarget.RelativeVelocity.magnitude;
         MassText.text             = _currentTarget.Mass.ToString(_currentTarget.Mass > 10 ? "0" : "0.00");
         MassSlider.value          = (float)_currentTarget.Mass;
         IsKeplerMotionToggle.isOn = _currentTarget.UseKeplerMotion;
         IsDrawOrbitToggle.isOn    = _currentTarget.GetComponent <OrbitDisplay>().isActiveAndEnabled;
         if (_currentTarget.OrbitData.IsValidOrbit)
         {
             InfoEccentricityText.text = "Eccentricity      = " + _currentTarget.OrbitData.Eccentricity.ToString("0.00");
             InfoPeriodText.text       = "Period               = " + _currentTarget.OrbitData.Period.ToString("0.00");
             InfoDistanceText.text     = "Distance           = " + _currentTarget.OrbitData.Position.magnitude.ToString("0.00");
             InfoPeriapsisText.text    = "Periapsis          = " + _currentTarget.OrbitData.PeriapsisDistance.ToString("0.00");
             InfoMeanAnomalyText.text  = "Mean anomaly = " + (_currentTarget.OrbitData.MeanAnomaly * Mathd.Rad2Deg).ToString("0.00");
         }
         else
         {
             InfoEccentricityText.text = "Eccentricity      = 0";
             InfoPeriodText.text       = "Period               = 0";
             InfoDistanceText.text     = "Distance           = 0";
             InfoPeriapsisText.text    = "Periapsis          = 0";
             InfoMeanAnomalyText.text  = "Mean anomaly = 0";
         }
     }
     _isRefreshing = false;
 }
Example #4
0
        public TiltBodyLoader(CelestialBody body)
        {
            if (body?.scaledBody == null || Injector.IsInPrefab)
            {
                throw new InvalidOperationException("The body must be already spawned by the PSystemManager.");
            }

            Value = body.GetComponent <TiltedBody>();
            if (Value == null)
            {
                Value = body.gameObject.AddComponent <TiltedBody>();
            }
        }
            public WormholeLoader(CelestialBody body)
            {
                // Is this a spawned body?
                if (body?.scaledBody == null || Injector.IsInPrefab)
                {
                    throw new InvalidOperationException("The body must be already spawned by the PSystemManager.");
                }

                // Store values
                Value = body.GetComponent <WormholeComponent>();
                if (Value == null)
                {
                    Value = body.gameObject.AddComponent <WormholeComponent>();
                }
            }
    private void findClossestBody()
    {
        foreach (CelestialBody body in universe.CelestialBodies)
        {
            if (ClossestBody == null ||
                ((transform.position - body.transform.position).sqrMagnitude < (transform.position - ClossestBody.transform.position).sqrMagnitude))
            {
                if (ClossestBody != body)
                {
                    ClossestBody       = body;
                    _clossestBodyOrbit = ClossestBody.GetComponent <FixedOrbit>();
                }
            }
        }

        checkCelestialOrbit();
    }
Example #7
0
            public SpaceCenterLoader(CelestialBody body)
            {
                // Is this a spawned body?
                if (body?.scaledBody == null || Injector.IsInPrefab)
                {
                    throw new InvalidOperationException("The body must be already spawned by the PSystemManager.");
                }

                // Store values
                Value = body.GetComponent <KSC>();
                if (Value == null)
                {
                    Value = body.gameObject.AddComponent <KSC>();
                    Value.Start();
                    UnityEngine.Object.DontDestroyOnLoad(Value);
                }
            }
Example #8
0
        public GrassMaterialLoader(CelestialBody body)
        {
            // Is this a spawned body?
            if (body.scaledBody == null || Injector.IsInPrefab)
            {
                throw new InvalidOperationException("The body must be already spawned by the PSystemManager.");
            }

            // Store values
            KSC ksc = body.GetComponent <KSC>();

            if (ksc != null)
            {
                Value = ksc.Material;
                if (Value != null)
                {
                    return;
                }
                Value = ksc.Material = new KSC.GrassMaterial();
            }
        }
Example #9
0
    public void Generate()
    {
        Random.seed = seed;

        for (int i = 0; i < maxAsteroids; i++)
        {
            Vector3 randomPos = Random.insideUnitSphere * maxRadius;
            if (randomPos.magnitude > exclusionRadius)
            {
                GameObject asteroid = Instantiate(asteroidPrefab);
                asteroid.transform.parent   = transform;
                asteroid.transform.position = randomPos;
                asteroid.transform.rotation = Random.rotation;
                CelestialBody      asteroidBody   = asteroid.GetComponent <CelestialBody>();
                ProceduralCubeBody proceduralBody = asteroidBody.GetComponent <ProceduralCubeBody>();
                proceduralBody.Prepare();
                proceduralBody.Generate();
                proceduralBody.Finish();
            }
        }
    }
Example #10
0
 void Start()
 {
     if (!Target)
     {
         var ship = GameObject.Find("Ship");
         if (ship)
         {
             Target = ship.GetComponentInParent <CelestialBody>();
         }
     }
     _defaultTimeScale = SimulationControl.instance.TimeScale;
     if (timeScaleButtons.Length == 3)
     {
         timeScaleButtons[0].interactable = false;
     }
     if (!ShipControl)
     {
         ShipControl = Target.GetComponent <ShipController>();
     }
     if (!FuelSlider)
     {
         Debug.Log("SpaceGravity2D.ShipGUI: fuel slider ref is null");
     }
     else
     {
         FuelSlider.maxValue = ShipControl.MaxFuel;
         FuelSlider.value    = ShipControl.Fuel;
     }
     if (MarkPrefab)
     {
         _markApoapsis      = Instantiate(MarkPrefab) as Transform;
         _markApoapsis.name = "apoapsisMark";
         _markApoapsis.GetComponentInChildren <Text>().text = "A";
         _markPeriapsis      = Instantiate(MarkPrefab) as Transform;
         _markPeriapsis.name = "periapsisMark";
         _markPeriapsis.GetComponentInChildren <Text>().text = "P";
     }
 }
    CelestialBody SetupCelestialBody(CelestialBody prefab, CelestialBody parent, float distance, float size, float rotationSpeed, float orbitSpeed)      // planet create
    {
        CelestialBody planetInstance = Instantiate(prefab, parent.transform.position + Vector3.forward * distance, transform.rotation) as CelestialBody; //creates the object

        planetInstance.transform.localScale = Vector3.one * size;                                                                                        //sets values
        planetInstance.speed = rotationSpeed;

        GameObject planetStrut = new GameObject("PlanetStrut");//create another object tied to the planet

        planetStrut.transform.position = parent.transform.position;
        CelestialBody planetStrutCeletialBody = planetStrut.AddComponent <CelestialBody>();

        planetStrutCeletialBody.speed   = orbitSpeed;
        planetInstance.transform.parent = planetStrut.transform;
        planetStrut.transform.parent    = parent.transform;


        //*****Creates a trail for the planets......not needed anymore as orbit lines have been drawn
        Color planetColour = new Color(Random.value, Random.value, Random.value);

        TrailRenderer trail = planetInstance.GetComponentInChildren <TrailRenderer>();

        if (trail != null)
        {
            trail.material.color = planetColour;
            trail.startWidth     = planetInstance.transform.lossyScale.magnitude / 2.0f;
            trail.endWidth       = 0.0f;
        }

        planetInstance.GetComponent <Renderer>().material.color = planetColour;
        /********************************************************************/

        //create orbit lines
        SetUpOrbit(parent, planetInstance, planetColour, distance);


        return(planetInstance);
    }
Example #12
0
    public void SetUpBody(GenerationSettings.MoonSettings moonSettings, bool setPositions = true, CelestialBody newOrbitalParent = null)
    {
        // this is called when a celestial body is wanted to be set up as moon

        GenericSetup(moonSettings);

        drawPath   = moonSettings.drawPath;
        pathLength = moonSettings.pathLength;

        if (setPositions)
        {
            // place the moon away from the planet
            Vector3 direction = new Vector3(Mathf.Cos(moonSettings.offsetAngle * Mathf.Deg2Rad), 0, Mathf.Sin(moonSettings.offsetAngle * Mathf.Deg2Rad));
            transform.position = newOrbitalParent.transform.position + (direction * moonSettings.distance);
        }

        // set up the initial velocity to give a stable orbit
        if (newOrbitalParent != null)
        {
            initialVelocity   = moonSettings.eccentricity * CalculateInitialVelocity(newOrbitalParent.transform);
            orbitalParent     = newOrbitalParent;
            orbitalParentMass = orbitalParent.GetComponent <CelestialBody>().mass;
        }
    }