Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Need help with procedural generation

Discussion in 'Scripting' started by Noxbuds, Jul 6, 2015.

  1. Noxbuds

    Noxbuds

    Joined:
    Mar 17, 2015
    Posts:
    57
    I thought that was only supposed to happen at (2^31) or -(2^31)
     
  2. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    No, imprecision of floating point numbers is always a problem, more the further you get from 0. You're probably thinking of the limits for signed integers.
     
  3. Noxbuds

    Noxbuds

    Joined:
    Mar 17, 2015
    Posts:
    57
    But it's especially a problem if it's that bad at 500k already.
     
    chelnok likes this.
  4. chelnok

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    680
    It starts to show already around 10k / 20k.


    Jump to 4 min marker, there is explanation for your problem.
     
    landon912 likes this.
  5. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    That's a very good video.

    You can't have infinite precision in 32 bits. I don't know if you can find an arbitrary precision library compatible with Unity, but they will probably end up taking a lot of space in memory.

    But yeah, do the thing in the video. It may be more of a hassle, but it'll be worth it in the long run.
     
  6. Noxbuds

    Noxbuds

    Joined:
    Mar 17, 2015
    Posts:
    57
    OK. Thank you, I will do that.
     
  7. Noxbuds

    Noxbuds

    Joined:
    Mar 17, 2015
    Posts:
    57
    OK so I have done some work on this. Firstly, for brainstorming, I created a project in Scratch:
    https://scratch.mit.edu/projects/70031266/#player

    That's what it's supposed to be. My system is generating everything but the co-ordinates correctly. The co-ordinates are almost there, but not spiraling, just going out in straight lines.
    Code (CSharp):
    1.  
    2.         for (int i = 0; i < galaxy.armcount; i++) {
    3.        
    4.             c_x = 0;
    5.             c_z = 0;
    6.        
    7.             c_rotation += galaxy.armcount/swirl_value;
    8.        
    9.             for (int j = 0; j < c_armlength; j++) {
    10.            
    11.                 c_x += (int)(Mathd.Cos (c_rotation) * 11); //110
    12.                 c_z += (int)(Mathd.Sin (c_rotation) * 11); //110
    13.            
    14.                 galaxy.arm_waypoints[i,j] = new Vector3d (c_x+x_offset,y_offset,c_z+z_offset);
    15.            
    16.                 c_rotation += (galaxy.armcount/c_armlength)*swirl_value;
    17.            
    18.                 //log positions
    19.                 Debug.Log ("X: " + c_x + " Y: " + c_z);
    20.            
    21.             }
    22.        
    23.         }
    The values it logs are so (input in the Scratch project, that's what it should look like):
    Arm Length: 6
    Arms: 31 (I realise 31 arms in a galaxy may be unrealistic)
    Swirl value (bend_factor in my Scratch project): 4

    This is what it creates:
    upload_2015-7-10_17-24-7.png
     
  8. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    I'd probably switch your loops, so you're doing one "disc" at a time, moving outwards, and rotating that disc a bit each time. It could also make it a bit more intuitive if you do it in polar coordinates, and translate them later.
     
  9. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    I don't think you understand that you will have to write an entire physics wrapper/simulation once you get out of Int32 ranges. That is a massive undertaking. Once again, just one solar system, in astronomical scale an invisible dust mite on a dust mite on a dust mite, brought an incredibly skilled team to their knees at times. These numbers you present are absolutely ridiculous in real terms.
     
  10. Noxbuds

    Noxbuds

    Joined:
    Mar 17, 2015
    Posts:
    57
    Nope. I didn't.
    Oh well. I can keep Int32. I'll upgrade if I absolutely need to
     
  11. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    You should watch the entire Kerbal Space Center video.

    They present solutions to many of the problems they ran into while simulating our local solar system. What you propose would be tenfold.
     
    Last edited: Jul 11, 2015
  12. Noxbuds

    Noxbuds

    Joined:
    Mar 17, 2015
    Posts:
    57
    I've been working on my galaxy generator. I managed to break it and I am unsure what the problem is.
    It says this:
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. GalaxyGenerator.GenerateGalaxy (Vector3 input_position) (at Assets/Scripts/GalaxyGenerator.cs:233)
    3. GalaxyGenerator.Start () (at Assets/Scripts/GalaxyGenerator.cs:78)

    Here are the 2 areas causing errors:
    Code (CSharp):
    1. GalacticChunk galaxy_1 = GenerateGalaxy(new Vector3 (-250, -250, -250));
    Code (CSharp):
    1.     Galaxy GenerateGalaxy(Vector3 input_position) {
    2.  
    3.         Galaxy c_galaxy = new Galaxy ();
    4.         c_galaxy.min = input_position;
    5.         c_galaxy.max = new Vector3 (input_position.x+500, input_position.y+500, input_position.z+5000);
    6.  
    7.         Vector3 position = new Vector3 ((input_position.x + 250), (input_position.y + 250), (input_position.z + 250));
    8.    
    9. --->    int x_offset = generator.generate_int (2, (int)position.y, (int)position.z);
    10.         int y_offset = generator.generate_int (2, (int)position.x, (int)position.z);
    11.         int z_offset = generator.generate_int (2, (int)position.x, (int)position.y);
    12.  
    13.  
    It says I haven't set input_position as an initialised Vector3 when I have

    EDIT: It didn't mean Vector3. I rewrote the sequence of generating, and forgot to initialise the number generator. *facepalm*
     
    Last edited: Jul 13, 2015