Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help with Physics Voxel

Discussion in 'Scripting' started by Rhyusaky, Apr 18, 2016.

  1. Rhyusaky

    Rhyusaky

    Joined:
    Jan 22, 2016
    Posts:
    25
    Hello, I am creating a voxel physics engine (In fact, one it between the existing and created), and good, as could do it, what I need is the following:
    It test if a voxel this piece disconnected from the ground;
    It destroys these voxels and creates cubes in place (they will have RigidBodies);
    Take note that I am using Cubiquity;

    Until now I have this code for this, and it works well, to some extent, his problems are:
    It leaves only fall when the voxel this all alone;
    It weighs enough when you have a large plot;
    Here's the code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using Cubiquity;
    5.  
    6. public class CubicPhysics : MonoBehaviour {
    7.     public ColoredCubesVolume Terrain;
    8.     public GameObject Block;
    9.     public float TimePassed;
    10.     private CubicTerrainGenerator Script;
    11.     private Vector3i Size;
    12.     // Use this for initialization
    13.     void Start () {
    14.         Script = gameObject.GetComponent<CubicTerrainGenerator>();
    15.         Size = Script.TerrainSize;
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.         TimePassed =+ TimePassed+Time.deltaTime;
    21.         if(TimePassed >= 5) {
    22.          TestBlocks();
    23.             Directions.Clear();
    24.          TimePassed = 0;
    25.         }
    26.     }
    27.     void TestBlocks() {
    28.         for(int i=0;i<Size.x;i++)
    29.             for(int j=0;j<256;j++)
    30.                 for(int k=0;k<Size.z;k++)
    31.             {
    32.                 if(Terrain.data.GetVoxel(i,j,k).red != 0 && Terrain.data.GetVoxel(i,j,k).green != 0 && Terrain.data.GetVoxel(i,j,k).blue != 0 && Terrain.data.GetVoxel(i,j,k).alpha != 0) {
    33.                     TestPhysics(i,j,k);
    34.                 }
    35.             }
    36.     }
    37.     void TestPhysics(int X,int Y,int Z) {
    38.         if(Terrain.data.GetVoxel(X-1,Y,Z).red == 0 && Terrain.data.GetVoxel(X-1,Y,Z).blue == 0 && Terrain.data.GetVoxel(X-1,Y,Z).green == 0 && Terrain.data.GetVoxel(X-1,Y,Z).alpha == 0) {
    39.             if(Terrain.data.GetVoxel(X+1,Y,Z).red == 0 && Terrain.data.GetVoxel(X+1,Y,Z).blue == 0 && Terrain.data.GetVoxel(X+1,Y,Z).green == 0 && Terrain.data.GetVoxel(X+1,Y,Z).alpha == 0) {
    40.                 if(Terrain.data.GetVoxel(X,Y+1,Z).red == 0 && Terrain.data.GetVoxel(X,Y+1,Z).blue == 0 && Terrain.data.GetVoxel(X,Y+1,Z).green == 0 && Terrain.data.GetVoxel(X,Y+1,Z).alpha == 0) {
    41.                     if(Terrain.data.GetVoxel(X,Y-1,Z).red == 0 && Terrain.data.GetVoxel(X,Y-1,Z).blue == 0 && Terrain.data.GetVoxel(X,Y-1,Z).green == 0 && Terrain.data.GetVoxel(X,Y-1,Z).alpha == 0) {
    42.                         if(Terrain.data.GetVoxel(X,Y,Z+1).red == 0 && Terrain.data.GetVoxel(X,Y,Z+1).blue == 0 && Terrain.data.GetVoxel(X,Y,Z+1).green == 0 && Terrain.data.GetVoxel(X,Y,Z+1).alpha == 0) {
    43.                             if(Terrain.data.GetVoxel(X,Y,Z-1).red == 0 && Terrain.data.GetVoxel(X,Y,Z-1).blue == 0 && Terrain.data.GetVoxel(X,Y,Z-1).green == 0 && Terrain.data.GetVoxel(X,Y,Z-1).alpha == 0) {
    44.                                 Terrain.data.SetVoxel(X,Y,Z,new QuantizedColor(0,0,0,0));
    45.                                 GameObject.Instantiate(Block,new Vector3(X,Y,Z),Quaternion.identity);
    46.                             }
    47.                         }
    48.                     }
    49.                 }
    50.             }
    51.         }
    52.     }
    53. }
    54.  
    Can anyone help me fix it, show another way to do it, or at least show me the way?