View Javadoc

1   /*
2    * Copyright (C) 2005 Erland Isaksson (erland_i@hotmail.com)
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU General Public License
6    * as published by the Free Software Foundation; either version 2
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   */
19  package erland.game.tileadventure;
20  
21  import java.awt.*;
22  import java.awt.geom.Rectangle2D;
23  
24  public class Box3D {
25      private float x;
26      private float y;
27      private float z;
28      private float sizeX;
29      private float sizeY;
30      private float sizeZ;
31  
32      public Box3D() {}
33      public Box3D(float x, float y, float z, float sizeX,float sizeY, float sizeZ) {
34          setLocation(x,y,z);
35          setSize(sizeX,sizeY,sizeZ);
36      }
37  
38      public void setBox(Box3D p) {
39          setLocation(p.x,p.y,p.z);
40          setSize(p.sizeX,p.sizeY,p.sizeZ);
41      }
42      public void setLocation(Point3D p) {
43          setLocation(p.getX(),p.getY(),p.getZ());
44      }
45      public void setLocation(float x, float y, float z) {
46          this.x = x;
47          this.y = y;
48          this.z = z;
49      }
50      public void setSize(Size3D s) {
51          setSize(s.getX(),s.getY(),s.getZ());
52      }
53      public void setSize(float sizeX,float sizeY,float sizeZ) {
54          this.sizeX = sizeX;
55          this.sizeY = sizeY;
56          this.sizeZ = sizeZ;
57      }
58      public float getMinX() {
59          return x;
60      }
61  
62      public float getMinY() {
63          return y;
64      }
65  
66      public float getMinZ() {
67          return z;
68      }
69      public float getMaxX() {
70          return x+sizeX;
71      }
72  
73      public float getMaxY() {
74          return y+sizeY;
75      }
76  
77      public float getMaxZ() {
78          return z+sizeZ;
79      }
80      public boolean contains(float x, float y, float z) {
81          return x>=this.x && x<=(this.x+sizeX) &&
82                 y>=this.y && y<=(this.y+sizeY) &&
83                 z>=this.z && z<=(this.z+sizeZ);
84     }
85     public boolean intersects(Box3D b) {
86         if(b.x>x+sizeX ||
87            b.y>y+sizeY ||
88            b.z>z+sizeZ ||
89            b.x+b.sizeX<x ||
90            b.y+b.sizeY<y ||
91            b.z+b.sizeZ<z) {
92             return false;
93         }
94         return b.contains(x,y,z) ||
95                b.contains(x,y,z+sizeZ) ||
96                b.contains(x,y+sizeY,z) ||
97                b.contains(x,y+sizeY,z+sizeZ) ||
98                b.contains(x+sizeX,y,z) ||
99                b.contains(x+sizeX,y,z+sizeZ) ||
100               b.contains(x+sizeX,y+sizeY,z) ||
101               b.contains(x+sizeX,y+sizeY,z+sizeZ) ||
102               contains(x,y,z);
103    }
104 
105 }