Gravity Engine 12.0
Unity Asset for Gravity
Loading...
Searching...
No Matches
GravityScaler Class Reference

Handles scaling between: GE: internal values used in the physics integrators Scene: values in Unity units (raw Unity position values and game time seconds) World: The chosen units for values in inspector entries etc. (e.g. AU, km, m) SI: Metric system values. More...

Inheritance diagram for GravityScaler:

Public Types

enum  Units { DIMENSIONLESS , SI , ORBITAL , SOLAR }
 

Static Public Member Functions

static void Init ()
 
static double UpdateMassScale (Units units, float timeScale, float lengthScale)
 The integrators in GE do not provide Newton's constant G as part of the force calculation.
 
static float GetGameSecondPerPhysicsSecond ()
 
static float ScalePeriod (float period)
 
static float ScaleToGameSeconds (float time)
 Scale a physics time to Unity game seconds.
 
static float GameSecToWorldTime (float gameSec)
 
static Vector3 ScaleAcceleration (Vector3 a, float lengthScale, float timeScale)
 Modify the physics engine acceleration to appear in world scale units.
 
static double AccelerationScaleInternalToGEUnits ()
 
static float GetVelocityScale ()
 Factor to convert GE units (SOLAR, ORBITAL) to GE internal velocity units.
 
static void ScaleScene (Units units, float lengthScale)
 Changes the length scale of all NBody objects in the scene due to a change in the inspector. Find all NBody containing objects.
 
static float ScaleDistancePhyToScene (float distance)
 Convert a distance in GE physics units to scene (Unity) units.
 
static Vector3d ScaleVector3dPhyToScene (Vector3d v)
 Convert a distance in GE physics units to scene (Unity) units.
 
static Vector3 ScalePositionSceneToPhys (Vector3 pos)
 
static void ScaleNBody (NBody nbody, Units units, float lengthScale)
 Scales the N body using the provided units and length scale.
 
static Vector3 ScaleVelSceneToPhys (Vector3 vel)
 Scale the velocity in Unity scene units (e.g. suitable for a rigidbody) into GE interal units.
 
static Vector3d ScaleVelSceneToPhys (Vector3d vel)
 
static Vector3 ScaleGEVelocityToSI (Vector3 v)
 
static Vector3d ScaleGEVelocityToSI (Vector3d v)
 
static Vector3 ScaleGEPositionToSI (Vector3 v)
 
static Vector3d ScaleGEPositionToSI (Vector3d v)
 
static Vector3 ScaleVelPhysToScene (Vector3 vel)
 Scale the velocity from internal phys units to scene units.
 
static double ScaleVelPhysMagnitudeToScene (double vel)
 
static Vector3d ScaleVelPhysToScene (Vector3d vel)
 
static double AccelSItoGEUnits ()
 Provide conversion factor to convert acceleration in SI units to the GE units in use by Gravity Engine.
 
static double AccelGEtoSIUnits ()
 Determine the factor to convert acceleration to SI units.
 
static double PositionScaletoSIUnits ()
 Determine the factor to convert scaled position to SI units.
 
static double VelocityScaletoSIUnits ()
 Determine the factor to convert scaled velocity to SI units.
 
static double AccelGEtoInternalUnits ()
 Provide the conversion factor to convert acceleration from GE units (e.g. km/hr^2 if ORBITAL) into the internal units. The internal units are determined based on the timeScale and lengthScale chosen.
 
static string LengthUnits (Units units)
 Return the string indicating the length units in use by the gravity engine.
 
static string VelocityUnits (Units units)
 Return the string indicating the velocity units in use by the gravity engine.
 
static string MassUnits (Units units)
 Return the string indicating the mass units in use by the gravity engine.
 
static string GetWorldTimeFormatted (double physTime, Units units)
 Return the world time in the selected units as a string.
 
static System.TimeSpan GetTimeSpan (double physTime, Units units)
 Return the time as a C# TimeSpan object. Used in code to show Calendar date for the evolution. Typically in SOLAR or ORBITAL units.
 
static double GetWorldTimeSeconds (double physTime)
 Return the world time in the selected units as a double.
 
static double WorldSecsToPhysTime (double worldSecs)
 
static double WorldTimeToPhysTime (double worldTime)
 Convert the world time in GravityEngine units to GE internal phys time.
 

Public Attributes

Units units
 

Static Public Attributes

const float G = 6.67408E-11f
 Units. Gravity N-Body simulations use units in which G=1 (G, the gravitational constant).
 
const float M_PER_KM = 1000f
 
const float M_PER_AU = 1.49598E+11f
 
const float SEC_PER_YEAR = 3600f * 24f * 365.25f
 
const float KM_HOUR_TO_M_SEC = M_PER_KM / SEC_PER_HOUR
 
const float M_SEC_TO_KM_HOUR = 1 / KM_HOUR_TO_M_SEC
 
const float SECS_PER_SIDEREAL_DAY = 86164.1f
 
const float FT_TO_M = 0.3048f
 
const float FTSEC_TO_KMHR = 1.09728f
 
const float KM_SEC_TO_AU_YR = 0.210949527f
 
const float SEC_PER_HOUR = 3600f
 
const float SEC_PER_DAY = 3600 * 24f
 
static double game_sec_per_phys_sec = 1f
 

Detailed Description

Handles scaling between: GE: internal values used in the physics integrators Scene: values in Unity units (raw Unity position values and game time seconds) World: The chosen units for values in inspector entries etc. (e.g. AU, km, m) SI: Metric system values.

The GE inspector allows for four choices of units, with different inspector inputs in each case: -DIMENSIONLESS:

  • mass scale -SI: (m/kg/sec)
  • Unity units per m (lengthScale)
  • Game sec. per SI sec. (timeScale) -ORBITAL (km/1E24 kg/hour)
  • Unity units per km (lengthScale)
  • game sec. per world hour (timeScale) -SOLAR (AU/1E24 kg/year)
  • Unity units per AU (lengthScale)
  • game sec. per world year (timeScale)

Components that specifiy initial positions (e.g. OrbitEllipse) interpret the values provided as World units and convert them to internal GE units when they are added to GE. A position specified for a NBody in the inspector will indicate the active choice of units and set the NBody initialPos field. Scripts can set this field in world units directly.

GravityEngine.lengthScale specifies the Unity units per World unit value. For dimensionless units it is 1.

GravityEngine.timescale specifies game seconds per World time unit. For dimensionless units it is 1.

The internal units for mass and time do not in general align with any of the world unit choices. This is a consequence of choosing to implement the gravity calculations in which the gravitational constant G=1 (this represents a specific choice of internal units, since we are always free to scale units to make G=1). This adjustment is made by UpdateTimeScale. Length is not changed, so length in World units match internal lengths (scene position differs by lengthScale).

Scaling When Adding an NBody

position: (initialPos is specified in the NBody inspector in World units) nbody.initialPhyPos = nbody.initialPos * lengthScale; r = nbody.initialPhyPos/ phyToWorldFactor; (phyToWorldFactor allows dynamic scaling from a value in the GE inspector)

velocity: vel_phys = nbody.vel * velocityScale;

mass: m = nbody.mass * massScale;

mass_scale is set by UpdateMassScale

Member Function Documentation

◆ AccelGEtoInternalUnits()

static double GravityScaler.AccelGEtoInternalUnits ( )
static

Provide the conversion factor to convert acceleration from GE units (e.g. km/hr^2 if ORBITAL) into the internal units. The internal units are determined based on the timeScale and lengthScale chosen.

This routine references the currently set units in the GE

Returns
Conversion factor

◆ AccelGEtoSIUnits()

static double GravityScaler.AccelGEtoSIUnits ( )
static

Determine the factor to convert acceleration to SI units.

Returns

◆ AccelSItoGEUnits()

static double GravityScaler.AccelSItoGEUnits ( )
static

Provide conversion factor to convert acceleration in SI units to the GE units in use by Gravity Engine.

This routine references the currently set units in the GE

Returns
Conversion Factor

◆ GetTimeSpan()

static System.TimeSpan GravityScaler.GetTimeSpan ( double  physTime,
Units  units 
)
static

Return the time as a C# TimeSpan object. Used in code to show Calendar date for the evolution. Typically in SOLAR or ORBITAL units.

Parameters
physTime
units
Returns

◆ GetVelocityScale()

static float GravityScaler.GetVelocityScale ( )
static

Factor to convert GE units (SOLAR, ORBITAL) to GE internal velocity units.

vel_phys = nbody.vel * velocityScale;

Returns

◆ GetWorldTimeFormatted()

static string GravityScaler.GetWorldTimeFormatted ( double  physTime,
Units  units 
)
static

Return the world time in the selected units as a string.

DateTime reports 0 time as year 1, so that is the starting time in SOLAR and ORBITAL units.

Returns

◆ GetWorldTimeSeconds()

static double GravityScaler.GetWorldTimeSeconds ( double  physTime)
static

Return the world time in the selected units as a double.

Returns

◆ LengthUnits()

static string GravityScaler.LengthUnits ( Units  units)
static

Return the string indicating the length units in use by the gravity engine.

Returns
The units.

◆ MassUnits()

static string GravityScaler.MassUnits ( Units  units)
static

Return the string indicating the mass units in use by the gravity engine.

Returns
The units.

◆ PositionScaletoSIUnits()

static double GravityScaler.PositionScaletoSIUnits ( )
static

Determine the factor to convert scaled position to SI units.

Returns

◆ ScaleAcceleration()

static Vector3 GravityScaler.ScaleAcceleration ( Vector3  a,
float  lengthScale,
float  timeScale 
)
static

Modify the physics engine acceleration to appear in world scale units.

Parameters
a
lengthScale
timeScale
Returns

◆ ScaleDistancePhyToScene()

static float GravityScaler.ScaleDistancePhyToScene ( float  distance)
static

Convert a distance in GE physics units to scene (Unity) units.

Parameters
distance
Returns

◆ ScaleNBody()

static void GravityScaler.ScaleNBody ( NBody  nbody,
Units  units,
float  lengthScale 
)
static

Scales the N body using the provided units and length scale.

Parameters
nbodyNbody.
unitsUnits.
timeScaleTime scale.
lengthScaleLength scale.

◆ ScaleScene()

static void GravityScaler.ScaleScene ( Units  units,
float  lengthScale 
)
static

Changes the length scale of all NBody objects in the scene due to a change in the inspector. Find all NBody containing objects.

  • independent objects are rescaled
  • orbit based objects have their primary dimension adjusted (e.g. for ellipse, a) (these objects are scalable and are asked to rescale themselves)

Not intended for run-time use.

◆ ScaleToGameSeconds()

static float GravityScaler.ScaleToGameSeconds ( float  time)
static

Scale a physics time to Unity game seconds.

Parameters
time
Returns

◆ ScaleVector3dPhyToScene()

static Vector3d GravityScaler.ScaleVector3dPhyToScene ( Vector3d  v)
static

Convert a distance in GE physics units to scene (Unity) units.

Parameters
distance
Returns

◆ ScaleVelPhysToScene()

static Vector3 GravityScaler.ScaleVelPhysToScene ( Vector3  vel)
static

Scale the velocity from internal phys units to scene units.

Mapping of scene units depends on the unit choice in GE and the "Unity unit per km/AU" value set for GE.

Parameters
vel
Returns

◆ ScaleVelSceneToPhys()

static Vector3 GravityScaler.ScaleVelSceneToPhys ( Vector3  vel)
static

Scale the velocity in Unity scene units (e.g. suitable for a rigidbody) into GE interal units.

Mapping of scene units depends on the unit choice in GE and the "Unity unit per km/AU" value set for GE.

Parameters
vel
Returns

◆ UpdateMassScale()

static double GravityScaler.UpdateMassScale ( Units  units,
float  timeScale,
float  lengthScale 
)
static

The integrators in GE do not provide Newton's constant G as part of the force calculation.

This is absorbed into a massScale that is applies to all masses as they are added to the internal mass recorded in GravityState. In the case of dimensionful units this can be regarded as "pre-multiplying by G", where G is adjusted based on the units and the scaling of length and time indicated in the GE inspector settings.

This routine also assigns game_sec_per_phys_sec.

Parameters
units
timeScale
lengthScale
Returns

◆ VelocityScaletoSIUnits()

static double GravityScaler.VelocityScaletoSIUnits ( )
static

Determine the factor to convert scaled velocity to SI units.

Returns

◆ VelocityUnits()

static string GravityScaler.VelocityUnits ( Units  units)
static

Return the string indicating the velocity units in use by the gravity engine.

Returns
The units.

◆ WorldTimeToPhysTime()

static double GravityScaler.WorldTimeToPhysTime ( double  worldTime)
static

Convert the world time in GravityEngine units to GE internal phys time.

Parameters
worldTime
Returns

Member Data Documentation

◆ G

const float GravityScaler.G = 6.67408E-11f
static

Units. Gravity N-Body simulations use units in which G=1 (G, the gravitational constant).

From unit analysis of F = m_1 a = (G m_1 m_2)/r^2 we get: T = (L^3/G M)^(1/2) with T = time, L = length, M = Mass, G=Newton's constant = 6.67E-11.

SI (m,kg) => T=1.224E5 sec Solar (AU, 1E24 kg) => T = 7.083E9 sec.

To control game time, mass is rescaled in the physics engine to acheive the desired result. Initial velocities are also adjusted to appropriate scale.


The documentation for this class was generated from the following file: