Ejemplo n.º 1
0
    public void InsertObject(IQuadObject in_obj)
    {
        if (_objs_ht.ContainsKey(in_obj.GetId()))
        {
            Debug.LogError(string.Format("Object with ID = {0} already exists", in_obj.GetId()));
            return;
        }

        CQuadLeaf current_quad = _initial_quad;
        int       comparator   = 1;
        RectInt   object_sides = GetObjectSidesOnQuadMap(in_obj.GetAABB());

        for (int i = _tree_depth; i > 0; i--)
        {
            int k = comparator << i - 1;
            int x = (k & object_sides.xMin) > 0 ? 1 : 0;
            int y = (k & object_sides.yMax) > 0 ? 1 : 0;
            if (x > 0 != (k & object_sides.xMax) > 0 || y > 0 != (k & object_sides.yMin) > 0)
            {
                break;
            }
            int index = (y << 1) + x;
            current_quad = current_quad.GetQuadOrCreateNewIfNonExist(index);
        }

        CObj obj = new CObj(in_obj, new AABB2D(object_sides.xMin, object_sides.yMax, object_sides.xMax, object_sides.yMin), current_quad);

        current_quad.AddObject(obj);
        _objs_ht.Add(in_obj.GetId(), obj);
    }
Ejemplo n.º 2
0
 public CQuadLeaf GetQuadOrCreateNewIfNonExist(int in_index)
 {
     if (_childs[in_index] == null)
     {
         _childs[in_index] = new CQuadLeaf(this, in_index);
     }
     return(_childs[in_index]);
 }
Ejemplo n.º 3
0
 public CQuadLeaf(CQuadLeaf in_parent, int in_quad_index)
 {
     _parent     = in_parent;
     _quad_index = in_quad_index;
     _objects    = new List <CObj>();
     CalculateOrigin();
     _quad_model = new SQuadModel(_origin, _size);
 }
Ejemplo n.º 4
0
 public QuadMap(float in_map_size, Vector3 in_world_origin, int in_tree_depth)
 {
     _unity_meters_in_piece = in_map_size / (Mathf.Pow(2, in_tree_depth));
     _origin             = in_world_origin;
     _tree_depth         = in_tree_depth;
     _initial_quad       = new CQuadLeaf(in_map_size, _origin);
     _objs_ht            = new Hashtable();
     _deleted_objects_id = new List <int>();
 }
Ejemplo n.º 5
0
 public CQuadLeaf(float in_size, Vector3 in_origin)
 {
     _parent     = null;
     _size       = in_size;
     _quad_index = 0;
     _objects    = new List <CObj>();
     _origin     = in_origin;
     _quad_model = new SQuadModel(_origin, _size);
 }
Ejemplo n.º 6
0
    public void DeleteNodeIfEmpty()
    {
        if (_parent == null)
        {
            return;
        }

        CQuadLeaf parent = _parent;

        if (_objects.Count == 0 && !HaveChilds())
        {
            _parent.DeleteChild(_quad_index);
        }

        parent.DeleteNodeIfEmpty();
    }
Ejemplo n.º 7
0
    public void SelectObjects(Vector3 in_mouse_position, List <IQuadObject> out_objects)
    {
        int comparator = 1;
        int depth      = _tree_depth;
        int x          = (int)((in_mouse_position.x - _origin.x) / _unity_meters_in_piece);
        int y          = (int)((in_mouse_position.z - _origin.z) / _unity_meters_in_piece);

        CQuadLeaf current_quad = _initial_quad;

        while (current_quad != null)
        {
            current_quad.GetSelectedObjects(in_mouse_position, out_objects);
            depth--;

            int k     = comparator << depth;
            int x_bin = (k & x) > 0 ? 1 : 0;
            int y_bin = (k & y) > 0 ? 1 : 0;
            int index = (y_bin << 1) + x_bin;

            current_quad = current_quad.GetQuad(index);
        }
    }
Ejemplo n.º 8
0
    public void DeleteObjectByMousePos(Vector3 in_mouse_position)
    {
        _deleted_objects_id.Clear();

        int comparator = 1;
        int depth      = _tree_depth;
        int x          = (int)((in_mouse_position.x - _origin.x) / _unity_meters_in_piece);
        int y          = (int)((in_mouse_position.z - _origin.z) / _unity_meters_in_piece);

        CQuadLeaf current_quad = _initial_quad;
        CQuadLeaf prev_quad    = null;

        while (current_quad != null)
        {
            if (current_quad.GetQuadObjectectsCount() != 0)
            {
                current_quad.DeleteObjectByMousePosition(in_mouse_position, _deleted_objects_id);
                for (int i = 0; i < _deleted_objects_id.Count; i++)
                {
                    _objs_ht.Remove(_deleted_objects_id[i]);
                }
            }


            depth--;

            int k     = comparator << depth;
            int x_bin = (k & x) > 0 ? 1 : 0;
            int y_bin = (k & y) > 0 ? 1 : 0;
            int index = (y_bin << 1) + x_bin;

            prev_quad    = current_quad;
            current_quad = current_quad.GetQuad(index);
        }

        prev_quad.DeleteNodeIfEmpty();
    }
Ejemplo n.º 9
0
 public CObj(IQuadObject in_quad_obj, AABB2D in_box2D, CQuadLeaf in_leaf)
 {
     _quad_obj = in_quad_obj;
     _box2D    = in_box2D;
     _leaf     = in_leaf;
 }