Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (532)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Java Sector holding N-Size Tree – Quid =)  (Read 683 times)
0 Members and 1 Guest are viewing this topic.
Offline DQQAYME

Junior Member


Medals: 1
Projects: 1



« Posted 2013-05-17 15:46:02 »

Quid tree like Octree that Holds Sectors like chunks in minecraft.
Quid Grow Down, if minimum sector is 3 x 3 it can grow on 9 x 9 sector.

Main usage chunk holder =)

int size_Aray - Array Len By Axis
2 x 2 Base 2D Octree size.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
//Top Class
public class Quid_2D <E extends Quid_2D_Object>{
   private Quid_2D_Hold<E> Upper;
   
   public Quid_2D(int size_Aray){
      Upper = new Quid_2D_Hold<E>(size_Aray);
   }
   public void add_Object(Quid_2D_Object obj){
      Upper.add_Object(obj);
      Upper = Upper.get_Upper();
   }
   public E get_Object(GL_Vector_YX_int vec){
      return get_Object(vec.y, vec.x);
   }
   public E get_Object(int z, int x){
      E obj = Upper.get_Object(z, x);  
      return obj;
   }
}



1  
2  
3  
public interface Quid_2D_Object{
   public GL_Vector_YX_int get_Pos_Id();
}


1  
2  
3  
4  
   public GL_Vector_YX_int(int ypos, int xpos){
      y = ypos;
      x = xpos;
   }


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
public class Quid_2D_Hold <E extends Quid_2D_Object> implements Quid_2D_Object{
   int Side_Aray;
   int Size_Sector;
   
   Quid_2D_Hold<E> Up;
   GL_Vector_YX_int Quid_Pos;
   Object Quids_Grid[];
   boolean last;
   
   /**
    *  Data holding in array
    *  X X X -1 -1 -1 size 3
    *  X 0 X
    *  X X X
    *  
    *  X X X X -2 -2 -2 size 4
    *  X X X X
    *  X X 0 X
    *  X X X X
    */

   // z = y in code
  public Quid_2D_Hold(int size_Aray){
      int Dif = 0 - size_Aray / 2;//3 = 1// 4 = 2
     GL_Vector_YX_int quid_Pos = new GL_Vector_YX_int(Dif, Dif);//B
     Init(size_Aray, 1, quid_Pos);
      last = true;  
   }
   private Quid_2D_Hold(int size_Aray, int size_Sector, GL_Vector_YX_int Quid_Pos){
      Init(size_Aray, size_Sector, Quid_Pos);
   }
   private void Init(int size_Aray, int size_Sector, GL_Vector_YX_int quid_Pos){
      Size_Sector = size_Sector;
      Side_Aray = size_Aray;
      Quid_Pos = quid_Pos;
      Quids_Grid = new Object[Side_Aray * Side_Aray];//z x
  }
   public void add_Object(Quid_2D_Object obj){
      if(Check_Object_in(obj))
         add_Object_in(obj);
      else
         add_Object_Out(obj);  
   }
   public E get_Object(GL_Vector_YX_int vec){
      return get_Object(vec.y, vec.x);
   }
   public E get_Object(int z, int x){
      Quid_2D_Hold<E> quid = get_Upper();
      E obj = quid.get_Object_in(z, x);  
      return obj;
   }
   public Quid_2D_Hold<E> get_Upper(){
      Quid_2D_Hold<E> quid = this;
      while(quid.Up != null){
         quid = quid.Up;
      }
      return quid;
   }
////////////////////////////////////////////////////////////////////////
  private boolean Check_Object_in(Quid_2D_Object obj){
      GL_Vector_YX_int pos_Obj = obj.get_Pos_Id();
      return Check_Object_in(pos_Obj.y, pos_Obj.x);
   }
   private boolean Check_Object_in(int z, int x){
      int size = Side_Aray * Size_Sector;        
      int from_z = Quid_Pos.y;
      int to_z = from_z + size;      
      if(from_z <= z && z <= to_z){
         
         int from_x = Quid_Pos.x;
         int to_x = from_x + size;
         if(from_x <= x && x <= to_x){
            return true;
         }
      }
      return false;
   }
   
   private void add_Object_in(Quid_2D_Object obj){
      GL_Vector_YX_int pos_Obj = obj.get_Pos_Id();
      int z = pos_Obj.y - Quid_Pos.y;
      int x = pos_Obj.x - Quid_Pos.x;
      z /= Size_Sector;
      x /= Size_Sector;
     
      int pos_Ar = z * Side_Aray + x;
      if(last){
         if(Quids_Grid[pos_Ar] != null)
            throw new IllegalArgumentException("Quid2D Try create Double " + obj.get_Pos_Id());
         Quids_Grid[pos_Ar] = obj;
      }
      else{
         Quid_2D_Hold<E> in_Quid = (Quid_2D_Hold<E>) Quids_Grid[pos_Ar];
         if(in_Quid == null){
            in_Quid = Create_Quid_Down(z, x);
            Quids_Grid[pos_Ar] = in_Quid;
         }
         in_Quid.add_Object(obj);
      }
   }
   private void add_Object_Out(Quid_2D_Object obj){
      if(Up == null){
         Create_Quid_Up();
      }
      Up.add_Object(obj);
   }
   private Quid_2D_Hold<E> Create_Quid_Down(int z, int x){
      z = z * Size_Sector + Quid_Pos.y;
      x = x * Size_Sector + Quid_Pos.x;
      GL_Vector_YX_int quid_Pos = new GL_Vector_YX_int(z, x);
      int size_Sector = Size_Sector / Side_Aray;
      Quid_2D_Hold<E> down = new Quid_2D_Hold<E>(Side_Aray, size_Sector, quid_Pos);
      if(size_Sector == 1)
         down.last = true;
      down.Up = this;
      D.S("Create Down " + down);
      return down;
   }
   private void Create_Quid_Up(){
      int size_Sector = Size_Sector * Side_Aray;
      //Midls pos
     int Dif = 0 - Side_Aray / 2;
      Dif *= size_Sector;
      int z = Dif + Quid_Pos.y;
      int x = Dif + Quid_Pos.x;
      GL_Vector_YX_int quid_Pos = new GL_Vector_YX_int(z, x);
      Quid_2D_Hold<E> up = new Quid_2D_Hold<E>(Side_Aray, size_Sector, quid_Pos);
      up.add_Quid_in(this);
      Up = up;
      D.S("Create Up " + Up);
   }
   private void add_Quid_in(Quid_2D_Object obj){
      GL_Vector_YX_int pos_Obj = obj.get_Pos_Id();
      int z = pos_Obj.y - Quid_Pos.y;
      int x = pos_Obj.x - Quid_Pos.x;
      z /= Size_Sector;
      x /= Size_Sector;
     
      int pos_Ar = z * Side_Aray + x;
      Quids_Grid[pos_Ar] = obj;
   }

   private E get_Object_in(int oz, int ox){
      int z = oz - Quid_Pos.y;
      int x = ox - Quid_Pos.x;
      z /= Size_Sector;
      x /= Size_Sector;
     
      int pos_Ar = z * Side_Aray + x;
      if(pos_Ar < 0 || pos_Ar >= Quids_Grid.length)
         return null;//out
     
      if(last){
         E obj = (E) Quids_Grid[pos_Ar];
         return obj;
      }
      else{
         Quid_2D_Hold<E> in_Quid = (Quid_2D_Hold<E>) Quids_Grid[pos_Ar];
         if(in_Quid == null)
            return null;
         return in_Quid.get_Object_in(oz, ox);
      }
   }
   
   @Override
   public GL_Vector_YX_int get_Pos_Id(){
      return Quid_Pos;
   }
   
   @Override
   public String toString(){
      String te = " z:" + (Quid_Pos.y)  + " * " + (Quid_Pos.y + Side_Aray * Size_Sector - 1);
      te += " x:" + (Quid_Pos.x)  + " * " + (Quid_Pos.x + Side_Aray * Size_Sector - 1);
      te += " s:" + Size_Sector;
      return te;
   }
}

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (16 views)
2014-07-24 01:59:36

Riven (16 views)
2014-07-23 21:16:32

Riven (14 views)
2014-07-23 21:07:15

Riven (16 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!