1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 }