Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lDEVinux
GitHub Repository: lDEVinux/eaglercraft
Path: blob/main/src/lwjgl/java/paulscode/sound/Vector3D.java
8644 views
1
package paulscode.sound;
2
3
/**
4
* The Vector3D class contains methods to simplify common 3D vector functions,
5
* such as cross and dot product, normalize, etc.
6
*<br><br>
7
*<b><i> SoundSystem License:</b></i><br><b><br>
8
* You are free to use this library for any purpose, commercial or otherwise.
9
* You may modify this library or source code, and distribute it any way you
10
* like, provided the following conditions are met:
11
*<br>
12
* 1) You may not falsely claim to be the author of this library or any
13
* unmodified portion of it.
14
*<br>
15
* 2) You may not copyright this library or a modified version of it and then
16
* sue me for copyright infringement.
17
*<br>
18
* 3) If you modify the source code, you must clearly document the changes
19
* made before redistributing the modified source code, so other users know
20
* it is not the original code.
21
*<br>
22
* 4) You are not required to give me credit for this library in any derived
23
* work, but if you do, you must also mention my website:
24
* http://www.paulscode.com
25
*<br>
26
* 5) I the author will not be responsible for any damages (physical,
27
* financial, or otherwise) caused by the use if this library or any part
28
* of it.
29
*<br>
30
* 6) I the author do not guarantee, warrant, or make any representations,
31
* either expressed or implied, regarding the use of this library or any
32
* part of it.
33
* <br><br>
34
* Author: Paul Lamb
35
* <br>
36
* http://www.paulscode.com
37
* </b>
38
*/
39
public class Vector3D
40
{
41
42
/**
43
* The vector's X coordinate.
44
*/
45
public float x;
46
47
/**
48
* The vector's Y coordinate.
49
*/
50
public float y;
51
52
/**
53
* The vector's Z coordinate.
54
*/
55
public float z;
56
57
/**
58
* Constructor: Places the vector at the origin.
59
*/
60
public Vector3D()
61
{
62
x = 0.0f;
63
y = 0.0f;
64
z = 0.0f;
65
}
66
67
/**
68
* Constructor: Places the vector at the specified 3D coordinates.
69
* @param nx X coordinate for the new vector.
70
* @param ny Y coordinate for the new vector.
71
* @param nz Z coordinate for the new vector.
72
*/
73
public Vector3D( float nx, float ny, float nz )
74
{
75
x = nx;
76
y = ny;
77
z = nz;
78
}
79
80
/**
81
* Returns a new instance containing the same information as this one.
82
* @return A new Vector3D.
83
*/
84
@Override
85
public Vector3D clone()
86
{
87
return new Vector3D( x, y, z );
88
}
89
90
/**
91
* Returns a vector containing the cross-product: A cross B.
92
* @param A First vector in the cross product.
93
* @param B Second vector in the cross product.
94
* @return A new Vector3D.
95
*/
96
public Vector3D cross( Vector3D A, Vector3D B )
97
{
98
return new Vector3D(
99
A.y * B.z - B.y * A.z,
100
A.z * B.x - B.z * A.x,
101
A.x * B.y - B.x * A.y );
102
}
103
104
/**
105
* Returns a vector containing the cross-product: (this) cross B.
106
* @param B Second vector in the cross product.
107
* @return A new Vector3D.
108
*/
109
public Vector3D cross( Vector3D B )
110
{
111
return new Vector3D(
112
y * B.z - B.y * z,
113
z * B.x - B.z * x,
114
x * B.y - B.x * y );
115
116
}
117
118
/**
119
* Returns the dot-product result of: A dot B.
120
* @param A First vector in the dot product.
121
* @param B Second vector in the dot product.
122
* @return Dot product.
123
*/
124
public float dot( Vector3D A, Vector3D B )
125
{
126
return( (A.x * B.x) + (A.y * B.y) + (A.z * B.z) );
127
}
128
129
/**
130
* Returns the dot-product result of: (this) dot B.
131
* @param B Second vector in the dot product.
132
* @return Dot product.
133
*/
134
public float dot( Vector3D B )
135
{
136
return( (x * B.x) + (y * B.y) + (z * B.z) );
137
}
138
139
/**
140
* Returns the vector represented by: A + B.
141
* @param A First vector.
142
* @param B Vector to add to A.
143
* @return A new Vector3D.
144
*/
145
public Vector3D add( Vector3D A, Vector3D B )
146
{
147
return new Vector3D( A.x + B.x, A.y + B.y, A.z + B.z );
148
}
149
150
/**
151
* Returns the vector represented by: (this) + B.
152
* @param B Vector to add to this one.
153
* @return A new Vector3D.
154
*/
155
public Vector3D add( Vector3D B )
156
{
157
return new Vector3D( x + B.x, y + B.y, z + B.z );
158
}
159
160
/**
161
* Returns the vector represented by: A - B.
162
* @param A First vector.
163
* @param B Vector to subtract from A.
164
* @return A new Vector3D.
165
*/
166
public Vector3D subtract( Vector3D A, Vector3D B )
167
{
168
return new Vector3D( A.x - B.x, A.y - B.y, A.z - B.z );
169
}
170
171
/**
172
* Returns the vector represented by: (this) - B.
173
* @param B Vector to subtract from this one.
174
* @return A new Vector3D.
175
*/
176
public Vector3D subtract( Vector3D B )
177
{
178
return new Vector3D( x - B.x, y - B.y, z - B.z );
179
}
180
181
/**
182
* Returns the length of this vector.
183
* @return Length.
184
*/
185
public float length()
186
{
187
return (float) Math.sqrt( x * x + y * y + z * z );
188
}
189
190
/**
191
* Changes the length of this vector to 1.0.
192
*/
193
public void normalize()
194
{
195
double t = Math.sqrt( x*x + y*y + z*z );
196
x = (float) (x / t);
197
y = (float) (y / t);
198
z = (float) (z / t);
199
}
200
201
/**
202
* Returns a string depicting this vector.
203
* @return "Vector3D (x, y, z)".
204
*/
205
@Override
206
public String toString()
207
{
208
return "Vector3D (" + x + ", " + y + ", " + z + ")";
209
}
210
}
211
212