Add projecton on horizontal plane

This commit is contained in:
Pascal Serrarens 2023-12-29 12:27:44 +01:00
parent aaf335d218
commit 91027b2e41
2 changed files with 318 additions and 310 deletions

View File

@ -5,6 +5,8 @@
#ifndef VECTOR3_H
#define VECTOR3_H
#include "Vector2.h"
extern "C" {
/// <summary>
/// 3-dimensional Vector representation
@ -99,11 +101,10 @@ public:
/// <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.
/// <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
@ -210,13 +211,22 @@ public:
/// <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.
/// 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>
/// 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
/// </summary>
@ -237,7 +247,6 @@ public:
/// <returns>The signed angle</returns>
static float SignedAngle(Vector3 from, Vector3 to, Vector3 axis);
/// <summary>
/// Lerp between two vectors
/// </summary>
@ -245,8 +254,9 @@ public:
/// <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.
/// 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);
};

View File

@ -2,8 +2,8 @@
// 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;
@ -27,8 +27,7 @@ Vector3::Vector3(Vec3 v) {
z = v.z;
}
Vector3::~Vector3() {
}
Vector3::~Vector3() {}
const Vector3 Vector3::zero = Vector3(0, 0, 0);
const Vector3 Vector3::one = Vector3(1, 1, 1);
@ -42,16 +41,12 @@ 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 { 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 { return (x * x + y * y + z * z); }
Vector3 Vector3::Normalize(Vector3 v) {
float num = Vector3::Magnitude(v);
@ -74,9 +69,7 @@ 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);
@ -107,7 +100,8 @@ float Vector3::Distance(const Vector3& p1, const Vector3& 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);
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) {
@ -126,6 +120,11 @@ Vector3 Vector3::ProjectOnPlane(Vector3 vector, Vector3 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);
@ -159,7 +158,6 @@ float Vector3::SignedAngle(Vector3 from, Vector3 to, Vector3 axis) {
float signed_angle = angle * signd;
return signed_angle;
}
Vector3 Vector3::Lerp(Vector3 from, Vector3 to, float f) {