diff --git a/src/mathematics/Vector2.h b/src/mathematics/Vector2.h index dfc10b65..7a96aa93 100644 --- a/src/mathematics/Vector2.h +++ b/src/mathematics/Vector2.h @@ -141,6 +141,9 @@ struct Vector2 { /// Return a vector taking the maximum components of two vectors static Vector2 max(const Vector2& vector1, const Vector2& vector2); + /// Return the zero vector + static Vector2 zero(); + // -------------------- Friends -------------------- // friend Vector2 operator+(const Vector2& vector1, const Vector2& vector2); @@ -333,6 +336,11 @@ inline Vector2 Vector2::max(const Vector2& vector1, const Vector2& vector2) { std::max(vector1.y, vector2.y)); } +// Return the zero vector +inline Vector2 Vector2::zero() { + return Vector2(0, 0); +} + } #endif diff --git a/src/mathematics/Vector3.h b/src/mathematics/Vector3.h index 4ab37db3..fc6f4629 100644 --- a/src/mathematics/Vector3.h +++ b/src/mathematics/Vector3.h @@ -153,6 +153,9 @@ struct Vector3 { /// Return a vector taking the maximum components of two vectors static Vector3 max(const Vector3& vector1, const Vector3& vector2); + /// Return the zero vector + static Vector3 zero(); + // -------------------- Friends -------------------- // friend Vector3 operator+(const Vector3& vector1, const Vector3& vector2); @@ -373,6 +376,11 @@ inline decimal Vector3::getMaxValue() const { return std::max(std::max(x, y), z); } +// Return the zero vector +inline Vector3 Vector3::zero() { + return Vector3(0, 0, 0); +} + } #endif diff --git a/src/mathematics/mathematics_functions.cpp b/src/mathematics/mathematics_functions.cpp index 95bc3d65..54aa0878 100644 --- a/src/mathematics/mathematics_functions.cpp +++ b/src/mathematics/mathematics_functions.cpp @@ -49,3 +49,11 @@ void reactphysics3d::computeBarycentricCoordinatesInTriangle(const Vector3& a, c w = (d00 * d21 - d01 * d20) / denom; u = decimal(1.0) - u - w; } + +// Clamp a vector such that it is no longer than a given maximum length +Vector3 reactphysics3d::clamp(const Vector3& vector, decimal maxLength) { + if (vector.lengthSquare() > maxLength * maxLength) { + return vector.getUnit() * maxLength; + } + return vector; +} diff --git a/src/mathematics/mathematics_functions.h b/src/mathematics/mathematics_functions.h index a302bf62..243a7d48 100644 --- a/src/mathematics/mathematics_functions.h +++ b/src/mathematics/mathematics_functions.h @@ -75,6 +75,9 @@ inline bool sameSign(decimal a, decimal b) { return a * b >= decimal(0.0); } +/// Clamp a vector such that it is no longer than a given maximum length +Vector3 clamp(const Vector3& vector, decimal maxLength); + /// Compute the barycentric coordinates u, v, w of a point p inside the triangle (a, b, c) void computeBarycentricCoordinatesInTriangle(const Vector3& a, const Vector3& b, const Vector3& c, const Vector3& p, decimal& u, decimal& v, decimal& w);