• 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: 

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Question about distant objects 3 Answers

Gravitational Rotation 4 Answers

Planetary perlin noise? My oceans are too patchy. 2 Answers

Mapping Cube to Sphere - Using 64 planes as a cube "side" 1 Answer

Have rigid body enter a planets orbit but always be able to escape it 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges