|
5 | 5 | * Simple 3D vector class. Handles basic vector math for 3D vectors.
|
6 | 6 | */
|
7 | 7 | public final class Vector3 {
|
8 |
| - public float x; |
9 |
| - public float y; |
10 |
| - public float z; |
11 |
| - |
12 |
| - public static final Vector3 ZERO = new Vector3(0, 0, 0); |
13 |
| - public static final Vector3 UNIT_X = new Vector3(1, 0, 0); |
14 |
| - public static final Vector3 UNIT_Y = new Vector3(0, 1, 0); |
15 |
| - public static final Vector3 UNIT_Z = new Vector3(0, 0, 1); |
16 |
| - |
17 |
| - public Vector3() { |
18 |
| - } |
19 |
| - |
20 |
| - public Vector3(float[] array) |
21 |
| - { |
22 |
| - set(array[0], array[1], array[2]); |
23 |
| - } |
24 |
| - |
25 |
| - public Vector3(float xValue, float yValue, float zValue) { |
26 |
| - set(xValue, yValue, zValue); |
27 |
| - } |
28 |
| - |
29 |
| - public Vector3(Vector3 other) { |
30 |
| - set(other); |
31 |
| - } |
32 |
| - |
33 |
| - public final void add(Vector3 other) { |
34 |
| - x += other.x; |
35 |
| - y += other.y; |
36 |
| - z += other.z; |
37 |
| - } |
38 |
| - |
39 |
| - public final void add(float otherX, float otherY, float otherZ) { |
40 |
| - x += otherX; |
41 |
| - y += otherY; |
42 |
| - z += otherZ; |
43 |
| - } |
44 |
| - |
45 |
| - public final void subtract(Vector3 other) { |
46 |
| - x -= other.x; |
47 |
| - y -= other.y; |
48 |
| - z -= other.z; |
49 |
| - } |
50 |
| - |
51 |
| - public final void subtractMultiple(Vector3 other, float multiplicator) |
52 |
| - { |
53 |
| - x -= other.x * multiplicator; |
54 |
| - y -= other.y * multiplicator; |
55 |
| - z -= other.z * multiplicator; |
56 |
| - } |
57 |
| - |
58 |
| - public final void multiply(float magnitude) { |
59 |
| - x *= magnitude; |
60 |
| - y *= magnitude; |
61 |
| - z *= magnitude; |
62 |
| - } |
63 |
| - |
64 |
| - public final void multiply(Vector3 other) { |
65 |
| - x *= other.x; |
66 |
| - y *= other.y; |
67 |
| - z *= other.z; |
68 |
| - } |
69 |
| - |
70 |
| - public final void divide(float magnitude) { |
71 |
| - if (magnitude != 0.0f) { |
72 |
| - x /= magnitude; |
73 |
| - y /= magnitude; |
74 |
| - z /= magnitude; |
75 |
| - } |
76 |
| - } |
77 |
| - |
78 |
| - public final void set(Vector3 other) { |
79 |
| - x = other.x; |
80 |
| - y = other.y; |
81 |
| - z = other.z; |
82 |
| - } |
83 |
| - |
84 |
| - public final void set(float xValue, float yValue, float zValue) { |
85 |
| - x = xValue; |
86 |
| - y = yValue; |
87 |
| - z = zValue; |
88 |
| - } |
89 |
| - |
90 |
| - public final float dot(Vector3 other) { |
91 |
| - return (x * other.x) + (y * other.y) + (z * other.z); |
92 |
| - } |
93 |
| - |
94 |
| - public final Vector3 cross(Vector3 other) { |
95 |
| - return new Vector3(y * other.z - z * other.y, |
96 |
| - z * other.x - x * other.z, |
97 |
| - x * other.y - y * other.x); |
98 |
| - } |
99 |
| - |
100 |
| - public final float length() { |
101 |
| - return (float) Math.sqrt(length2()); |
102 |
| - } |
103 |
| - |
104 |
| - public final float length2() { |
105 |
| - return (x * x) + (y * y) + (z * z); |
106 |
| - } |
107 |
| - |
108 |
| - public final float distance2(Vector3 other) { |
109 |
| - float dx = x - other.x; |
110 |
| - float dy = y - other.y; |
111 |
| - float dz = z - other.z; |
112 |
| - return (dx * dx) + (dy * dy) + (dz * dz); |
113 |
| - } |
114 |
| - |
115 |
| - public final float normalize() { |
116 |
| - final float magnitude = length(); |
117 |
| - |
118 |
| - // TODO: I'm choosing safety over speed here. |
119 |
| - if (magnitude != 0.0f) { |
120 |
| - x /= magnitude; |
121 |
| - y /= magnitude; |
122 |
| - z /= magnitude; |
123 |
| - } |
124 |
| - |
125 |
| - return magnitude; |
126 |
| - } |
127 |
| - |
128 |
| - public final void zero() { |
129 |
| - set(0.0f, 0.0f, 0.0f); |
130 |
| - } |
131 |
| - |
132 |
| - public final boolean pointsInSameDirection(Vector3 other) { |
133 |
| - return this.dot(other) > 0; |
134 |
| - } |
| 8 | + public float x; |
| 9 | + public float y; |
| 10 | + public float z; |
| 11 | + |
| 12 | + public static final Vector3 ZERO = new Vector3(0, 0, 0); |
| 13 | + public static final Vector3 UNIT_X = new Vector3(1, 0, 0); |
| 14 | + public static final Vector3 UNIT_Y = new Vector3(0, 1, 0); |
| 15 | + public static final Vector3 UNIT_Z = new Vector3(0, 0, 1); |
| 16 | + |
| 17 | + public Vector3() { |
| 18 | + } |
| 19 | + |
| 20 | + public Vector3(float[] array) { |
| 21 | + set(array[0], array[1], array[2]); |
| 22 | + } |
| 23 | + |
| 24 | + public Vector3(float xValue, float yValue, float zValue) { |
| 25 | + set(xValue, yValue, zValue); |
| 26 | + } |
| 27 | + |
| 28 | + public Vector3(Vector3 other) { |
| 29 | + set(other); |
| 30 | + } |
| 31 | + |
| 32 | + public final void add(Vector3 other) { |
| 33 | + x += other.x; |
| 34 | + y += other.y; |
| 35 | + z += other.z; |
| 36 | + } |
| 37 | + |
| 38 | + public final void add(float otherX, float otherY, float otherZ) { |
| 39 | + x += otherX; |
| 40 | + y += otherY; |
| 41 | + z += otherZ; |
| 42 | + } |
| 43 | + |
| 44 | + public final void subtract(Vector3 other) { |
| 45 | + x -= other.x; |
| 46 | + y -= other.y; |
| 47 | + z -= other.z; |
| 48 | + } |
| 49 | + |
| 50 | + public final void subtractMultiple(Vector3 other, float multiplicator) { |
| 51 | + x -= other.x * multiplicator; |
| 52 | + y -= other.y * multiplicator; |
| 53 | + z -= other.z * multiplicator; |
| 54 | + } |
| 55 | + |
| 56 | + public final void multiply(float magnitude) { |
| 57 | + x *= magnitude; |
| 58 | + y *= magnitude; |
| 59 | + z *= magnitude; |
| 60 | + } |
| 61 | + |
| 62 | + public final void multiply(Vector3 other) { |
| 63 | + x *= other.x; |
| 64 | + y *= other.y; |
| 65 | + z *= other.z; |
| 66 | + } |
| 67 | + |
| 68 | + public final void divide(float magnitude) { |
| 69 | + if (magnitude != 0.0f) { |
| 70 | + x /= magnitude; |
| 71 | + y /= magnitude; |
| 72 | + z /= magnitude; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public final void set(Vector3 other) { |
| 77 | + x = other.x; |
| 78 | + y = other.y; |
| 79 | + z = other.z; |
| 80 | + } |
| 81 | + |
| 82 | + public final void set(float xValue, float yValue, float zValue) { |
| 83 | + x = xValue; |
| 84 | + y = yValue; |
| 85 | + z = zValue; |
| 86 | + } |
| 87 | + |
| 88 | + public final float dot(Vector3 other) { |
| 89 | + return (x * other.x) + (y * other.y) + (z * other.z); |
| 90 | + } |
| 91 | + |
| 92 | + public final Vector3 cross(Vector3 other) { |
| 93 | + return new Vector3(y * other.z - z * other.y, |
| 94 | + z * other.x - x * other.z, |
| 95 | + x * other.y - y * other.x); |
| 96 | + } |
| 97 | + |
| 98 | + public final float length() { |
| 99 | + return (float) Math.sqrt(length2()); |
| 100 | + } |
| 101 | + |
| 102 | + public final float length2() { |
| 103 | + return (x * x) + (y * y) + (z * z); |
| 104 | + } |
| 105 | + |
| 106 | + public final float distance2(Vector3 other) { |
| 107 | + float dx = x - other.x; |
| 108 | + float dy = y - other.y; |
| 109 | + float dz = z - other.z; |
| 110 | + return (dx * dx) + (dy * dy) + (dz * dz); |
| 111 | + } |
| 112 | + |
| 113 | + public final float normalize() { |
| 114 | + final float magnitude = length(); |
| 115 | + |
| 116 | + // TODO: I'm choosing safety over speed here. |
| 117 | + if (magnitude != 0.0f) { |
| 118 | + x /= magnitude; |
| 119 | + y /= magnitude; |
| 120 | + z /= magnitude; |
| 121 | + } |
| 122 | + |
| 123 | + return magnitude; |
| 124 | + } |
| 125 | + |
| 126 | + public final void zero() { |
| 127 | + set(0.0f, 0.0f, 0.0f); |
| 128 | + } |
| 129 | + |
| 130 | + public final boolean pointsInSameDirection(Vector3 other) { |
| 131 | + return this.dot(other) > 0; |
| 132 | + } |
135 | 133 |
|
136 | 134 | }
|
0 commit comments