void GeneratePlanets() { if (planets.Count < 1 || planets[0].planetPrefab == null) { Debug.LogError("请保证有一个行星数据并且有prefab依赖信息"); return; } var templatePrefab = planets[0].planetPrefab; planets.Clear(); for (var i = 0; i < planetCount; i++) { var planet = new Planet(); planet.planetPrefab = templatePrefab; var orbit = new OrbitalEllipse(); orbit.xExtent = Random.Range(30, 50); orbit.yExtent = Random.Range(30, 50); orbit.tilt = Random.Range(0, 70); planet.orbit = orbit; planet.hasMoon = Random.Range(1, 100) > 50; planet.rotationSpeed = Random.Range(10, 100); planet.orbitDuration = Random.Range(3, 20); planets.Add(planet); } }
// Update the state of the orbiter when its position along the ellipse changes // Note: Make sure the ellipse is up to date before updating the orbit state private void Update(float orbiterAngle, Orbiter orbiter, OrbitalEllipse ellipse) { this.orbiter = orbiter; this.ellipse = ellipse; this.normal = CalcNormal(orbiterAngle); this.tangent = CalcTangent(normal); this.position = ellipse.GetPosition(orbiterAngle, orbiter.orbitAround.position); this.velocity = CalcVelocity(orbiter.orbitSpeed * orbiter.GravityConstant(), position, orbiter.orbitAround.position); }
// Setup the orbit when the is added private void Reset() { if (orbitAround == null) { return; } ellipse = new OrbitalEllipse(orbitAround.position, transform.position, apsisDistance, circularOrbit); apsisDistance = ellipse.endingApsis; // Default to a circular orbit by setting both apses to the same value }
void DrawOrbitalElipse(LineRenderer line, OrbitalEllipse ellipse) { Vector3[] drawPoints = new Vector3[elipseSegments + 1]; for (int i = 0; i < elipseSegments; i++) { drawPoints[i] = ellipse.Evaluate(i / (elipseSegments - 1f)); } drawPoints[elipseSegments] = drawPoints[0]; line.useWorldSpace = false; line.positionCount = elipseSegments + 1; line.startWidth = elipseWidth; line.SetPositions(drawPoints); }
private void OnDrawGizmosSelected() { if (orbitAround == null) { return; } // This is required for the OrbitRenderer. For some reason the ellipse var is always null // if it's set anywhere else, even including OnApplicationQuit; if (ellipse == null) { ellipse = new OrbitalEllipse(orbitAround.position, transform.position, apsisDistance, circularOrbit); } // Never allow 0 apsis. Start with a circular orbit. if (apsisDistance == 0) { apsisDistance = ellipse.startingApsis; } }
//==== Instance Methods ====// // Constructor public OrbitState(float angle, Orbiter orbiter, OrbitalEllipse ellipse) { Update(angle, orbiter, ellipse); }
private void OnApplicationQuit() { ellipse = new OrbitalEllipse(orbitAround.position, transform.position, apsisDistance, circularOrbit); }