Exemple #1
0
    // Update is called once per frame
    void FixedUpdate()
    {
        for (int i = 0; i < numPlanets; i++)
        {
            // float time = .005f;
            // float celestialMass = planets[i].getMass();
            // Vector3 velocity = planets[i].getVelocity();
            // GameObject celestialGameObject = planetsGameObjects[i];
            // Vector3 originalPos = celestialGameObject.transform.position;
            // Vector3 acceleration = (calcForce(originalPos, celestialMass, starPoisiton, currentStar.getMass())/celestialMass);
            // celestialGameObject.transform.position += (velocity * time + .5f * acceleration * time * time);
            // Vector3 newPosition = celestialGameObject.transform.position;
            // velocity = (newPosition - originalPos) / time;
            // planets[i].setVelocity(velocity);

            Vector3 newVelocity = updateVelocity(planets[i], planetsGameObjects[i], currentStar, starPoisiton, false);
            planets[i].setVelocity(newVelocity);

            for (int j = 0; j < planets[i].getNumMoons(); j++)
            {
                EllipseOrbit ellipse = orbits[i, j];
                ellipse.setCenter(planetsGameObjects[i].transform.position);
                Quaternion q      = Quaternion.AngleAxis(ellipse.getTheta(), Vector3.forward);
                float      angle  = (float)ellipse.getTime() / (float)resolution * 2.0f * Mathf.PI;
                Vector3    newPos = new Vector3(ellipse.getA() * Mathf.Cos(angle), ellipse.getB() * Mathf.Sin(angle), ellipse.getCenter().z);
                moonsGameObjects[i, j].transform.position = q * newPos + ellipse.getCenter();
                ellipse.incrementTime();
            }
        }
    }
Exemple #2
0
        void ApplyOrbits(List <Planet> planetsInSystem)
        {
            Planet prevPlanet        = null;
            float  prevBaseOrbitSize = SunObject.transform.localScale.x;

            while (planetsInSystem.Count > 0)
            {
                Planet planet = planetsInSystem[Random.Range(0, planetsInSystem.Count)];
                planetsInSystem.Remove(planet);

                float offset          = prevPlanet != null ? prevPlanet.WorldSize + planet.WorldSize : Random.Range(GameSettings.MinInitOffset, GameSettings.MaxInitOffset);
                float baseOrbitOffset = prevBaseOrbitSize + offset + Random.Range(0, GameSettings.MaxPlanetOrbitOffset);

                EllipseOrbit orbit = GenerateOrbit(baseOrbitOffset);
                prevBaseOrbitSize = baseOrbitOffset;

                planet.SetOrbit(orbit, Random.Range(0f, 1f));
                planet.StartMotion();

                prevPlanet = planet;
            }
        }
Exemple #3
0
 public void SetOrbit(EllipseOrbit orbit, float initOrbitPosition)
 {
     //Motion
     m_Motion = GetComponent <EllipseMotion>();
     m_Motion.Init(transform, orbit, m_OrbitPeriod, initOrbitPosition);
 }
Exemple #4
0
    void Start()
    {
        minMoonMass = (int)(minPlanetMass * moonMassSacle);
        maxMoonMass = (int)(maxPlanetMass * moonMassSacle);

        x = gameInfoObject.GetComponent <GameInfo>().x;
        y = gameInfoObject.GetComponent <GameInfo>().y;
        z = gameInfoObject.GetComponent <GameInfo>().z;
        GameObject star = Instantiate(starPrefab, starPoisiton, Quaternion.identity);

        ushort posY = (ushort)y;
        ushort posZ = (ushort)z;

        nLehmer       = (long)x << 32 | (long)posY << 16 | (long)posZ;
        randGenerator = new Lehmer(nLehmer);

        bool starExists = randGenerator.randomInt(0, 15) == 1;

        scale      = randGenerator.randomInt(1, 100) / 100f;
        numPlanets = (int)randGenerator.randomInt(0, maxNumPlanets);
        // numPlanets = 1;

        planetsGameObjects = new GameObject[numPlanets];
        rigidBodies        = new Rigidbody[numPlanets];
        planets            = new Planet[numPlanets];
        moons            = new Moon[numPlanets, maxNumMoons];
        moonsGameObjects = new GameObject[numPlanets, maxNumMoons];
        orbits           = new EllipseOrbit[numPlanets, maxNumMoons];



        for (int i = 0; i < numPlanets; i++)
        {
            int     maxPlanetScaleToInt = (int)(maxPlanetScale * 100);
            float   planetScale         = (maxPlanetScaleToInt - 1 - (uint)randGenerator.randomInt(1, maxPlanetScaleToInt - 1)) / 100f;
            Vector3 planetPosition      = new Vector3(randGenerator.randomInt(-maxPlanetRadiusDistance, maxPlanetRadiusDistance),
                                                      randGenerator.randomInt(-maxPlanetRadiusDistance, maxPlanetRadiusDistance),
                                                      randGenerator.randomInt(-maxPlanetRadiusDistance, maxPlanetRadiusDistance));

            GameObject planet = Instantiate(planetPrefab, planetPosition, Quaternion.identity);
            planetsGameObjects[i] = planet;
            planets[i]            = new Planet();
            // startVelocity = (starPoisiton - planetPosition).normalized;
            // planets[i].setVelocity(startVelocity);
            planets[i].setMass((float)randGenerator.randomInt(minPlanetMass, maxPlanetMass));

            //MOOOOOOOOOOOOOOOOOOOOON
            numMoons = (int)randGenerator.randomInt(0, maxNumMoons);
            planets[i].setNumMoons(numMoons);



            for (int j = 0; j < numMoons; j++)
            {
                Vector3 moondistance = new Vector3(randGenerator.randomInt(-maxMoonRadiusDistance, maxMoonRadiusDistance),
                                                   randGenerator.randomInt(-maxMoonRadiusDistance, maxMoonRadiusDistance),
                                                   randGenerator.randomInt(-maxMoonRadiusDistance, maxMoonRadiusDistance));

                Vector3 moonPosition = moondistance + planetPosition;

                float minMagnitude = moonPosition.magnitude + planetRadius + 1;
                float maxMagnitude = minMagnitude + maxVectorMagFromPlanet;
                float mag          = (float)randGenerator.randomInt((int)minMagnitude, (int)maxMagnitude);
                float b            = mag / 2;

                Vector3 ellipseEndpoint = (mag * -1 * moondistance) + moonPosition;
                Vector3 midPoint        = (b * -1 * moonPosition) + moonPosition;

                Vector3 normal = moondistance;
                Vector3 tangent;
                Vector3 t1 = Vector3.Cross(normal, Vector3.forward);
                Vector3 t2 = Vector3.Cross(normal, Vector3.up);
                if (t1.magnitude > t2.magnitude)
                {
                    tangent = t1;
                }
                else
                {
                    tangent = t2;
                }

                float a = (float)randGenerator.randomInt((int)minMagnitude, (int)maxMagnitude);
                orbits[i, j] = new EllipseOrbit(a * .2f, b * .2f, midPoint);
                orbits[i, j].setTheta((float)randGenerator.randomInt(0, 90));



                GameObject moon = Instantiate(moonPrefab, moonPosition, Quaternion.identity);
                moonsGameObjects[i, j] = moon;
                moons[i, j]            = new Moon();
                //moons[i, j].setVelocity(startVelocity);
                moons[i, j].setMass((float)randGenerator.randomInt(minMoonMass, maxMoonMass));
            }
        }
    }