public static CelestialVector3 Cross(CelestialVector3 a, CelestialVector3 b) { double cx = a.y * b.z - a.z * b.y; double cy = a.z * b.x - a.x * b.z; double cz = a.x * b.y - a.y * b.x; return(new CelestialVector3(cx, cy, cz)); }
public override List <Vector3> GetOrbit(double currentJulianDate, double resolution) { double orbitalPeriodInDays = GetOrbitalPeriod(); double julianDaysPerPosition = (orbitalPeriodInDays / resolution); List <Vector3> orbit = new List <Vector3>(); // Start at 1/2 orbit in the past double julianDate = currentJulianDate - (orbitalPeriodInDays * 0.5); double radiusVector; double eclipticalLongitude; double eclipticLatitude; GetHeliocentricEclipticalCoordinates(julianDate, out radiusVector, out eclipticalLongitude, out eclipticLatitude); // In case a planet makes it in here without an orbit, abort if (radiusVector != 0.0f) { double initialRotation = eclipticalLongitude; double difference = 0.0; bool closing = false; while (true) { double newDifference = Math.Abs(initialRotation - eclipticalLongitude); if (closing) { if (newDifference > difference) { break; } } else if (newDifference < difference) { closing = true; } difference = newDifference; CelestialVector3 position = PlanetPositionUtility.GetPositionFromHeliocentricEclipticalCoordinates(radiusVector, eclipticalLongitude, eclipticLatitude); orbit.Add((Vector3)(position / GlobalConstants.CelestialUnit)); julianDate += julianDaysPerPosition; GetHeliocentricEclipticalCoordinates(julianDate, out radiusVector, out eclipticalLongitude, out eclipticLatitude); } } return(orbit); }
private void UpdateShipPosition() { // Update the spaceship's position if (null != m_BaseBody && null != m_SpaceShip) { CelestialVector3 offset = m_Position + (m_Position.Normalized() * m_BaseBody.Radius); m_SpaceShip.Position = m_BaseBody.Position - offset; m_BasePosition = m_SpaceShip.Position; } }
public void UpdateDynamicScale(CelestialVector3 basePosition) { // Update the dynamic scalar values of all Celestial Bodies using given base position foreach (KeyValuePair <uint, CelestialBody> celestialBodyRecord in m_CelestialBodies) { CelestialBody celestialBody = celestialBodyRecord.Value; CelestialVector3 position = new CelestialVector3(); double scale; GetScaledPosition(basePosition, celestialBody.Position, out position, out scale); float radius = (float)(scale * celestialBody.Radius); float diameter = 2.0f * radius; celestialBody.transform.localScale = new Vector3(diameter, diameter, diameter); celestialBody.transform.localPosition = (Vector3)position; } }
public override void UpdatePosition(double julianDate) { CelestialVector3 position = CalculatePosition(julianDate); LocalPosition = position; if (OrbitParentID != 0) { CelestialBody parentBody = CelestialManagerPhysical.Instance.GetCelestialBody(OrbitParentID); if (parentBody != null) { position += parentBody.Position; } } Position = position; }
private void UpdatePosition(CelestialBody body) { CelestialVirtual virtualBody = body as CelestialVirtual; if (null != virtualBody) { CelestialBody celestialBody = CelestialManagerPhysical.Instance.GetCelestialBody(virtualBody.OwnerID); if (null != celestialBody) { CelestialPlanetoid planetoid = celestialBody as CelestialPlanetoid; if (null != planetoid) { CelestialVector3 position = planetoid.CalculatePosition(CelestialTime.Instance.Current); virtualBody.LocalPosition = position; if (body.OrbitParentID != 0) { CelestialBody parentBody = GetCelestialBody(body.OrbitParentID); if (parentBody != null) { position += parentBody.Position; } } virtualBody.Position = position; } else { // Set the virtual position to match the real planet's virtualBody.LocalPosition = celestialBody.LocalPosition; virtualBody.Position = celestialBody.Position; } } else { Debug.LogWarning("Unable to update position of " + body.name); } } }
// Give the planets position in real data and get back the scaled version private void GetScaledPosition(CelestialVector3 basePosition, CelestialVector3 celestialPosition, out CelestialVector3 scaledPosition, out double scale) { scale = 1.0; scaledPosition = celestialPosition - basePosition; double distance = scaledPosition.Length(); double _cosfov2 = 1.0 - System.Math.Cos(m_FieldOfView * 0.5); double f = m_FarClipPlane * _cosfov2; double p = 0.75 * f; if (distance > p) { double s = 1.0 - System.Math.Exp(-p / (distance - p)); double dist = p + (f - p) * s; scale = dist / distance; scaledPosition = scaledPosition / distance * dist; } }
public static CelestialVector3 GetPositionFromHeliocentricEclipticalCoordinates(double radiusVector, double eclipticalLongitude, double eclipticLatitude) { // radiusVector is in AU units // If we were using only floats then all we would need to do here is this: // *Notice* how we need to scale by astronomical units to keep data in range // Vector3 position = new Vector3( (float)( radiusVector * GlobalConstants.AstronomicalUnit ), 0, 0 ); // Quaternion rotation = Quaternion.Euler( 0, -(float)eclipticalLongitude, -(float)eclipticLatitude ); // return position * rotation; // Instead we are using doubles so we need to do this manually // We do not need to scale the data using this method CelestialVector3 position = new CelestialVector3((radiusVector * GlobalConstants.AstronomicalUnit), 0, 0); Quaternion rotation = Quaternion.Euler(0, -(float)eclipticalLongitude, -(float)eclipticLatitude); // Algorithm for converting Euler angles to a quaternion //public void rotate( double heading, double attitude, double bank ) //{ // // Assuming the angles are in radians. // double c1 = Math.cos( heading / 2 ); // double s1 = Math.sin( heading / 2 ); // double c2 = Math.cos( attitude / 2 ); // double s2 = Math.sin( attitude / 2 ); // double c3 = Math.cos( bank / 2 ); // double s3 = Math.sin( bank / 2 ); // double c1c2 = c1 * c2; // double s1s2 = s1 * s2; // w = c1c2 * c3 - s1s2 * s3; // x = c1c2 * s3 + s1s2 * c3; // y = s1 * c2 * c3 + c1 * s2 * s3; // z = c1 * s2 * c3 - s1 * c2 * s3; //} // Algorithm for rotation a vector by a quaternion //void rotate_vector_by_quaternion(const Vector3&v, const Quaternion&q, Vector3 & vprime) //{ // // Extract the vector part of the quaternion // Vector3 u( q.x, q.y, q.z); // // // Extract the scalar part of the quaternion // float s = q.w; // // // Do the math // vprime = 2.0f * dot( u, v ) * u // + ( s * s - dot( u, u ) ) * v // + 2.0f * s * cross( u, v ); //} // The algorithm can be simplified to this double halfDegreesToRadians = GlobalConstants.DegreesToRadians / 2; double c1 = Math.Cos(-eclipticalLongitude * halfDegreesToRadians); double s1 = Math.Sin(-eclipticalLongitude * halfDegreesToRadians); double c2 = Math.Cos(-eclipticLatitude * halfDegreesToRadians); double s2 = Math.Sin(-eclipticLatitude * halfDegreesToRadians); double w = c1 * c2; CelestialVector3 u = new CelestialVector3(s1 * s2, s1 * c2, c1 * s2); CelestialVector3 part1 = u * (2.0 * CelestialVector3.Dot(u, position)); CelestialVector3 part2 = position * (w * w - CelestialVector3.Dot(u, u)); CelestialVector3 part3 = CelestialVector3.Cross(u, position) * (2.0 * w); return(part1 + part2 + part3); }
protected override void SetPosition(CelestialVector3 position) { base.SetPosition(position); transform.position = (Vector3)(position / GlobalConstants.CelestialUnit); }
// This is the position in space using real units protected virtual void SetPosition(CelestialVector3 position) { m_PositionInKM = position; //transform.localPosition = (Vector3)position; }
// Dot Product //V1.x* V2.x + V1.y* V2.y + V1.z* V2.z //Cross Product //cx = aybz− azby //cy = azbx− axbz //cz = axby− aybx public static double Dot(CelestialVector3 a, CelestialVector3 b) { return(a.x * b.x + a.y * b.y + a.z * b.z); }