Path: blob/main/src/lwjgl/java/paulscode/sound/Vector3D.java
8644 views
package paulscode.sound;12/**3* The Vector3D class contains methods to simplify common 3D vector functions,4* such as cross and dot product, normalize, etc.5*<br><br>6*<b><i> SoundSystem License:</b></i><br><b><br>7* You are free to use this library for any purpose, commercial or otherwise.8* You may modify this library or source code, and distribute it any way you9* like, provided the following conditions are met:10*<br>11* 1) You may not falsely claim to be the author of this library or any12* unmodified portion of it.13*<br>14* 2) You may not copyright this library or a modified version of it and then15* sue me for copyright infringement.16*<br>17* 3) If you modify the source code, you must clearly document the changes18* made before redistributing the modified source code, so other users know19* it is not the original code.20*<br>21* 4) You are not required to give me credit for this library in any derived22* work, but if you do, you must also mention my website:23* http://www.paulscode.com24*<br>25* 5) I the author will not be responsible for any damages (physical,26* financial, or otherwise) caused by the use if this library or any part27* of it.28*<br>29* 6) I the author do not guarantee, warrant, or make any representations,30* either expressed or implied, regarding the use of this library or any31* part of it.32* <br><br>33* Author: Paul Lamb34* <br>35* http://www.paulscode.com36* </b>37*/38public class Vector3D39{4041/**42* The vector's X coordinate.43*/44public float x;4546/**47* The vector's Y coordinate.48*/49public float y;5051/**52* The vector's Z coordinate.53*/54public float z;5556/**57* Constructor: Places the vector at the origin.58*/59public Vector3D()60{61x = 0.0f;62y = 0.0f;63z = 0.0f;64}6566/**67* Constructor: Places the vector at the specified 3D coordinates.68* @param nx X coordinate for the new vector.69* @param ny Y coordinate for the new vector.70* @param nz Z coordinate for the new vector.71*/72public Vector3D( float nx, float ny, float nz )73{74x = nx;75y = ny;76z = nz;77}7879/**80* Returns a new instance containing the same information as this one.81* @return A new Vector3D.82*/83@Override84public Vector3D clone()85{86return new Vector3D( x, y, z );87}8889/**90* Returns a vector containing the cross-product: A cross B.91* @param A First vector in the cross product.92* @param B Second vector in the cross product.93* @return A new Vector3D.94*/95public Vector3D cross( Vector3D A, Vector3D B )96{97return new Vector3D(98A.y * B.z - B.y * A.z,99A.z * B.x - B.z * A.x,100A.x * B.y - B.x * A.y );101}102103/**104* Returns a vector containing the cross-product: (this) cross B.105* @param B Second vector in the cross product.106* @return A new Vector3D.107*/108public Vector3D cross( Vector3D B )109{110return new Vector3D(111y * B.z - B.y * z,112z * B.x - B.z * x,113x * B.y - B.x * y );114115}116117/**118* Returns the dot-product result of: A dot B.119* @param A First vector in the dot product.120* @param B Second vector in the dot product.121* @return Dot product.122*/123public float dot( Vector3D A, Vector3D B )124{125return( (A.x * B.x) + (A.y * B.y) + (A.z * B.z) );126}127128/**129* Returns the dot-product result of: (this) dot B.130* @param B Second vector in the dot product.131* @return Dot product.132*/133public float dot( Vector3D B )134{135return( (x * B.x) + (y * B.y) + (z * B.z) );136}137138/**139* Returns the vector represented by: A + B.140* @param A First vector.141* @param B Vector to add to A.142* @return A new Vector3D.143*/144public Vector3D add( Vector3D A, Vector3D B )145{146return new Vector3D( A.x + B.x, A.y + B.y, A.z + B.z );147}148149/**150* Returns the vector represented by: (this) + B.151* @param B Vector to add to this one.152* @return A new Vector3D.153*/154public Vector3D add( Vector3D B )155{156return new Vector3D( x + B.x, y + B.y, z + B.z );157}158159/**160* Returns the vector represented by: A - B.161* @param A First vector.162* @param B Vector to subtract from A.163* @return A new Vector3D.164*/165public Vector3D subtract( Vector3D A, Vector3D B )166{167return new Vector3D( A.x - B.x, A.y - B.y, A.z - B.z );168}169170/**171* Returns the vector represented by: (this) - B.172* @param B Vector to subtract from this one.173* @return A new Vector3D.174*/175public Vector3D subtract( Vector3D B )176{177return new Vector3D( x - B.x, y - B.y, z - B.z );178}179180/**181* Returns the length of this vector.182* @return Length.183*/184public float length()185{186return (float) Math.sqrt( x * x + y * y + z * z );187}188189/**190* Changes the length of this vector to 1.0.191*/192public void normalize()193{194double t = Math.sqrt( x*x + y*y + z*z );195x = (float) (x / t);196y = (float) (y / t);197z = (float) (z / t);198}199200/**201* Returns a string depicting this vector.202* @return "Vector3D (x, y, z)".203*/204@Override205public String toString()206{207return "Vector3D (" + x + ", " + y + ", " + z + ")";208}209}210211212