Add projecton on horizontal plane
This commit is contained in:
parent
aaf335d218
commit
91027b2e41
@ -5,27 +5,29 @@
|
||||
#ifndef VECTOR3_H
|
||||
#define VECTOR3_H
|
||||
|
||||
extern "C" {
|
||||
/// <summary>
|
||||
/// 3-dimensional Vector representation
|
||||
/// </summary>
|
||||
/// This is a C-style implementation
|
||||
/// This uses the right-handed coordinate system.
|
||||
typedef struct Vec3 {
|
||||
/// <summary>
|
||||
/// The right axis of the vector
|
||||
/// </summary>
|
||||
float x;
|
||||
/// <summary>
|
||||
/// The upward axis of the vector
|
||||
/// </summary>
|
||||
float y;
|
||||
/// <summary>
|
||||
/// The forward axis of the vector
|
||||
/// </summary>
|
||||
float z;
|
||||
#include "Vector2.h"
|
||||
|
||||
} Vec3;
|
||||
extern "C" {
|
||||
/// <summary>
|
||||
/// 3-dimensional Vector representation
|
||||
/// </summary>
|
||||
/// This is a C-style implementation
|
||||
/// This uses the right-handed coordinate system.
|
||||
typedef struct Vec3 {
|
||||
/// <summary>
|
||||
/// The right axis of the vector
|
||||
/// </summary>
|
||||
float x;
|
||||
/// <summary>
|
||||
/// The upward axis of the vector
|
||||
/// </summary>
|
||||
float y;
|
||||
/// <summary>
|
||||
/// The forward axis of the vector
|
||||
/// </summary>
|
||||
float z;
|
||||
|
||||
} Vec3;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -34,220 +36,228 @@ extern "C" {
|
||||
/// This uses the right-handed coordinate system.
|
||||
struct Vector3 : Vec3 {
|
||||
public:
|
||||
/// <summary>
|
||||
/// Create a new 3-dimensinal zero vector
|
||||
/// </summary>
|
||||
Vector3();
|
||||
/// <summary>
|
||||
/// Create a new 3-dimensional vector
|
||||
/// </summary>
|
||||
/// <param name="x">x axis value</param>
|
||||
/// <param name="y">y axis value</param>
|
||||
/// <param name="z">z axis value</param>
|
||||
Vector3(float x, float y, float z);
|
||||
/// <summary>
|
||||
/// Create a vector from C-style Vec3
|
||||
/// </summary>
|
||||
/// <param name="v">The C-style Vec</param>
|
||||
Vector3(Vec3 v);
|
||||
~Vector3();
|
||||
/// <summary>
|
||||
/// Create a new 3-dimensinal zero vector
|
||||
/// </summary>
|
||||
Vector3();
|
||||
/// <summary>
|
||||
/// Create a new 3-dimensional vector
|
||||
/// </summary>
|
||||
/// <param name="x">x axis value</param>
|
||||
/// <param name="y">y axis value</param>
|
||||
/// <param name="z">z axis value</param>
|
||||
Vector3(float x, float y, float z);
|
||||
/// <summary>
|
||||
/// Create a vector from C-style Vec3
|
||||
/// </summary>
|
||||
/// <param name="v">The C-style Vec</param>
|
||||
Vector3(Vec3 v);
|
||||
~Vector3();
|
||||
|
||||
/// <summary>
|
||||
/// A vector with zero for all axis
|
||||
/// </summary>
|
||||
const static Vector3 zero;
|
||||
/// <summary>
|
||||
/// A vector with one for all axis
|
||||
/// </summary>
|
||||
const static Vector3 one;
|
||||
/// <summary>
|
||||
/// A vector with values (1, 0, 0)
|
||||
/// </summary>
|
||||
const static Vector3 right;
|
||||
/// <summary>
|
||||
/// A vector3 with values (-1, 0, 0)
|
||||
/// </summary>
|
||||
const static Vector3 left;
|
||||
/// <summary>
|
||||
/// A vector with values (0, 1, 0)
|
||||
/// </summary>
|
||||
const static Vector3 up;
|
||||
/// <summary>
|
||||
/// A vector with values (0, -1, 0)
|
||||
/// </summary>
|
||||
const static Vector3 down;
|
||||
/// <summary>
|
||||
/// A vector with values (0, 0, 1)
|
||||
/// </summary>
|
||||
const static Vector3 forward;
|
||||
/// <summary>
|
||||
/// A vector with values (0, 0, -1)
|
||||
/// </summary>
|
||||
const static Vector3 back;
|
||||
/// <summary>
|
||||
/// A vector with zero for all axis
|
||||
/// </summary>
|
||||
const static Vector3 zero;
|
||||
/// <summary>
|
||||
/// A vector with one for all axis
|
||||
/// </summary>
|
||||
const static Vector3 one;
|
||||
/// <summary>
|
||||
/// A vector with values (1, 0, 0)
|
||||
/// </summary>
|
||||
const static Vector3 right;
|
||||
/// <summary>
|
||||
/// A vector3 with values (-1, 0, 0)
|
||||
/// </summary>
|
||||
const static Vector3 left;
|
||||
/// <summary>
|
||||
/// A vector with values (0, 1, 0)
|
||||
/// </summary>
|
||||
const static Vector3 up;
|
||||
/// <summary>
|
||||
/// A vector with values (0, -1, 0)
|
||||
/// </summary>
|
||||
const static Vector3 down;
|
||||
/// <summary>
|
||||
/// A vector with values (0, 0, 1)
|
||||
/// </summary>
|
||||
const static Vector3 forward;
|
||||
/// <summary>
|
||||
/// A vector with values (0, 0, -1)
|
||||
/// </summary>
|
||||
const static Vector3 back;
|
||||
|
||||
/// <summary>
|
||||
/// The length of a vector
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector for which you need the length</param>
|
||||
/// <returns>The length of the given vector</returns>
|
||||
static float Magnitude(const Vector3& vector);
|
||||
/// <summary>
|
||||
/// The length of this vector
|
||||
/// </summary>
|
||||
/// <returns>The length of this vector</returns>
|
||||
float magnitude() const;
|
||||
/// <summary>
|
||||
/// The squared length of a vector
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector for which you need the squared length</param>
|
||||
/// <returns>The squatred length</returns>
|
||||
/// 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);
|
||||
/// <summary>
|
||||
/// The squared length of this vector
|
||||
/// </summary>
|
||||
/// <returns>The squared length</returns>
|
||||
/// 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;
|
||||
/// <summary>
|
||||
/// Connvert a vector to a length of 1
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector to convert</param>
|
||||
/// <returns>The vector with length 1</returns>
|
||||
static Vector3 Normalize(Vector3 vector);
|
||||
/// <summary>
|
||||
/// Convert the vector to a length of a
|
||||
/// </summary>
|
||||
/// <returns>The vector with length 1</returns>
|
||||
Vector3 normalized() const;
|
||||
/// <summary>
|
||||
/// The length of a vector
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector for which you need the length</param>
|
||||
/// <returns>The length of the given vector</returns>
|
||||
static float Magnitude(const Vector3 &vector);
|
||||
/// <summary>
|
||||
/// The length of this vector
|
||||
/// </summary>
|
||||
/// <returns>The length of this vector</returns>
|
||||
float magnitude() const;
|
||||
/// <summary>
|
||||
/// The squared length of a vector
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector for which you need the squared
|
||||
/// length</param> <returns>The squatred length</returns> 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);
|
||||
/// <summary>
|
||||
/// The squared length of this vector
|
||||
/// </summary>
|
||||
/// <returns>The squared length</returns>
|
||||
/// 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;
|
||||
/// <summary>
|
||||
/// Connvert a vector to a length of 1
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector to convert</param>
|
||||
/// <returns>The vector with length 1</returns>
|
||||
static Vector3 Normalize(Vector3 vector);
|
||||
/// <summary>
|
||||
/// Convert the vector to a length of a
|
||||
/// </summary>
|
||||
/// <returns>The vector with length 1</returns>
|
||||
Vector3 normalized() const;
|
||||
|
||||
/// <summary>
|
||||
/// Negate the vector
|
||||
/// </summary>
|
||||
/// <returns>The negated vector</returns>
|
||||
/// This will result in a vector pointing in the opposite direction
|
||||
Vector3 operator -();
|
||||
/// <summary>
|
||||
/// Subtract a vector from this vector
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector to subtract from this vector</param>
|
||||
/// <returns>The result of the subtraction</returns>
|
||||
Vector3 operator -(const Vector3& vector) const;
|
||||
/// <summary>
|
||||
/// Negate the vector
|
||||
/// </summary>
|
||||
/// <returns>The negated vector</returns>
|
||||
/// This will result in a vector pointing in the opposite direction
|
||||
Vector3 operator-();
|
||||
/// <summary>
|
||||
/// Subtract a vector from this vector
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector to subtract from this vector</param>
|
||||
/// <returns>The result of the subtraction</returns>
|
||||
Vector3 operator-(const Vector3 &vector) const;
|
||||
|
||||
/// <summary>
|
||||
/// Add another vector to this vector
|
||||
/// </summary>
|
||||
/// <param name="vector2">The vector to add</param>
|
||||
/// <returns>The result of adding the vector</returns>
|
||||
Vector3 operator +(const Vector3& vector2) const;
|
||||
/// <summary>
|
||||
/// Add another vector to this vector
|
||||
/// </summary>
|
||||
/// <param name="vector2">The vector to add</param>
|
||||
/// <returns>The result of adding the vector</returns>
|
||||
Vector3 operator+(const Vector3 &vector2) const;
|
||||
|
||||
/// <summary>
|
||||
/// Scale a vector using another vector
|
||||
/// </summary>
|
||||
/// <param name="vector1">The vector to scale</param>
|
||||
/// <param name="vector2">A vector with scaling factors</param>
|
||||
/// <returns>The scaled vector</returns>
|
||||
/// 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);
|
||||
/// <summary>
|
||||
/// Scale a vector uniformly up
|
||||
/// </summary>
|
||||
/// <param name="factor">The scaling factor</param>
|
||||
/// <returns>The scaled vector</returns>
|
||||
/// Each component of the vector will be multipled with the same factor.
|
||||
Vector3 operator *(float factor) const;
|
||||
/// <summary>
|
||||
/// Scale a vector uniformy down
|
||||
/// </summary>
|
||||
/// <param name="factor">The scaling factor</param>
|
||||
/// <returns>The scaled vector</returns>
|
||||
/// Each componet of the vector will be divided by the same factor.
|
||||
Vector3 operator /(const float& factor);
|
||||
/// <summary>
|
||||
/// Scale a vector using another vector
|
||||
/// </summary>
|
||||
/// <param name="vector1">The vector to scale</param>
|
||||
/// <param name="vector2">A vector with scaling factors</param>
|
||||
/// <returns>The scaled vector</returns>
|
||||
/// 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);
|
||||
/// <summary>
|
||||
/// Scale a vector uniformly up
|
||||
/// </summary>
|
||||
/// <param name="factor">The scaling factor</param>
|
||||
/// <returns>The scaled vector</returns>
|
||||
/// Each component of the vector will be multipled with the same factor.
|
||||
Vector3 operator*(float factor) const;
|
||||
/// <summary>
|
||||
/// Scale a vector uniformy down
|
||||
/// </summary>
|
||||
/// <param name="factor">The scaling factor</param>
|
||||
/// <returns>The scaled vector</returns>
|
||||
/// Each componet of the vector will be divided by the same factor.
|
||||
Vector3 operator/(const float &factor);
|
||||
|
||||
/// <summary>
|
||||
/// The dot product of two vectors
|
||||
/// </summary>
|
||||
/// <param name="vector1">The first vector</param>
|
||||
/// <param name="vector2">The second vector</param>
|
||||
/// <returns>The dot product of the two vectors</returns>
|
||||
static float Dot(const Vector3& vector1, const Vector3& vector2);
|
||||
/// <summary>
|
||||
/// The dot product of two vectors
|
||||
/// </summary>
|
||||
/// <param name="vector1">The first vector</param>
|
||||
/// <param name="vector2">The second vector</param>
|
||||
/// <returns>The dot product of the two vectors</returns>
|
||||
static float Dot(const Vector3 &vector1, const Vector3 &vector2);
|
||||
|
||||
/// <summary>
|
||||
/// Check is this vector is equal to the given vector
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector to check against</param>
|
||||
/// <returns>True if it is identical to the given vector</returns>
|
||||
/// Note this uses float comparison to check equality which
|
||||
/// may have strange effects. Equality on float should be avoided.
|
||||
bool operator ==(const Vector3& vector);
|
||||
/// <summary>
|
||||
/// Check is this vector is equal to the given vector
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector to check against</param>
|
||||
/// <returns>True if it is identical to the given vector</returns>
|
||||
/// Note this uses float comparison to check equality which
|
||||
/// may have strange effects. Equality on float should be avoided.
|
||||
bool operator==(const Vector3 &vector);
|
||||
|
||||
/// <summary>
|
||||
/// The distance between two vectors
|
||||
/// </summary>
|
||||
/// <param name="vector1">The first vector</param>
|
||||
/// <param name="vector2">The second vectors</param>
|
||||
/// <returns>The distance between the two vectors</returns>
|
||||
static float Distance(const Vector3& vector1, const Vector3& vector2);
|
||||
/// <summary>
|
||||
/// The distance between two vectors
|
||||
/// </summary>
|
||||
/// <param name="vector1">The first vector</param>
|
||||
/// <param name="vector2">The second vectors</param>
|
||||
/// <returns>The distance between the two vectors</returns>
|
||||
static float Distance(const Vector3 &vector1, const Vector3 &vector2);
|
||||
|
||||
/// <summary>
|
||||
/// The cross product of two vectors
|
||||
/// </summary>
|
||||
/// <param name="vector1">The first vector</param>
|
||||
/// <param name="vector2">The second vector</param>
|
||||
/// <returns>The cross product of the two vectors</returns>
|
||||
static Vector3 Cross(const Vector3& vector1, const Vector3& vector2);
|
||||
/// <summary>
|
||||
/// The cross product of two vectors
|
||||
/// </summary>
|
||||
/// <param name="vector1">The first vector</param>
|
||||
/// <param name="vector2">The second vector</param>
|
||||
/// <returns>The cross product of the two vectors</returns>
|
||||
static Vector3 Cross(const Vector3 &vector1, const Vector3 &vector2);
|
||||
|
||||
/// <summary>
|
||||
/// Project a vector on another vector
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector to project</param>
|
||||
/// <param name="onNormal">The normal vector to project on</param>
|
||||
/// <returns>The projected vector</returns>
|
||||
static Vector3 Project(Vector3 vector, Vector3 onNormal);
|
||||
/// <summary>
|
||||
/// Projects a vector onto a plane defined by a normal orthogonal to the plane.
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector to project</param>
|
||||
/// <param name="planeNormal">The normal of the plane to project on</param>
|
||||
/// <returns></returns>
|
||||
static Vector3 ProjectOnPlane(Vector3 vector, Vector3 planeNormal);
|
||||
/// <summary>
|
||||
/// Project a vector on another vector
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector to project</param>
|
||||
/// <param name="onNormal">The normal vector to project on</param>
|
||||
/// <returns>The projected vector</returns>
|
||||
static Vector3 Project(Vector3 vector, Vector3 onNormal);
|
||||
/// <summary>
|
||||
/// Projects a vector onto a plane defined by a normal orthogonal to the
|
||||
/// plane.
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector to project</param>
|
||||
/// <param name="planeNormal">The normal of the plane to project on</param>
|
||||
/// <returns></returns>
|
||||
static Vector3 ProjectOnPlane(Vector3 vector, Vector3 planeNormal);
|
||||
|
||||
/// <summary>
|
||||
/// Calculate the angle between two vectors
|
||||
/// </summary>
|
||||
/// <param name="vector1">The first vector</param>
|
||||
/// <param name="vector2">The second vector</param>
|
||||
/// <returns></returns>
|
||||
/// 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);
|
||||
/// <summary>
|
||||
/// Projects a vector onto the horizontal plane.
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector to project</param>
|
||||
/// <returns>A 2D carthesian vector with the coordinates in the horizontal
|
||||
/// plane.</returns>
|
||||
static Vector2 ProjectHorizontalPlane(Vector3 vector);
|
||||
|
||||
/// <summary>
|
||||
/// Calculate the angle between two vectors rotation around an axis.
|
||||
/// </summary>
|
||||
/// <param name="from">The starting vector</param>
|
||||
/// <param name="to">The ending vector</param>
|
||||
/// <param name="axis">The axis to rotate around</param>
|
||||
/// <returns>The signed angle</returns>
|
||||
static float SignedAngle(Vector3 from, Vector3 to, Vector3 axis);
|
||||
/// <summary>
|
||||
/// Calculate the angle between two vectors
|
||||
/// </summary>
|
||||
/// <param name="vector1">The first vector</param>
|
||||
/// <param name="vector2">The second vector</param>
|
||||
/// <returns></returns>
|
||||
/// 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);
|
||||
|
||||
/// <summary>
|
||||
/// Calculate the angle between two vectors rotation around an axis.
|
||||
/// </summary>
|
||||
/// <param name="from">The starting vector</param>
|
||||
/// <param name="to">The ending vector</param>
|
||||
/// <param name="axis">The axis to rotate around</param>
|
||||
/// <returns>The signed angle</returns>
|
||||
static float SignedAngle(Vector3 from, Vector3 to, Vector3 axis);
|
||||
|
||||
/// <summary>
|
||||
/// Lerp between two vectors
|
||||
/// </summary>
|
||||
/// <param name="from">The from vector</param>
|
||||
/// <param name="to">The to vector</param>
|
||||
/// <param name="f">The interpolation distance (0..1)</param>
|
||||
/// <returns>The lerped vector</returns>
|
||||
/// 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);
|
||||
/// <summary>
|
||||
/// Lerp between two vectors
|
||||
/// </summary>
|
||||
/// <param name="from">The from vector</param>
|
||||
/// <param name="to">The to vector</param>
|
||||
/// <param name="f">The interpolation distance (0..1)</param>
|
||||
/// <returns>The lerped vector</returns>
|
||||
/// 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
|
178
src/Vector3.cpp
178
src/Vector3.cpp
@ -2,33 +2,32 @@
|
||||
// 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/.
|
||||
|
||||
#include <math.h>
|
||||
#include "Vector3.h"
|
||||
#include <math.h>
|
||||
|
||||
const float Deg2Rad = 0.0174532924F;
|
||||
const float Rad2Deg = 57.29578F;
|
||||
const float epsilon = 1E-05f;
|
||||
|
||||
Vector3::Vector3() {
|
||||
x = 0;
|
||||
y = 0;
|
||||
z = 0;
|
||||
x = 0;
|
||||
y = 0;
|
||||
z = 0;
|
||||
}
|
||||
|
||||
Vector3::Vector3(float _x, float _y, float _z) {
|
||||
x = _x;
|
||||
y = _y;
|
||||
z = _z;
|
||||
x = _x;
|
||||
y = _y;
|
||||
z = _z;
|
||||
}
|
||||
|
||||
Vector3::Vector3(Vec3 v) {
|
||||
x = v.x;
|
||||
y = v.y;
|
||||
z = v.z;
|
||||
x = v.x;
|
||||
y = v.y;
|
||||
z = v.z;
|
||||
}
|
||||
|
||||
Vector3::~Vector3() {
|
||||
}
|
||||
Vector3::~Vector3() {}
|
||||
|
||||
const Vector3 Vector3::zero = Vector3(0, 0, 0);
|
||||
const Vector3 Vector3::one = Vector3(1, 1, 1);
|
||||
@ -39,130 +38,129 @@ const Vector3 Vector3::down = Vector3(0, -1, 0);
|
||||
const Vector3 Vector3::forward = Vector3(0, 0, 1);
|
||||
const Vector3 Vector3::back = Vector3(0, 0, -1);
|
||||
|
||||
float Vector3::Magnitude(const Vector3& a) {
|
||||
return sqrtf(a.x * a.x + a.y * a.y + a.z * a.z);
|
||||
}
|
||||
float Vector3::magnitude() const {
|
||||
return (float)sqrtf(x * x + y * y + z * z);
|
||||
float Vector3::Magnitude(const Vector3 &a) {
|
||||
return sqrtf(a.x * a.x + a.y * a.y + a.z * a.z);
|
||||
}
|
||||
float Vector3::magnitude() const { return (float)sqrtf(x * x + y * y + z * z); }
|
||||
|
||||
float Vector3::SqrMagnitude(const Vector3& a) {
|
||||
return a.x * a.x + a.y * a.y + a.z * a.z;
|
||||
}
|
||||
float Vector3::sqrMagnitude() const {
|
||||
return(x * x + y * y + z * z);
|
||||
float Vector3::SqrMagnitude(const Vector3 &a) {
|
||||
return a.x * a.x + a.y * a.y + a.z * a.z;
|
||||
}
|
||||
float Vector3::sqrMagnitude() const { return (x * x + y * y + z * z); }
|
||||
|
||||
Vector3 Vector3::Normalize(Vector3 v) {
|
||||
float num = Vector3::Magnitude(v);
|
||||
Vector3 result = Vector3::zero;
|
||||
if (num > epsilon) {
|
||||
result = v / num;
|
||||
}
|
||||
return result;
|
||||
float num = Vector3::Magnitude(v);
|
||||
Vector3 result = Vector3::zero;
|
||||
if (num > epsilon) {
|
||||
result = v / num;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
Vector3 Vector3::normalized() const {
|
||||
float num = this->magnitude();
|
||||
Vector3 result = Vector3::zero;
|
||||
if (num > epsilon) {
|
||||
result = ((Vector3)*this) / num;
|
||||
}
|
||||
return result;
|
||||
float num = this->magnitude();
|
||||
Vector3 result = Vector3::zero;
|
||||
if (num > epsilon) {
|
||||
result = ((Vector3) * this) / num;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector3 Vector3::operator -(const Vector3& v2) const {
|
||||
return Vector3(this->x - v2.x, this->y - v2.y, this->z - v2.z);
|
||||
Vector3 Vector3::operator-(const Vector3 &v2) const {
|
||||
return Vector3(this->x - v2.x, this->y - v2.y, this->z - v2.z);
|
||||
}
|
||||
|
||||
Vector3 Vector3::operator -() {
|
||||
return Vector3(-this->x, -this->y, -this->z);
|
||||
Vector3 Vector3::operator-() { return Vector3(-this->x, -this->y, -this->z); }
|
||||
|
||||
Vector3 Vector3::operator+(const Vector3 &v2) const {
|
||||
return Vector3(this->x + v2.x, this->y + v2.y, this->z + v2.z);
|
||||
}
|
||||
|
||||
Vector3 Vector3::operator +(const Vector3& v2) const {
|
||||
return Vector3(this->x + v2.x, this->y + v2.y, this->z + v2.z);
|
||||
Vector3 Vector3::Scale(const Vector3 &p1, const Vector3 &p2) {
|
||||
return Vector3(p1.x * p2.x, p1.y * p2.y, p1.z * p2.z);
|
||||
}
|
||||
|
||||
Vector3 Vector3::Scale(const Vector3& p1, const Vector3& p2) {
|
||||
return Vector3(p1.x * p2.x, p1.y * p2.y, p1.z * p2.z);
|
||||
Vector3 Vector3::operator*(float f) const {
|
||||
return Vector3(this->x * f, this->y * f, this->z * f);
|
||||
}
|
||||
|
||||
Vector3 Vector3::operator *(float f) const {
|
||||
return Vector3(this->x * f, this->y * f, this->z * f);
|
||||
Vector3 Vector3::operator/(const float &d) {
|
||||
return Vector3(this->x / d, this->y / d, this->z / d);
|
||||
}
|
||||
|
||||
Vector3 Vector3::operator/(const float& d) {
|
||||
return Vector3(this->x / d, this->y / d, this->z / d);
|
||||
float Vector3::Dot(const Vector3 &v1, const Vector3 &v2) {
|
||||
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
|
||||
}
|
||||
|
||||
float Vector3::Dot(const Vector3& v1, const Vector3& v2) {
|
||||
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
|
||||
bool Vector3::operator==(const Vector3 &v) {
|
||||
return (this->x == v.x && this->y == v.y && this->z == v.z);
|
||||
}
|
||||
|
||||
bool Vector3::operator==(const Vector3& v) {
|
||||
return (this->x == v.x && this->y == v.y && this->z == v.z);
|
||||
float Vector3::Distance(const Vector3 &p1, const Vector3 &p2) {
|
||||
return Magnitude(p1 - p2);
|
||||
}
|
||||
|
||||
float Vector3::Distance(const Vector3& p1, const Vector3& p2) {
|
||||
return Magnitude(p1 - p2);
|
||||
}
|
||||
|
||||
Vector3 Vector3::Cross(const Vector3& v1, const Vector3& v2) {
|
||||
return Vector3(v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, v1.x * v2.y - v1.y * v2.x);
|
||||
Vector3 Vector3::Cross(const Vector3 &v1, const Vector3 &v2) {
|
||||
return Vector3(v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z,
|
||||
v1.x * v2.y - v1.y * v2.x);
|
||||
}
|
||||
|
||||
Vector3 Vector3::Project(Vector3 vector, Vector3 onNormal) {
|
||||
float sqrMagnitude = Dot(onNormal, onNormal);
|
||||
if (sqrMagnitude < epsilon)
|
||||
return Vector3::zero;
|
||||
else {
|
||||
float dot = Dot(vector, onNormal);
|
||||
Vector3 r = onNormal * dot / sqrMagnitude;
|
||||
return r;
|
||||
}
|
||||
float sqrMagnitude = Dot(onNormal, onNormal);
|
||||
if (sqrMagnitude < epsilon)
|
||||
return Vector3::zero;
|
||||
else {
|
||||
float dot = Dot(vector, onNormal);
|
||||
Vector3 r = onNormal * dot / sqrMagnitude;
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
Vector3 Vector3::ProjectOnPlane(Vector3 vector, Vector3 planeNormal) {
|
||||
Vector3 r = vector - Project(vector, planeNormal);
|
||||
return r;
|
||||
Vector3 r = vector - Project(vector, planeNormal);
|
||||
return r;
|
||||
}
|
||||
|
||||
Vector2 Vector3::ProjectHorizontalPlane(Vector3 vector) {
|
||||
Vector2 r = Vector2(vector.x, vector.z);
|
||||
return r;
|
||||
}
|
||||
|
||||
float clamp(float x, float lower, float upper) {
|
||||
float lowerClamp = fmaxf(x, lower);
|
||||
float upperClamp = fminf(upper, lowerClamp);
|
||||
return upperClamp;
|
||||
float lowerClamp = fmaxf(x, lower);
|
||||
float upperClamp = fminf(upper, lowerClamp);
|
||||
return upperClamp;
|
||||
}
|
||||
|
||||
float Vector3::Angle(Vector3 from, Vector3 to) {
|
||||
float denominator = sqrtf(from.sqrMagnitude() * to.sqrMagnitude());
|
||||
if (denominator < epsilon)
|
||||
return 0;
|
||||
float denominator = sqrtf(from.sqrMagnitude() * to.sqrMagnitude());
|
||||
if (denominator < epsilon)
|
||||
return 0;
|
||||
|
||||
float dot = Vector3::Dot(from, to);
|
||||
float fraction = dot / denominator;
|
||||
if (isnan(fraction))
|
||||
return fraction; // short cut to returning NaN universally
|
||||
float dot = Vector3::Dot(from, to);
|
||||
float fraction = dot / denominator;
|
||||
if (isnan(fraction))
|
||||
return fraction; // short cut to returning NaN universally
|
||||
|
||||
float cdot = clamp(fraction, -1.0, 1.0);
|
||||
float r = ((float)acos(cdot)) * Rad2Deg;
|
||||
return r;
|
||||
float cdot = clamp(fraction, -1.0, 1.0);
|
||||
float r = ((float)acos(cdot)) * Rad2Deg;
|
||||
return r;
|
||||
}
|
||||
|
||||
float Vector3::SignedAngle(Vector3 from, Vector3 to, Vector3 axis) {
|
||||
// angle in [0,180]
|
||||
float angle = Vector3::Angle(from, to);
|
||||
// angle in [0,180]
|
||||
float angle = Vector3::Angle(from, to);
|
||||
|
||||
Vector3 cross = Vector3::Cross(from, to);
|
||||
float b = Vector3::Dot(axis, cross);
|
||||
float signd = b < 0 ? -1.0F : (b > 0 ? 1.0F : 0.0F);
|
||||
Vector3 cross = Vector3::Cross(from, to);
|
||||
float b = Vector3::Dot(axis, cross);
|
||||
float signd = b < 0 ? -1.0F : (b > 0 ? 1.0F : 0.0F);
|
||||
|
||||
// angle in [-179,180]
|
||||
float signed_angle = angle * signd;
|
||||
|
||||
return signed_angle;
|
||||
// angle in [-179,180]
|
||||
float signed_angle = angle * signd;
|
||||
|
||||
return signed_angle;
|
||||
}
|
||||
|
||||
Vector3 Vector3::Lerp(Vector3 from, Vector3 to, float f) {
|
||||
Vector3 v = from + (to - from) * f;
|
||||
return v;
|
||||
Vector3 v = from + (to - from) * f;
|
||||
return v;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user