From 31ed509c1cc4a2c028e7a5af31a0a4964e2f0d23 Mon Sep 17 00:00:00 2001 From: Areloch Date: Mon, 28 Nov 2016 22:42:29 -0600 Subject: [PATCH] Adds some helpful utility math functions. --- Engine/source/math/mConsoleFunctions.cpp | 32 ++++++++++++++++++++++++ Engine/source/math/mRotation.cpp | 19 ++++++++++++++ Engine/source/math/mRotation.h | 1 + Engine/source/math/mathTypes.cpp | 12 +++++++++ Engine/source/math/mathUtils.cpp | 10 ++++++++ Engine/source/math/mathUtils.h | 7 ++++++ 6 files changed, 81 insertions(+) diff --git a/Engine/source/math/mConsoleFunctions.cpp b/Engine/source/math/mConsoleFunctions.cpp index 1d57743e6..08ebf72ba 100644 --- a/Engine/source/math/mConsoleFunctions.cpp +++ b/Engine/source/math/mConsoleFunctions.cpp @@ -25,6 +25,8 @@ #include "console/console.h" #include "math/mMathFn.h" #include "math/mRandom.h" +#include "math/mMath.h" +#include "math/mathUtils.h" #include "console/engineAPI.h" @@ -341,3 +343,33 @@ DefineConsoleFunction( mIsPow2, bool, ( S32 v ),, { return isPow2( v ); } + +DefineConsoleFunction( mRandomDir, Point3F, (Point3F axis, F32 angleMin, F32 angleMax),, + "Returns a randomized direction based on a starting axis and the min/max angles.\n" + "@param axis Main axis to deviate the direction from." + "@param angleMin minimum amount of deviation from the axis." + "@param angleMax maximum amount of deviation from the axis." + "@returns Randomized direction vector." + "@ingroup Math") +{ + return MathUtils::randomDir(axis, angleMin, angleMax); +} + +DefineConsoleFunction( mRandomPointInSphere, Point3F, (F32 radius), , + "Returns a randomized point inside a sphere of a given radius.\n" + "@param radius The radius of the sphere to find a point in." + "@returns Randomized point inside a sphere." + "@ingroup Math") +{ + return MathUtils::randomPointInSphere(radius); +} + +DefineConsoleFunction( mGetAngleBetweenVectors, F32, (VectorF vecA, VectorF vecB), , + "Returns angle between two vectors.\n" + "@param vecA First input vector." + "@param vecB Second input vector." + "@returns Angle between both vectors in radians." + "@ingroup Math") +{ + return MathUtils::getAngleBetweenVectors(vecA, vecB); +} diff --git a/Engine/source/math/mRotation.cpp b/Engine/source/math/mRotation.cpp index 436f992cf..fad915888 100644 --- a/Engine/source/math/mRotation.cpp +++ b/Engine/source/math/mRotation.cpp @@ -22,6 +22,7 @@ #include "math/mRotation.h" #include "console/console.h" #include "console/engineAPI.h" +#include "math/mathUtils.h" #ifdef TORQUE_TESTS_ENABLED #include "testing/unitTesting.h" @@ -187,6 +188,15 @@ void RotationF::lookAt(const Point3F& _origin, const Point3F& _target, const Poi set(mat); } +VectorF RotationF::getDirection() +{ + VectorF dir; + EulerF angles = asEulerF(); + MathUtils::getVectorFromAngles(dir, angles.z, angles.x); + + return dir; +} + //======================================================== EulerF RotationF::asEulerF(UnitFormat _format) const { @@ -346,3 +356,12 @@ DefineConsoleStaticMethod(rotation, LookAt, RotationF, (Point3F origin, Point3F result.lookAt(origin, target, up); return result; } + +DefineConsoleStaticMethod(rotation, getDirection, Point3F, (RotationF rot),, +"Takes the angles of the provided rotation and returns a direction vector.\n" +"@param rot Our rotation." +"@returns v Direction vector result." +"@ingroup Math") +{ + return rot.getDirection(); +} diff --git a/Engine/source/math/mRotation.h b/Engine/source/math/mRotation.h index a99510b8c..0ec23f2e1 100644 --- a/Engine/source/math/mRotation.h +++ b/Engine/source/math/mRotation.h @@ -132,6 +132,7 @@ public: // void interpolate(const RotationF& _pt1, const RotationF& _pt2, F32 _factor); void lookAt(const Point3F& _origin, const Point3F& _target, const Point3F& _up = Point3F(0, 0, 1)); + VectorF getDirection(); F32 len() const; diff --git a/Engine/source/math/mathTypes.cpp b/Engine/source/math/mathTypes.cpp index 9e5605207..1d1011d0f 100644 --- a/Engine/source/math/mathTypes.cpp +++ b/Engine/source/math/mathTypes.cpp @@ -1032,6 +1032,18 @@ DefineConsoleFunction( VectorLerp, VectorF, ( VectorF a, VectorF b, F32 t ),, return c; } +DefineConsoleFunction(VectorReflect, VectorF, (VectorF vec, VectorF normal), , + "Compute the reflection of a vector based on a normal.\n" + "@param a The vector.\n" + "@param b The normal.\n" + "@return The reflected vector.\n\n" + "@ingroup Vectors") +{ + normal.normalize(); + + return MathUtils::reflect(vec, normal); +} + //----------------------------------------------------------------------------- DefineConsoleFunction( MatrixCreate, TransformF, ( VectorF position, AngAxisF orientation ),, diff --git a/Engine/source/math/mathUtils.cpp b/Engine/source/math/mathUtils.cpp index dba228fde..48a4c6d99 100644 --- a/Engine/source/math/mathUtils.cpp +++ b/Engine/source/math/mathUtils.cpp @@ -361,6 +361,16 @@ void getVectorFromAngles( VectorF &vec, F32 yawAng, F32 pitchAng ) vec = pnt; } +F32 getAngleBetweenVectors(VectorF vecA, VectorF vecB) +{ + F32 dot = mDot(vecA, vecB); + F32 lenSq1 = vecA.lenSquared(); + F32 lenSq2 = vecB.lenSquared(); + F32 angle = mAcos(dot / mSqrt(lenSq1 * lenSq2)); + + return 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 ee7be9c2a..cd4ac9a15 100644 --- a/Engine/source/math/mathUtils.h +++ b/Engine/source/math/mathUtils.h @@ -155,6 +155,13 @@ namespace MathUtils /// ASSUMES Z AXIS IS UP void getVectorFromAngles( VectorF &vec, F32 yawAng, F32 pitchAng ); + /// Returns the angle between two given vectors + /// + /// Angles is in RADIANS + /// + F32 getAngleBetweenVectors(VectorF vecA, VectorF vecB); + + /// Simple reflection equation - pass in a vector and a normal to reflect off of inline Point3F reflect( Point3F &inVec, Point3F &norm ) {