diff --git a/Engine/source/math/mConsoleFunctions.cpp b/Engine/source/math/mConsoleFunctions.cpp index 08ebf72ba..651889539 100644 --- a/Engine/source/math/mConsoleFunctions.cpp +++ b/Engine/source/math/mConsoleFunctions.cpp @@ -373,3 +373,20 @@ DefineConsoleFunction( mGetAngleBetweenVectors, F32, (VectorF vecA, VectorF vecB { return MathUtils::getAngleBetweenVectors(vecA, vecB); } + +DefineConsoleFunction(mGetSignedAngleBetweenVectors, F32, (VectorF vecA, VectorF vecB, VectorF norm), (VectorF::Zero, VectorF::Zero, VectorF::Zero), + "Returns signed angle between two vectors, using a normal for orientation.\n" + "@param vecA First input vector." + "@param vecB Second input vector." + "@param norm Normal/Cross Product vector." + "@returns Angle between both vectors in radians." + "@ingroup Math") +{ + if (vecA.isZero() || vecB.isZero() || norm.isZero()) + { + Con::errorf("mGetSignedAngleBetweenVectors - Error! Requires all 3 vectors used to be non-zero!"); + return 0; + } + + return MathUtils::getSignedAngleBetweenVectors(vecA, vecB, norm); +} \ No newline at end of file diff --git a/Engine/source/math/mathUtils.cpp b/Engine/source/math/mathUtils.cpp index 48a4c6d99..19263e0a5 100644 --- a/Engine/source/math/mathUtils.cpp +++ b/Engine/source/math/mathUtils.cpp @@ -371,6 +371,18 @@ F32 getAngleBetweenVectors(VectorF vecA, VectorF vecB) return angle; } +F32 getSignedAngleBetweenVectors(VectorF vecA, VectorF vecB, VectorF norm) +{ + // angle in 0-180 + F32 angle = getAngleBetweenVectors(vecA, vecB); + F32 sign = mSign(mDot(norm, mCross(vecA, vecB))); + + // angle in -179-180 + F32 signed_angle = angle * sign; + + return signed_angle; +} + //----------------------------------------------------------------------------- void transformBoundingBox(const Box3F &sbox, const MatrixF &mat, const Point3F scale, Box3F &dbox) diff --git a/Engine/source/math/mathUtils.h b/Engine/source/math/mathUtils.h index cd4ac9a15..3787ebfeb 100644 --- a/Engine/source/math/mathUtils.h +++ b/Engine/source/math/mathUtils.h @@ -161,6 +161,11 @@ namespace MathUtils /// F32 getAngleBetweenVectors(VectorF vecA, VectorF vecB); + /// Returns the angle between two given vectors, utilizing a normal vector to discertain the angle's sign + /// + /// Angles is in RADIANS + /// + F32 getSignedAngleBetweenVectors(VectorF vecA, VectorF vecB, VectorF norm); /// Simple reflection equation - pass in a vector and a normal to reflect off of inline Point3F reflect( Point3F &inVec, Point3F &norm )