コード例 #1
0
    public void RestartPaths()
    {
        _cometPathState = _currState.Clone();

        _cometPaths = new Queue <Vector3> [_cometPathState.comets.Count];
        for (int i = 0; i < _cometPaths.Length; i++)
        {
            _cometPaths[i] = new Queue <Vector3>();
        }
    }
コード例 #2
0
    private void createSimulation()
    {
        _simTime = 0;
        if (_currState != null || _prevState != null)
        {
            destroySimulation();
        }

        _currState         = new SolarSystemState();
        _currState.sunMass = _sunMass;
        _currState.gravivationalConstant = _gravitationalConstant;

        //Generate the planets for the solar system
        for (int i = 0; i < _planetCount; i++)
        {
            var planet = Instantiate(_planetPrefab);
            planet.transform.parent        = _displayAnchor;
            planet.transform.localPosition = Vector3.zero;
            planet.transform.localRotation = Quaternion.identity;
            planet.transform.localScale    = Vector3.one;

            var planetState = new PlanetState();

            float orbitRadius     = Random.Range(_orbitRadiusRange.x, _orbitRadiusRange.y);
            float mass            = Random.Range(_massRange.x, _massRange.y);
            float revolutionSpeed = Random.Range(_revolutionRange.x, _revolutionRange.y);
            float axisTilt        = _axisTiltDistribution.Evaluate(Random.value);

            float orbitalVelocity = Mathf.Sqrt(_gravitationalConstant * _sunMass / orbitRadius);
            float orbitLength     = Mathf.PI * 2 * orbitRadius;
            float orbitPeriod     = orbitLength / orbitalVelocity;
            float angularSpeed    = Mathf.PI * 2 / orbitPeriod;

            if (Random.value > _chanceToRevolveBackwards)
            {
                revolutionSpeed = -revolutionSpeed;
            }

            float hue        = Random.value;
            float saturation = Random.Range(_saturationRange.x, _saturationRange.y);
            float value      = Random.Range(_valueRange.x, _valueRange.y);

            planetState.angle              = Random.Range(0, Mathf.PI * 2);
            planetState.angularSpeed       = angularSpeed;
            planetState.distanceFromCenter = orbitRadius;
            planetState.mass = mass;

            planet.gameObject.SetActive(true);
            planet.Init(revolutionSpeed, mass, axisTilt, Color.HSVToRGB(hue, saturation, value));

            _currState.planets.Add(planetState);
            _spawnedPlanets.Add(planet);
        }

        //Add starting commets
        for (int i = 0; i < _cometCount; i++)
        {
            var rot = Quaternion.Euler(0, Random.Range(0, 360), 0);
            _currState.comets.Add(new CometState()
            {
                position = rot * (Vector3.right * 0.3f + Vector3.up * 0.05f),
                velocity = rot * (Vector3.forward * 0.4f)
            });
        }

        _prevState = _currState.Clone();

        //Generate planet orbit mesh
        {
            foreach (var planet in _currState.planets)
            {
                for (int i = 0; i < _planetOrbitResolution; i++)
                {
                    _tempLines.Add(i + _tempVerts.Count);
                    _tempLines.Add((i + 1) % _planetOrbitResolution + _tempVerts.Count);
                }

                for (int i = 0; i < _planetOrbitResolution; i++)
                {
                    float angle = Mathf.PI * 2 * i / (float)_planetOrbitResolution;
                    float dx    = Mathf.Cos(angle) * planet.distanceFromCenter;
                    float dz    = Mathf.Sin(angle) * planet.distanceFromCenter;
                    _tempVerts.Add(new Vector3(dx, 0, dz));
                }
            }

            if (_planetOrbitMesh == null)
            {
                _planetOrbitMesh = new Mesh();
            }
            _planetOrbitMesh.Clear();
            _planetOrbitMesh.SetVertices(_tempVerts);
            _planetOrbitMesh.SetIndices(_tempLines.ToArray(), MeshTopology.Lines, 0);

            _tempVerts.Clear();
            _tempLines.Clear();
        }

        //Restart the comet paths
        RestartPaths();

        if (OnCreateSystem != null)
        {
            OnCreateSystem();
        }
    }