From 909e544ed322b28a6f59febf3213e05068e9e93c Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 7 Jan 2019 09:53:21 +0800 Subject: *slerp --- src/libjin/math/je_math.h | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/libjin/math/je_math.h b/src/libjin/math/je_math.h index f0a0a4c..4036696 100644 --- a/src/libjin/math/je_math.h +++ b/src/libjin/math/je_math.h @@ -59,12 +59,6 @@ namespace JinEngine return a > upper ? upper : a; } - template - inline T lerp(T a, T b, float t) - { - return a + t * (b - a); - } - inline float lerp(float a, float b, float f) { f = clamp(f, 0, 1); @@ -83,6 +77,38 @@ namespace JinEngine return -a; } + template + inline T lerp(T a, T b, float t) + { + return a + t * (b - a); + } + + template + inline T slerp(T start, T end, float percent) + { + // Dot product - the cosine of the angle between 2 vectors. + float dot = start * end; + // Clamp it to be in the range of Acos() + // This may be unnecessary, but floating point + // precision can be a fickle mistress. + dot = clamp(dot, -1.0f, 1.0f); + // Acos(dot) returns the angle between start and end, + // And multiplying that by percent returns the angle between + // start and the final result. + float theta = Mathf.Acos(dot)*percent; + Vector3 RelativeVec = end - start * dot; + RelativeVec.Normalize(); + // Orthonormal basis + // The final result. + return ((start*Mathf.Cos(theta)) + (RelativeVec*Mathf.Sin(theta))); + } + + template + inline T nlerp(T a, T b, float t) + { + + } + } // namespace Math } // namespace JinEngine -- cgit v1.1-26-g67d0