Search Unity

64 Bit Integer?

Discussion in 'World Building' started by MattJohnson2, Oct 25, 2018.

  1. MattJohnson2

    MattJohnson2

    Joined:
    Oct 25, 2018
    Posts:
    1
    Hi all, so I'm making a KSP style game with Unity, but one problem is that I can't go more than 1 million meters away from the origin, as the floating point errors start kicking in. All I'm wanting to know is, is there any way that I can trick Unity into using 64 bit integers so that I can make my virtual universe?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I don't see what integers have to do with it. What you want is 64-bit floats, i.e. type double. You can't make Unity use those, but you can use them in all your own code. You'll also need to recenter things periodically so all the action (i.e. the camera and objects the player can see nearby) are always near the origin.

    The KSP developers actually gave a talk on all this a few years ago. I'm sure with some googling you can find it.
     
  3. Deleted User

    Deleted User

    Guest

    I use this script, but its work only for x and y

    Code (CSharp):
    1. // FloatingOrigin.cs
    2. // Written by Peter Stirling
    3. // 11 November 2010
    4. // Uploaded to Unify Community Wiki on 11 November 2010
    5. // Updated to Unity 5.x particle system by Tony Lovell 14 January, 2016
    6. // fix to ensure ALL particles get moved by Tony Lovell 8 September, 2016
    7. // URL: http://wiki.unity3d.com/index.php/Floating_Origin
    8. using UnityEngine;
    9. using System.Collections;
    10.  
    11. [RequireComponent(typeof(Camera))]
    12. public class FloatingOrigin : MonoBehaviour
    13. {
    14.     public float threshold = 1000.0f;
    15.     public float physicsThreshold = 10000.0f; // Set to zero to disable
    16.  
    17. #if OLD_PHYSICS
    18.     public float defaultSleepVelocity = 0.14f;
    19.     public float defaultAngularVelocity = 0.14f;
    20. #else
    21.     public float defaultSleepThreshold = 0.14f;
    22. #endif
    23.  
    24.     ParticleSystem.Particle[] parts = null;
    25.  
    26.     void LateUpdate()
    27.     {
    28.         Vector3 cameraPosition = gameObject.transform.position;
    29.         cameraPosition.y = 0f;
    30.         if (cameraPosition.magnitude > threshold)
    31.         {
    32.             Object[] objects = FindObjectsOfType(typeof(Transform));
    33.             foreach (Object o in objects)
    34.             {
    35.                 Transform t = (Transform)o;
    36.                 if (t.parent == null)
    37.                 {
    38.                     t.position -= cameraPosition;
    39.                 }
    40.             }
    41.  
    42. #if SUPPORT_OLD_PARTICLE_SYSTEM
    43.             // move active particles from old Unity particle system that are active in world space
    44.             objects = FindObjectsOfType(typeof(ParticleEmitter));
    45.             foreach (Object o in objects)
    46.             {
    47.                 ParticleEmitter pe = (ParticleEmitter)o;
    48.                 // if the particle is not in world space, the logic above should have moved them already
    49.         if (!pe.useWorldSpace)
    50.             continue;
    51.                 Particle[] emitterParticles = pe.particles;
    52.                 for(int i = 0; i < emitterParticles.Length; ++i)
    53.                 {
    54.                     emitterParticles[i].position -= cameraPosition;
    55.                 }
    56.                 pe.particles = emitterParticles;
    57.             }
    58. #endif
    59.  
    60.             // new particles... very similar to old version above
    61.             objects = FindObjectsOfType(typeof(ParticleSystem));
    62.             foreach (UnityEngine.Object o in objects)
    63.             {
    64.                 ParticleSystem sys = (ParticleSystem)o;
    65.  
    66.                 if (sys.simulationSpace != ParticleSystemSimulationSpace.World)
    67.                     continue;
    68.  
    69.                 int particlesNeeded = sys.maxParticles;
    70.  
    71.                 if (particlesNeeded <= 0)
    72.                     continue;
    73.  
    74.                 bool wasPaused = sys.isPaused;
    75.                 bool wasPlaying = sys.isPlaying;
    76.  
    77.                 if (!wasPaused)
    78.                     sys.Pause();
    79.  
    80.                 // ensure a sufficiently large array in which to store the particles
    81.                 if (parts == null || parts.Length < particlesNeeded)
    82.                 {
    83.                     parts = new ParticleSystem.Particle[particlesNeeded];
    84.                 }
    85.  
    86.                 // now get the particles
    87.                 int num = sys.GetParticles(parts);
    88.  
    89.                 for (int i = 0; i < num; i++)
    90.                 {
    91.                     parts[i].position -= cameraPosition;
    92.                 }
    93.  
    94.                 sys.SetParticles(parts, num);
    95.  
    96.                 if (wasPlaying)
    97.                     sys.Play();
    98.             }
    99.  
    100.             if (physicsThreshold > 0f)
    101.             {
    102.                 float physicsThreshold2 = physicsThreshold * physicsThreshold; // simplify check on threshold
    103.                 objects = FindObjectsOfType(typeof(Rigidbody));
    104.                 foreach (UnityEngine.Object o in objects)
    105.                 {
    106.                     Rigidbody r = (Rigidbody)o;
    107.                     if (r.gameObject.transform.position.sqrMagnitude > physicsThreshold2)
    108.                     {
    109. #if OLD_PHYSICS
    110.                         r.sleepAngularVelocity = float.MaxValue;
    111.                         r.sleepVelocity = float.MaxValue;
    112. #else
    113.                         r.sleepThreshold = float.MaxValue;
    114. #endif
    115.                     }
    116.                     else
    117.                     {
    118. #if OLD_PHYSICS
    119.                         r.sleepAngularVelocity = defaultSleepVelocity;
    120.                         r.sleepVelocity = defaultAngularVelocity;
    121. #else
    122.                         r.sleepThreshold = defaultSleepThreshold;
    123. #endif
    124.                     }
    125.                 }
    126.             }
    127.         }
    128.     }
    129. }
    130.  
    131. /*
    132. Addendum from DulcetTone on 22 April 2018: a user named Marcos-Elias sent me a message with an optimization he found helpful on recent versions of Unity which include the new "SceneManager" functionality.
    133. He suggests replacing this fragment of my code:
    134. Object[] objects = FindObjectsOfType(typeof(Transform));
    135.       foreach(Object o in objects)
    136.       {
    137.           Transform t = (Transform)o;
    138.           if (t.parent == null)
    139.           {
    140.              t.position -= cameraPosition;
    141.           }
    142.       }
    143. with the following code, to avoid having to process ALL objects to find the root objects
    144. for (int z=0; z < SceneManager.sceneCount; z++) {
    145.        foreach (GameObject g in SceneManager.GetSceneAt(z).GetRootGameObjects()) {
    146.            g.transform.position -= cameraPosition;
    147.        }
    148. }
    149. I have not use this myself, as yet, but I wonder if an additional optimation would be the following, to update only the active scene:
    150. foreach (GameObject g in SceneManager.GetActiveScene().GetRootGameObjects()) {
    151.            g.transform.position -= cameraPosition;
    152. }
    153. */
    154.