Adds some helpful utility math functions.

This commit is contained in:
Areloch 2016-11-28 22:42:29 -06:00
parent 7af95e6a8e
commit 31ed509c1c
6 changed files with 81 additions and 0 deletions

View file

@ -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);
}

View file

@ -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();
}

View file

@ -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;

View file

@ -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 ),,

View file

@ -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)

View file

@ -155,6 +155,13 @@ namespace MathUtils
/// <b>ASSUMES Z AXIS IS UP</b>
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 )
{