// This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0.If a copy of the MPL was not distributed with this // file, You can obtain one at https ://mozilla.org/MPL/2.0/. #ifndef VECTOR3_H #define VECTOR3_H #include "Vector2.h" extern "C" { /// /// 3-dimensional Vector representation /// /// This is a C-style implementation /// This uses the right-handed coordinate system. typedef struct Vec3 { /// /// The right axis of the vector /// float x; /// /// The upward axis of the vector /// float y; /// /// The forward axis of the vector /// float z; } Vec3; } /// /// A 3-dimensional vector /// /// This uses the right-handed coordinate system. struct Vector3 : Vec3 { public: /// /// Create a new 3-dimensinal zero vector /// Vector3(); /// /// Create a new 3-dimensional vector /// /// x axis value /// y axis value /// z axis value Vector3(float x, float y, float z); /// /// Create a vector from C-style Vec3 /// /// The C-style Vec Vector3(Vec3 v); ~Vector3(); /// /// A vector with zero for all axis /// const static Vector3 zero; /// /// A vector with one for all axis /// const static Vector3 one; /// /// A vector with values (1, 0, 0) /// const static Vector3 right; /// /// A vector3 with values (-1, 0, 0) /// const static Vector3 left; /// /// A vector with values (0, 1, 0) /// const static Vector3 up; /// /// A vector with values (0, -1, 0) /// const static Vector3 down; /// /// A vector with values (0, 0, 1) /// const static Vector3 forward; /// /// A vector with values (0, 0, -1) /// const static Vector3 back; /// /// The length of a vector /// /// The vector for which you need the length /// The length of the given vector static float Magnitude(const Vector3 &vector); /// /// The length of this vector /// /// The length of this vector float magnitude() const; /// /// The squared length of a vector /// /// The vector for which you need the squared /// length The squatred length The squared length /// is computationally simpler than the real length. Think of Pythagoras A^2 + /// B^2 = C^2. This leaves out the calculation of the squared root of C. static float SqrMagnitude(const Vector3 &vector); /// /// The squared length of this vector /// /// The squared length /// The squared length is computationally simpler than the real length. /// Think of Pythagoras A^2 + B^2 = C^2. /// This leaves out the calculation of the squared root of C. float sqrMagnitude() const; /// /// Connvert a vector to a length of 1 /// /// The vector to convert /// The vector with length 1 static Vector3 Normalize(Vector3 vector); /// /// Convert the vector to a length of a /// /// The vector with length 1 Vector3 normalized() const; /// /// Negate the vector /// /// The negated vector /// This will result in a vector pointing in the opposite direction Vector3 operator-(); /// /// Subtract a vector from this vector /// /// The vector to subtract from this vector /// The result of the subtraction Vector3 operator-(const Vector3 &vector) const; /// /// Add another vector to this vector /// /// The vector to add /// The result of adding the vector Vector3 operator+(const Vector3 &vector2) const; /// /// Scale a vector using another vector /// /// The vector to scale /// A vector with scaling factors /// The scaled vector /// Each component of the vector v1 will be multiplied with the /// component from the scaling vector v2. static Vector3 Scale(const Vector3 &vector1, const Vector3 &vector2); /// /// Scale a vector uniformly up /// /// The scaling factor /// The scaled vector /// Each component of the vector will be multipled with the same factor. Vector3 operator*(float factor) const; /// /// Scale a vector uniformy down /// /// The scaling factor /// The scaled vector /// Each componet of the vector will be divided by the same factor. Vector3 operator/(const float &factor); /// /// The dot product of two vectors /// /// The first vector /// The second vector /// The dot product of the two vectors static float Dot(const Vector3 &vector1, const Vector3 &vector2); /// /// Check is this vector is equal to the given vector /// /// The vector to check against /// True if it is identical to the given vector /// Note this uses float comparison to check equality which /// may have strange effects. Equality on float should be avoided. bool operator==(const Vector3 &vector); /// /// The distance between two vectors /// /// The first vector /// The second vectors /// The distance between the two vectors static float Distance(const Vector3 &vector1, const Vector3 &vector2); /// /// The cross product of two vectors /// /// The first vector /// The second vector /// The cross product of the two vectors static Vector3 Cross(const Vector3 &vector1, const Vector3 &vector2); /// /// Project a vector on another vector /// /// The vector to project /// The normal vector to project on /// The projected vector static Vector3 Project(Vector3 vector, Vector3 onNormal); /// /// Projects a vector onto a plane defined by a normal orthogonal to the /// plane. /// /// The vector to project /// The normal of the plane to project on /// static Vector3 ProjectOnPlane(Vector3 vector, Vector3 planeNormal); /// /// Projects a vector onto the horizontal plane. /// /// The vector to project /// A 2D carthesian vector with the coordinates in the horizontal /// plane. static Vector2 ProjectHorizontalPlane(Vector3 vector); /// /// Calculate the angle between two vectors /// /// The first vector /// The second vector /// /// This reterns an unsigned angle which is the shortest distance /// between the two vectors. Use Vector3::SignedAngle if a /// signed angle is needed. static float Angle(Vector3 vector1, Vector3 vector2); /// /// Calculate the angle between two vectors rotation around an axis. /// /// The starting vector /// The ending vector /// The axis to rotate around /// The signed angle static float SignedAngle(Vector3 from, Vector3 to, Vector3 axis); /// /// Lerp between two vectors /// /// The from vector /// The to vector /// The interpolation distance (0..1) /// The lerped vector /// The factor f is unclamped. Value 0 matches the *from* vector, Value 1 /// matches the *to* vector Value -1 is *from* vector minus the difference /// between *from* and *to* etc. static Vector3 Lerp(Vector3 from, Vector3 to, float f); }; #endif