Просмотр исходного кода

adds an mWrap and mWrapF method for cycling values to within a given range.
examples of usage would be say, keeping a rotation within 360 degrees, or hitting a tile boundary and resetting the offset

Azaezel 7 лет назад
Родитель
Сommit
d57287cf75
2 измененных файлов с 36 добавлено и 0 удалено
  1. 23 0
      Engine/source/math/mConsoleFunctions.cpp
  2. 13 0
      Engine/source/math/mMathFn.h

+ 23 - 0
Engine/source/math/mConsoleFunctions.cpp

@@ -288,6 +288,29 @@ DefineConsoleFunction( mSaturate, F32, ( F32 v ),,
    return mClampF( v, 0.0f, 1.0f );
    return mClampF( v, 0.0f, 1.0f );
 }
 }
 
 
+DefineConsoleFunction(mWrapF, F32, (F32 v, F32 min, F32 max), ,
+	"Wrap the specified value between two bounds.\n"
+	"@param v Input value."
+	"@param min Minimum Bound."
+	"@param max Maximum Bound."
+	"@returns The specified value wrapped to the specified bounds."
+	"@ingroup Math")
+{
+	return mWrapF(v, min, max);
+}
+
+DefineConsoleFunction(mWrap, S32, (S32 v, S32 min, S32 max), ,
+	"Wrap the specified value between two bounds.\n"
+	"@param v Input value."
+	"@param min Minimum Bound."
+	"@param max Maximum Bound."
+	"@returns The specified value wrapped to the specified bounds."
+	"@ingroup Math")
+{
+	return mWrap(v, min, max);
+}
+
+
 DefineConsoleFunction( getMax, F32, ( F32 v1, F32 v2 ),,
 DefineConsoleFunction( getMax, F32, ( F32 v1, F32 v2 ),,
     "Calculate the greater of two specified numbers.\n"
     "Calculate the greater of two specified numbers.\n"
     "@param v1 Input value."
     "@param v1 Input value."

+ 13 - 0
Engine/source/math/mMathFn.h

@@ -237,6 +237,19 @@ inline F32 mClampF(F32 val, F32 low, F32 high)
    return (F32) getMax(getMin(val, high), low);
    return (F32) getMax(getMin(val, high), low);
 }
 }
 
 
+inline S32 mWrap(S32 val, S32 low, S32 high)
+{
+	int len = high - low;
+	return low + (val >= 0 ? val % len : -val % len ? len - (-val % len) : 0);
+
+}
+
+inline F32 mWrapF(F32 val, F32 low, F32 high)
+{
+	F32 t = fmod(val - low, high - low);
+	return t < 0 ? t + high : t + low;
+}
+
 /// Template function for doing a linear interpolation between any two
 /// Template function for doing a linear interpolation between any two
 /// types which implement operators for scalar multiply and addition.
 /// types which implement operators for scalar multiply and addition.
 template <typename T>
 template <typename T>