Ejemplo n.º 1
0
 void draw_cubes()
 {
     foreach (GameObject _cube in cubes)
     {
         Rect _r1     = RectangleCollisionChecker.BoundsToScreenRect(_cube.GetComponent <Renderer> ().bounds);
         Rect _r1_tex = new Rect(_r1.x, _r1.y, _r1.width, _r1.height);
         TextureDraw.DrawRectangle(tex, _r1_tex, Color.green);
         //TextureDraw.DrawLine(tex, (int)(_r1.x), (int)(_r1.y), (int)(_r1.xMax), (int)(_r1.y), Color.cyan);
         //Rect _r2 = RectangleCollisionChecker.BoundsToScreenRect(_cube.GetComponent<Renderer>().bounds);
         //TextureDraw.DrawLine(tex, (int)(_r1.x), (int)(_r1.y), (int)(_r1.xMax), (int)(_r1.yMax), Color.cyan);
     }
     //tex.Apply();
 }
Ejemplo n.º 2
0
    public bool is_occupied()
    {
        if (b_is_inited == false)
        {
            b_is_occupied = false;
            foreach (GameObject _object in collision_objects)
            {
                Rect _r1 = RectangleCollisionChecker.BoundsToScreenRect(_object.GetComponent <Renderer>().bounds);
                //TextureDraw.DrawRectangle(tex, _r1, Color.yellow);

                if (RectangleCollisionChecker.CircleRectInsersects(new Circle(_r1.center, _r1.width), bounds))                 //if( RectangleCollisionChecker.intersects(bounds, _r1))
                {
                    b_is_occupied = true;
                    break;
                }
            }
        }

        return(b_is_occupied);
    }
Ejemplo n.º 3
0
    void determine_terrain_data()
    {
        //for each subdivision (so thats a for by for loop
        // for each cube (n^3 now i think)
        //see if the cube is within the bounds of this cube, and if it is, set its terrain data to true.
        //break out of the loop if the data is true for this cube

        int _voxel_width      = Screen.width / (resolution);
        int _voxel_height     = Screen.height / (resolution);
        int half_voxel_width  = _voxel_width / 2;
        int half_voxel_height = _voxel_height / 2;


        int _multi_val_x = Screen.width / (resolution);
        int _multi_val_y = Screen.height / (resolution);


        Camera cam = GetComponent <Camera>();

        QuadTreeNode quad_tree = quadtree_for_this_update;
        //first, carve up the region and parition them into areas with stuff
        //given an XxY array


        //now traverse the quad tree and only do checks for areas that have hits in them
        List <Rect> _rects_to_search = quad_tree.pluck_leaves();

        foreach (Rect _rect in _rects_to_search)
        {
            //TextureDraw.DrawRectangle(tex, _rect, Color.yellow);
            float rect_far_horizontal_side = _rect.x + _rect.width;
            float rect_far_vertical_side   = _rect.y + _rect.height;

            int next_horizontal_voxel_index = Mathf.CeilToInt(rect_far_horizontal_side / _voxel_width);
            int next_vertical_voxel_index   = Mathf.CeilToInt(rect_far_vertical_side / _voxel_height);


            int start_x_voxel_index = (int)_rect.x / _voxel_width;

            for (int i = start_x_voxel_index; i < next_horizontal_voxel_index && i < resolution; i++)
            {
                int start_y_voxel_index = (int)_rect.y / _voxel_height;
                for (int j = start_y_voxel_index; j < next_vertical_voxel_index && j < resolution; j++)
                {
                    Rect _r0 = new Rect(new Vector2(i * _multi_val_x, j * _multi_val_y), new Vector2(_multi_val_x, _multi_val_y));
                    //TextureDraw.DrawRectangle(tex, _r0, Color.blue);
                    float _metaball_value = 0;


                    foreach (GameObject _cube in cubes)
                    {
                        if (bool_values[i, j] == true)
                        {
                            break;
                        }

                        Vector3 screen_coords = cam.WorldToScreenPoint(_cube.transform.position);


                        Rect _r1 = RectangleCollisionChecker.BoundsToScreenRect(_cube.GetComponent <Renderer>().bounds);
                        //TextureDraw.DrawRectangle(tex, _r1, Color.yellow);

                        float r = (_r1.width);
                        float _threshold_value = (Mathf.Pow(r, 2)) / (Mathf.Pow((_r1.center.x - _r0.center.x), 2) + Mathf.Pow((_r1.center.y - _r0.center.y), 2));
                        _metaball_value = _threshold_value;                         //set this to just = (or set to += for additive) and you get rid of the METAball dynamics. metaball - when many balls close together glob together to form a new ball (larger surface)

                        //_r1.y = Screen.height - _r1.y;
                        //Rect _r11 = new Rect(new Vector2(screen_coords.x, screen_coords.y), new Vector2(50, 50));
                        //Debug.Log(" bounds _r1 bounds " + _r1.ToString());
                        //Debug.Log(" bounds _r11 " + _r11.ToString());
                        if (_metaball_value >= 1f)                         //if( RectangleCollisionChecker.intersects(_r0, _r1))
                        {
                            bool_values[i, j] = true;
                            break;
                        }
                    }
                }
            }
        }



        /*
         * for(int i = 0; i<  resolution; i++)
         * {
         *      for(int j = 0; j<  resolution; j++)
         *      {
         *              Rect _r0 = new Rect(new Vector2(i * _multi_val_x, j * _multi_val_y), new Vector2(_multi_val_x, _multi_val_y));
         *              TextureDraw.DrawRectangle(tex, _r0, Color.blue);
         *
         *              foreach(GameObject _cube in cubes)
         *              {
         *                      if( bool_values[i, j] ==  true)
         *                              break;
         *
         *
         *                      Vector3 screen_coords = cam.WorldToScreenPoint(_cube.transform.position);
         *
         *                      Rect _r1 = RectangleCollisionChecker.BoundsToScreenRect(_cube.GetComponent<Renderer>().bounds);
         *                      TextureDraw.DrawRectangle(tex, _r1, Color.yellow);
         *                      //_r1.y = Screen.height - _r1.y;
         *                      //Rect _r11 = new Rect(new Vector2(screen_coords.x, screen_coords.y), new Vector2(50, 50));
         *                      //Debug.Log(" bounds _r1 bounds " + _r1.ToString());
         *                      //Debug.Log(" bounds _r11 " + _r11.ToString());
         *                      if( RectangleCollisionChecker.intersects(_r0, _r1))
         *                      {
         *                              bool_values[i, j] =  true;
         *                              break;
         *                      }
         *
         *              }
         *
         *      }
         * }
         */
    }
Ejemplo n.º 4
0
    public void subdivide(int _max_depth, int _depth)
    {
        //_max_depth, how far we are going to recurse
        //_depth, the current depth we are at for this quadtree
        ////for us (this node) to even exist means that it has at least one hit in its reguin, find it - NOT TRUE! neccesarily
        /// we exist, but it doesnt mean we contain anything. if any of our quadtrants exist, then they have someting in them.

        //break the rect into 4 regions and then see if anything is in it

        //int threshold_count = 0; //increase this as we find more shit
        Rect north_west_bounds = new Rect(bounds.x, bounds.y, (int)(bounds.width / 2), (int)(bounds.height / 2));
        Rect north_east_bounds = new Rect(bounds.x + (int)(bounds.width / 2), bounds.y, (int)(bounds.width / 2), (int)(bounds.height / 2));
        Rect south_east_bounds = new Rect(bounds.x + (int)(bounds.width / 2), bounds.y + (int)(bounds.height / 2), (int)(bounds.width / 2), (int)(bounds.height / 2));
        Rect south_west_bounds = new Rect(bounds.x, bounds.y + (int)(bounds.height / 2), (int)(bounds.width / 2), (int)(bounds.height / 2));

        //for each cube
        //for each subdivision rect
        //if collision(northwest, cube)
        //if collision(northeast, cube)
        //if collision(southwest, cube)
        //if collision(southeast, cube)

        foreach (GameObject _object in collision_objects)
        {
            //RectangleCollisionChecker.CircleRectInsersects(new Circle(_r1.center, _r1.width * 10), bounds)

            Rect   _object_bounds = RectangleCollisionChecker.BoundsToScreenRect(_object.GetComponent <Renderer>().bounds);
            Circle _obj_circle    = new Circle(_object_bounds.center, _object_bounds.width);

            //TextureDraw.DrawRectangle(tex, _r1, Color.yellow);
            //if(RectangleCollisionChecker.intersects(north_west_bounds, _object_bounds))
            if (RectangleCollisionChecker.CircleRectInsersects(_obj_circle, north_west_bounds))
            {
                this.northwest = new QuadTreeNode(north_west_bounds, this.bool_values, this.collision_objects, this.max_depth, this.depth + 1);
            }

            //if(RectangleCollisionChecker.intersects(north_east_bounds, _object_bounds))
            if (RectangleCollisionChecker.CircleRectInsersects(_obj_circle, north_east_bounds))
            {
                this.northeast = new QuadTreeNode(north_east_bounds, this.bool_values, this.collision_objects, this.max_depth, this.depth + 1);
            }

            //if(RectangleCollisionChecker.intersects(south_east_bounds, _object_bounds))
            if (RectangleCollisionChecker.CircleRectInsersects(_obj_circle, south_east_bounds))
            {
                this.southeast = new QuadTreeNode(south_east_bounds, this.bool_values, this.collision_objects, this.max_depth, this.depth + 1);
            }

            //if(RectangleCollisionChecker.intersects(south_west_bounds, _object_bounds))
            if (RectangleCollisionChecker.CircleRectInsersects(_obj_circle, south_west_bounds))
            {
                this.southwest = new QuadTreeNode(south_west_bounds, this.bool_values, this.collision_objects, this.max_depth, this.depth + 1);
            }

            /*
             * if( RectangleCollisionChecker.intersects(bounds, _r1))
             * {
             *      is_occupied =  true;
             *      break;
             * }
             */
        }

        /*
         * for(int i = 0; i<  resolution; i++)
         * {
         *      for(int j = 0; j<  resolution; j++)
         *      {
         *              Rect _r0 = new Rect(new Vector2(i * _multi_val_x, j * _multi_val_y), new Vector2(_multi_val_x, _multi_val_y));
         *              TextureDraw.DrawRectangle(tex, _r0, Color.blue);
         *
         *              foreach(GameObject _cube in cubes)
         *              {
         *                      if( bool_values[i, j] ==  true)
         *                              break;
         *
         *
         *                      Vector3 screen_coords = cam.WorldToScreenPoint(_cube.transform.position);
         *
         *                      Rect _r1 = RectangleCollisionChecker.BoundsToScreenRect(_cube.GetComponent<Renderer>().bounds);
         *                      TextureDraw.DrawRectangle(tex, _r1, Color.yellow);
         *                      //_r1.y = Screen.height - _r1.y;
         *                      //Rect _r11 = new Rect(new Vector2(screen_coords.x, screen_coords.y), new Vector2(50, 50));
         *                      //Debug.Log(" bounds _r1 bounds " + _r1.ToString());
         *                      //Debug.Log(" bounds _r11 " + _r11.ToString());
         *                      if( RectangleCollisionChecker.intersects(_r0, _r1))
         *                      {
         *                              bool_values[i, j] =  true;
         *                              break;
         *                      }
         *
         *              }
         *
         *      }
         * }
         */
    }