Add projecton on horizontal plane
This commit is contained in:
parent
aaf335d218
commit
91027b2e41
@ -5,13 +5,15 @@
|
|||||||
#ifndef VECTOR3_H
|
#ifndef VECTOR3_H
|
||||||
#define VECTOR3_H
|
#define VECTOR3_H
|
||||||
|
|
||||||
|
#include "Vector2.h"
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 3-dimensional Vector representation
|
/// 3-dimensional Vector representation
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// This is a C-style implementation
|
/// This is a C-style implementation
|
||||||
/// This uses the right-handed coordinate system.
|
/// This uses the right-handed coordinate system.
|
||||||
typedef struct Vec3 {
|
typedef struct Vec3 {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The right axis of the vector
|
/// The right axis of the vector
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -25,7 +27,7 @@ extern "C" {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
float z;
|
float z;
|
||||||
|
|
||||||
} Vec3;
|
} Vec3;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -90,7 +92,7 @@ public:
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="vector">The vector for which you need the length</param>
|
/// <param name="vector">The vector for which you need the length</param>
|
||||||
/// <returns>The length of the given vector</returns>
|
/// <returns>The length of the given vector</returns>
|
||||||
static float Magnitude(const Vector3& vector);
|
static float Magnitude(const Vector3 &vector);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The length of this vector
|
/// The length of this vector
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -99,12 +101,11 @@ public:
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The squared length of a vector
|
/// The squared length of a vector
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="vector">The vector for which you need the squared length</param>
|
/// <param name="vector">The vector for which you need the squared
|
||||||
/// <returns>The squatred length</returns>
|
/// length</param> <returns>The squatred length</returns> The squared length
|
||||||
/// The squared length is computationally simpler than the real length.
|
/// is computationally simpler than the real length. Think of Pythagoras A^2 +
|
||||||
/// Think of Pythagoras A^2 + B^2 = C^2.
|
/// B^2 = C^2. This leaves out the calculation of the squared root of C.
|
||||||
/// This leaves out the calculation of the squared root of C.
|
static float SqrMagnitude(const Vector3 &vector);
|
||||||
static float SqrMagnitude(const Vector3& vector);
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The squared length of this vector
|
/// The squared length of this vector
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -130,20 +131,20 @@ public:
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>The negated vector</returns>
|
/// <returns>The negated vector</returns>
|
||||||
/// This will result in a vector pointing in the opposite direction
|
/// This will result in a vector pointing in the opposite direction
|
||||||
Vector3 operator -();
|
Vector3 operator-();
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Subtract a vector from this vector
|
/// Subtract a vector from this vector
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="vector">The vector to subtract from this vector</param>
|
/// <param name="vector">The vector to subtract from this vector</param>
|
||||||
/// <returns>The result of the subtraction</returns>
|
/// <returns>The result of the subtraction</returns>
|
||||||
Vector3 operator -(const Vector3& vector) const;
|
Vector3 operator-(const Vector3 &vector) const;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Add another vector to this vector
|
/// Add another vector to this vector
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="vector2">The vector to add</param>
|
/// <param name="vector2">The vector to add</param>
|
||||||
/// <returns>The result of adding the vector</returns>
|
/// <returns>The result of adding the vector</returns>
|
||||||
Vector3 operator +(const Vector3& vector2) const;
|
Vector3 operator+(const Vector3 &vector2) const;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Scale a vector using another vector
|
/// Scale a vector using another vector
|
||||||
@ -153,21 +154,21 @@ public:
|
|||||||
/// <returns>The scaled vector</returns>
|
/// <returns>The scaled vector</returns>
|
||||||
/// Each component of the vector v1 will be multiplied with the
|
/// Each component of the vector v1 will be multiplied with the
|
||||||
/// component from the scaling vector v2.
|
/// component from the scaling vector v2.
|
||||||
static Vector3 Scale(const Vector3& vector1, const Vector3& vector2);
|
static Vector3 Scale(const Vector3 &vector1, const Vector3 &vector2);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Scale a vector uniformly up
|
/// Scale a vector uniformly up
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="factor">The scaling factor</param>
|
/// <param name="factor">The scaling factor</param>
|
||||||
/// <returns>The scaled vector</returns>
|
/// <returns>The scaled vector</returns>
|
||||||
/// Each component of the vector will be multipled with the same factor.
|
/// Each component of the vector will be multipled with the same factor.
|
||||||
Vector3 operator *(float factor) const;
|
Vector3 operator*(float factor) const;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Scale a vector uniformy down
|
/// Scale a vector uniformy down
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="factor">The scaling factor</param>
|
/// <param name="factor">The scaling factor</param>
|
||||||
/// <returns>The scaled vector</returns>
|
/// <returns>The scaled vector</returns>
|
||||||
/// Each componet of the vector will be divided by the same factor.
|
/// Each componet of the vector will be divided by the same factor.
|
||||||
Vector3 operator /(const float& factor);
|
Vector3 operator/(const float &factor);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The dot product of two vectors
|
/// The dot product of two vectors
|
||||||
@ -175,7 +176,7 @@ public:
|
|||||||
/// <param name="vector1">The first vector</param>
|
/// <param name="vector1">The first vector</param>
|
||||||
/// <param name="vector2">The second vector</param>
|
/// <param name="vector2">The second vector</param>
|
||||||
/// <returns>The dot product of the two vectors</returns>
|
/// <returns>The dot product of the two vectors</returns>
|
||||||
static float Dot(const Vector3& vector1, const Vector3& vector2);
|
static float Dot(const Vector3 &vector1, const Vector3 &vector2);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Check is this vector is equal to the given vector
|
/// Check is this vector is equal to the given vector
|
||||||
@ -184,7 +185,7 @@ public:
|
|||||||
/// <returns>True if it is identical to the given vector</returns>
|
/// <returns>True if it is identical to the given vector</returns>
|
||||||
/// Note this uses float comparison to check equality which
|
/// Note this uses float comparison to check equality which
|
||||||
/// may have strange effects. Equality on float should be avoided.
|
/// may have strange effects. Equality on float should be avoided.
|
||||||
bool operator ==(const Vector3& vector);
|
bool operator==(const Vector3 &vector);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The distance between two vectors
|
/// The distance between two vectors
|
||||||
@ -192,7 +193,7 @@ public:
|
|||||||
/// <param name="vector1">The first vector</param>
|
/// <param name="vector1">The first vector</param>
|
||||||
/// <param name="vector2">The second vectors</param>
|
/// <param name="vector2">The second vectors</param>
|
||||||
/// <returns>The distance between the two vectors</returns>
|
/// <returns>The distance between the two vectors</returns>
|
||||||
static float Distance(const Vector3& vector1, const Vector3& vector2);
|
static float Distance(const Vector3 &vector1, const Vector3 &vector2);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The cross product of two vectors
|
/// The cross product of two vectors
|
||||||
@ -200,7 +201,7 @@ public:
|
|||||||
/// <param name="vector1">The first vector</param>
|
/// <param name="vector1">The first vector</param>
|
||||||
/// <param name="vector2">The second vector</param>
|
/// <param name="vector2">The second vector</param>
|
||||||
/// <returns>The cross product of the two vectors</returns>
|
/// <returns>The cross product of the two vectors</returns>
|
||||||
static Vector3 Cross(const Vector3& vector1, const Vector3& vector2);
|
static Vector3 Cross(const Vector3 &vector1, const Vector3 &vector2);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Project a vector on another vector
|
/// Project a vector on another vector
|
||||||
@ -210,13 +211,22 @@ public:
|
|||||||
/// <returns>The projected vector</returns>
|
/// <returns>The projected vector</returns>
|
||||||
static Vector3 Project(Vector3 vector, Vector3 onNormal);
|
static Vector3 Project(Vector3 vector, Vector3 onNormal);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Projects a vector onto a plane defined by a normal orthogonal to the plane.
|
/// Projects a vector onto a plane defined by a normal orthogonal to the
|
||||||
|
/// plane.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="vector">The vector to project</param>
|
/// <param name="vector">The vector to project</param>
|
||||||
/// <param name="planeNormal">The normal of the plane to project on</param>
|
/// <param name="planeNormal">The normal of the plane to project on</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
static Vector3 ProjectOnPlane(Vector3 vector, Vector3 planeNormal);
|
static Vector3 ProjectOnPlane(Vector3 vector, Vector3 planeNormal);
|
||||||
|
|
||||||
|
/// <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>
|
/// <summary>
|
||||||
/// Calculate the angle between two vectors
|
/// Calculate the angle between two vectors
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -237,7 +247,6 @@ public:
|
|||||||
/// <returns>The signed angle</returns>
|
/// <returns>The signed angle</returns>
|
||||||
static float SignedAngle(Vector3 from, Vector3 to, Vector3 axis);
|
static float SignedAngle(Vector3 from, Vector3 to, Vector3 axis);
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Lerp between two vectors
|
/// Lerp between two vectors
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -245,8 +254,9 @@ public:
|
|||||||
/// <param name="to">The to vector</param>
|
/// <param name="to">The to vector</param>
|
||||||
/// <param name="f">The interpolation distance (0..1)</param>
|
/// <param name="f">The interpolation distance (0..1)</param>
|
||||||
/// <returns>The lerped vector</returns>
|
/// <returns>The lerped vector</returns>
|
||||||
/// The factor f is unclamped. Value 0 matches the *from* vector, Value 1 matches the *to* vector
|
/// The factor f is unclamped. Value 0 matches the *from* vector, Value 1
|
||||||
/// Value -1 is *from* vector minus the difference between *from* and *to* etc.
|
/// 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);
|
static Vector3 Lerp(Vector3 from, Vector3 to, float f);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2,8 +2,8 @@
|
|||||||
// License, v. 2.0.If a copy of the MPL was not distributed with this
|
// 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/.
|
// file, You can obtain one at https ://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
#include <math.h>
|
|
||||||
#include "Vector3.h"
|
#include "Vector3.h"
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
const float Deg2Rad = 0.0174532924F;
|
const float Deg2Rad = 0.0174532924F;
|
||||||
const float Rad2Deg = 57.29578F;
|
const float Rad2Deg = 57.29578F;
|
||||||
@ -27,8 +27,7 @@ Vector3::Vector3(Vec3 v) {
|
|||||||
z = v.z;
|
z = v.z;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3::~Vector3() {
|
Vector3::~Vector3() {}
|
||||||
}
|
|
||||||
|
|
||||||
const Vector3 Vector3::zero = Vector3(0, 0, 0);
|
const Vector3 Vector3::zero = Vector3(0, 0, 0);
|
||||||
const Vector3 Vector3::one = Vector3(1, 1, 1);
|
const Vector3 Vector3::one = Vector3(1, 1, 1);
|
||||||
@ -39,19 +38,15 @@ const Vector3 Vector3::down = Vector3(0, -1, 0);
|
|||||||
const Vector3 Vector3::forward = Vector3(0, 0, 1);
|
const Vector3 Vector3::forward = Vector3(0, 0, 1);
|
||||||
const Vector3 Vector3::back = Vector3(0, 0, -1);
|
const Vector3 Vector3::back = Vector3(0, 0, -1);
|
||||||
|
|
||||||
float Vector3::Magnitude(const Vector3& a) {
|
float Vector3::Magnitude(const Vector3 &a) {
|
||||||
return sqrtf(a.x * a.x + a.y * a.y + a.z * a.z);
|
return sqrtf(a.x * a.x + a.y * a.y + a.z * a.z);
|
||||||
}
|
}
|
||||||
float Vector3::magnitude() const {
|
float Vector3::magnitude() const { return (float)sqrtf(x * x + y * y + z * z); }
|
||||||
return (float)sqrtf(x * x + y * y + z * z);
|
|
||||||
}
|
|
||||||
|
|
||||||
float Vector3::SqrMagnitude(const Vector3& a) {
|
float Vector3::SqrMagnitude(const Vector3 &a) {
|
||||||
return a.x * a.x + a.y * a.y + a.z * a.z;
|
return a.x * a.x + a.y * a.y + a.z * a.z;
|
||||||
}
|
}
|
||||||
float Vector3::sqrMagnitude() const {
|
float Vector3::sqrMagnitude() const { return (x * x + y * y + z * z); }
|
||||||
return(x * x + y * y + z * z);
|
|
||||||
}
|
|
||||||
|
|
||||||
Vector3 Vector3::Normalize(Vector3 v) {
|
Vector3 Vector3::Normalize(Vector3 v) {
|
||||||
float num = Vector3::Magnitude(v);
|
float num = Vector3::Magnitude(v);
|
||||||
@ -65,49 +60,48 @@ Vector3 Vector3::normalized() const {
|
|||||||
float num = this->magnitude();
|
float num = this->magnitude();
|
||||||
Vector3 result = Vector3::zero;
|
Vector3 result = Vector3::zero;
|
||||||
if (num > epsilon) {
|
if (num > epsilon) {
|
||||||
result = ((Vector3)*this) / num;
|
result = ((Vector3) * this) / num;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 Vector3::operator -(const Vector3& v2) const {
|
Vector3 Vector3::operator-(const Vector3 &v2) const {
|
||||||
return Vector3(this->x - v2.x, this->y - v2.y, this->z - v2.z);
|
return Vector3(this->x - v2.x, this->y - v2.y, this->z - v2.z);
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 Vector3::operator -() {
|
Vector3 Vector3::operator-() { return Vector3(-this->x, -this->y, -this->z); }
|
||||||
return Vector3(-this->x, -this->y, -this->z);
|
|
||||||
}
|
|
||||||
|
|
||||||
Vector3 Vector3::operator +(const Vector3& v2) const {
|
Vector3 Vector3::operator+(const Vector3 &v2) const {
|
||||||
return Vector3(this->x + v2.x, this->y + v2.y, this->z + v2.z);
|
return Vector3(this->x + v2.x, this->y + v2.y, this->z + v2.z);
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 Vector3::Scale(const Vector3& p1, const Vector3& p2) {
|
Vector3 Vector3::Scale(const Vector3 &p1, const Vector3 &p2) {
|
||||||
return Vector3(p1.x * p2.x, p1.y * p2.y, p1.z * p2.z);
|
return Vector3(p1.x * p2.x, p1.y * p2.y, p1.z * p2.z);
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 Vector3::operator *(float f) const {
|
Vector3 Vector3::operator*(float f) const {
|
||||||
return Vector3(this->x * f, this->y * f, this->z * f);
|
return Vector3(this->x * f, this->y * f, this->z * f);
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 Vector3::operator/(const float& d) {
|
Vector3 Vector3::operator/(const float &d) {
|
||||||
return Vector3(this->x / d, this->y / d, this->z / d);
|
return Vector3(this->x / d, this->y / d, this->z / d);
|
||||||
}
|
}
|
||||||
|
|
||||||
float Vector3::Dot(const Vector3& v1, const Vector3& v2) {
|
float Vector3::Dot(const Vector3 &v1, const Vector3 &v2) {
|
||||||
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
|
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Vector3::operator==(const Vector3& v) {
|
bool Vector3::operator==(const Vector3 &v) {
|
||||||
return (this->x == v.x && this->y == v.y && this->z == v.z);
|
return (this->x == v.x && this->y == v.y && this->z == v.z);
|
||||||
}
|
}
|
||||||
|
|
||||||
float Vector3::Distance(const Vector3& p1, const Vector3& p2) {
|
float Vector3::Distance(const Vector3 &p1, const Vector3 &p2) {
|
||||||
return Magnitude(p1 - p2);
|
return Magnitude(p1 - p2);
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 Vector3::Cross(const Vector3& v1, const Vector3& v2) {
|
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);
|
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) {
|
Vector3 Vector3::Project(Vector3 vector, Vector3 onNormal) {
|
||||||
@ -126,6 +120,11 @@ Vector3 Vector3::ProjectOnPlane(Vector3 vector, Vector3 planeNormal) {
|
|||||||
return r;
|
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 clamp(float x, float lower, float upper) {
|
||||||
float lowerClamp = fmaxf(x, lower);
|
float lowerClamp = fmaxf(x, lower);
|
||||||
float upperClamp = fminf(upper, lowerClamp);
|
float upperClamp = fminf(upper, lowerClamp);
|
||||||
@ -159,7 +158,6 @@ float Vector3::SignedAngle(Vector3 from, Vector3 to, Vector3 axis) {
|
|||||||
float signed_angle = angle * signd;
|
float signed_angle = angle * signd;
|
||||||
|
|
||||||
return signed_angle;
|
return signed_angle;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 Vector3::Lerp(Vector3 from, Vector3 to, float f) {
|
Vector3 Vector3::Lerp(Vector3 from, Vector3 to, float f) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user