Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Oct 18, 2011 at 06:09 PM by DavidDebnar for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by DavidDebnar · Jul 14, 2011 at 05:30 PM · procedural meshplanetmultidimarray

3D array problem

Hey guys... I have a problem I suddenly ran into... Arrays can't go lover than 0! Well it was stupid from me but still... I have an 3D array and the code asks it to go like planet[-1,0,0] and it crashes.. The code is below... Well how the hell can I make it else? All I wanted was just to make a planet/sphere made of cubes, with similar rendering to Minepackage, like if next cube is air/0 than make that face.. But than I ran into this array problem...Should I just leave the planets, or is there any other way? I generate the planet from the center and than just loop until it is a sphere. Than I make if cube's distance from the center is lower than radius*2 set it to air block - because of atmosphere - if it's in radius set it to dirt/1 for the actual planet. Any ideas?

  • David

import System.Collections.Generic;

var rebuild : boolean = false;

//var radius : int = Mathf.Round(Random.Range(30,500)); var radius : int = 50; var size = radius*2; var planet = new int[100,100,100];

function Start() { GeneratePlanet(); }

function GeneratePlanet() { var x : int = -size; var y : int = -size; var z : int = -size;

 while(x <= size - 1)
 {
     while(y <= size - 1)
     {
         while(z <= size - 1)
         {
         
             var dist = Vector3.Distance((transform.position + Vector3(x,y,z)), transform.position);
             print(Vector3(x,y,z));
             if(dist <= radius * 2)
             {
                 planet[x,y,z] = 0;
                 
                 if(dist <= radius)
                 {
                     planet[x,y,z] = 1;
                 }
             }
             z++;
         }
         z= -size;
         y++;
     }
     z=-size;
     y=-size;
     x++;
 }
 rebuild = true;
 BuildPlanet();

}

function BuildPlanet() { var planetMesh : Mesh = GetComponent(MeshFilter).mesh; var planetVertices = new List.();

 for(var x : int = 0; x <= size; x++)
 {
     for(var y : int = 0; y <= size; y++)
     {
         for(var z : int = 0; z <= size; z++)
         {
             if(planet[x,y,z] != 0 && planet[x,y,z] != null)
             {
                 AddBlockToPlanet(x,y,z, planetVertices);
             }
         }
     }
 }
 
 planetMesh.Clear();
 planetMesh.vertices = planetVertices.ToArray();
 planetMesh.RecalculateBounds();
 rebuild = false;

}

function AddBlockToPlanet(x : int, y : int, z : int, planetVertices : List.) { if(planet[x+1,y,z] == 0) { AddBlockSideToPlanet( x+ .5, y -.5, z+.5, x+ .5, y -.5, z -.5, x+ .5, y+.5, z+.5, x+ .5, y+.5, z -.5, planetVertices); } if(planet[x-1,y,z] == 0) { AddBlockSideToPlanet( x- .5, y -.5, z+.5, x- .5, y -.5, z -.5, x- .5, y+.5, z+.5, x- .5, y+.5, z -.5, planetVertices); } if (planet[x,y+1,z] == 0) { AddBlockSideToPlanet( x+ .5, y+.5, z+.5, x+ .5, y+.5, z -.5, x- .5, y+ .5, z+.5, x- .5, y+ .5, z -.5, planetVertices); } if (planet[x,y-1,z] == 0) { AddBlockSideToPlanet( x+ .5,y -.5, z+.5, x+ .5,y -.5, z -.5, x- .5, y-.5, z+.5, x- .5, y-.5, z -.5, planetVertices); } if (planet[x,y,z+1] == 0) { AddBlockSideToPlanet( x+ .5,y-.5,z+.5, x- .5,y-.5,z +.5, x+ .5, y+.5, z+.5, x- .5, y+.5, z +.5, planetVertices); } }

function AddBlockSideToPlanet(x1 : int, y1 : int, z1 : int, x2 : int, y2 : int, z2 : int, x3 : int, y3 : int, z3 : int, x4 : int, y4 : int, z4 : int, planetVertices : List.) { planetVertices.Add(new Vector3(x1+transform.position.x, y1+transform.position.y, z1+transform.position.y)); planetVertices.Add(new Vector3(x2+transform.position.x, y2+transform.position.y, z2+transform.position.y)); planetVertices.Add(new Vector3(x3+transform.position.x, y3+transform.position.y, z3+transform.position.y)); planetVertices.Add(new Vector3(x4+transform.position.x, y4+transform.position.y, z4+transform.position.y)); }

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chris D · Jul 14, 2011 at 05:32 PM 0
Share

Have you tried shifting your starting point to halfway through your array?

avatar image DavidDebnar · Jul 14, 2011 at 06:03 PM 0
Share

Well, but than I can't get the center from which I'm getting the distance

avatar image DavidDebnar · Jul 14, 2011 at 08:50 PM 0
Share

I have an idea... Ill try to realise it tomorrow

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Terrain+Planet 1 Answer

Starforge type gameplay 1 Answer

Efficient massive heightmap storage 0 Answers

Creating random planet textures 3 Answers

How to make camera face to 2D planet as player moves around it. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges