VInterpController.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //-----------------------------------------------------------------------------
  2. // Verve
  3. // Copyright (C) 2014 - Violent Tulip
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //-----------------------------------------------------------------------------
  23. #ifndef _VT_VINTERPCONTROLLER_H_
  24. #define _VT_VINTERPCONTROLLER_H_
  25. #ifndef _MATH_H_
  26. #include "math/mMath.h"
  27. #endif
  28. //-----------------------------------------------------------------------------
  29. class VInterpController
  30. {
  31. protected:
  32. Point3F mPosition[2];
  33. QuatF mRotation[2];
  34. public:
  35. //-------------------------------------------------------------------------
  36. // Interpolation Methods.
  37. //-------------------------------------------------------------------------
  38. /// Get Position.
  39. Point3F getPosition( const F32 &pDelta )
  40. {
  41. // Interpolate Position.
  42. Point3F interpPosition;
  43. interpPosition.interpolate( mPosition[1], mPosition[0], pDelta );
  44. // Return Interpolated Point.
  45. return interpPosition;
  46. };
  47. /// Get Rotation.
  48. QuatF getRotation( const F32 &pDelta )
  49. {
  50. // Interpolate Rotation.
  51. QuatF interpRotation;
  52. interpRotation.interpolate( mRotation[1], mRotation[0], pDelta );
  53. // Return Interpolated Quat.
  54. return interpRotation;
  55. };
  56. /// Get Transform.
  57. MatrixF getTransform( const F32 &pDelta )
  58. {
  59. // Get Position.
  60. const Point3F interpPosition = getPosition( pDelta );
  61. // Get Rotation.
  62. const QuatF interpRotation = getRotation( pDelta );
  63. // Setup Matrix.
  64. MatrixF transform;
  65. interpRotation.setMatrix( &transform );
  66. // Set Position.
  67. transform.setPosition( interpPosition );
  68. // Return Matrix.
  69. return transform;
  70. };
  71. //-------------------------------------------------------------------------
  72. // Delta Methods.
  73. //-------------------------------------------------------------------------
  74. /// Reset Delta.
  75. void resetDelta( const Point3F &pPosition, const QuatF &pRotation )
  76. {
  77. mPosition[0] = mPosition[1] = pPosition;
  78. mRotation[0] = mRotation[1] = pRotation;
  79. };
  80. /// Reset Delta.
  81. void resetDelta( const MatrixF &pMatrix )
  82. {
  83. // Setup Quat.
  84. QuatF rotationQuat( pMatrix );
  85. // Reset Delta.
  86. resetDelta( pMatrix.getPosition(), rotationQuat );
  87. };
  88. /// Reset Delta (Vector)
  89. void resetDelta( const Point3F &pPosition, const VectorF &pForwardVector )
  90. {
  91. // Assert.
  92. AssertFatal( pForwardVector.isUnitLength(), "VInterpController::resetDelta() - Forward Vector hasn't been Normalized." );
  93. // Static Up Vector.
  94. static const VectorF sUpVector( 0.f, 0.f, 1.f );
  95. // X-Axis.
  96. VectorF xVec = mCross( pForwardVector, sUpVector );
  97. xVec.normalize();
  98. // Z-Axis.
  99. VectorF zVec = mCross( xVec, pForwardVector );
  100. zVec.normalize();
  101. // Setup Object Transform.
  102. MatrixF transform;
  103. transform.setColumn( 0, xVec );
  104. transform.setColumn( 1, pForwardVector );
  105. transform.setColumn( 2, zVec );
  106. transform.setColumn( 3, pPosition );
  107. // Reset Delta.
  108. resetDelta( transform );
  109. };
  110. /// Reset Delta (AngAxis)
  111. void resetDelta( const Point3F &pPosition, const AngAxisF &pAngAxis )
  112. {
  113. // Setup Matrix.
  114. MatrixF transform;
  115. pAngAxis.setMatrix( &transform );
  116. // Set Position.
  117. transform.setPosition( pPosition );
  118. // Reset Delta.
  119. resetDelta( transform );
  120. };
  121. /// Push Delta.
  122. void pushDelta( const Point3F &pPosition, const QuatF &pRotation )
  123. {
  124. mPosition[1] = pPosition;
  125. mRotation[1] = pRotation;
  126. };
  127. /// Push Delta (Matrix)
  128. void pushDelta( const MatrixF &pMatrix )
  129. {
  130. // Setup Quat.
  131. QuatF rotationQuat( pMatrix );
  132. // Push Delta.
  133. pushDelta( pMatrix.getPosition(), rotationQuat );
  134. };
  135. /// Push Delta (Vector)
  136. void pushDelta( const Point3F &pPosition, const VectorF &pForwardVector )
  137. {
  138. // Assert.
  139. AssertFatal( pForwardVector.isUnitLength(), "VInterpController::pushDelta() - Forward Vector hasn't been Normalized." );
  140. // Static Up Vector.
  141. static const VectorF sUpVector( 0.f, 0.f, 1.f );
  142. // X-Axis.
  143. VectorF xVec = mCross( pForwardVector, sUpVector );
  144. xVec.normalize();
  145. // Z-Axis.
  146. VectorF zVec = mCross( xVec, pForwardVector );
  147. zVec.normalize();
  148. // Setup Object Transform.
  149. MatrixF transform;
  150. transform.setColumn( 0, xVec );
  151. transform.setColumn( 1, pForwardVector );
  152. transform.setColumn( 2, zVec );
  153. transform.setColumn( 3, pPosition );
  154. // Push Delta.
  155. pushDelta( transform );
  156. };
  157. /// Push Delta (AngAxis)
  158. void pushDelta( const Point3F &pPosition, const AngAxisF &pAngAxis )
  159. {
  160. // Setup Matrix.
  161. MatrixF transform;
  162. pAngAxis.setMatrix( &transform );
  163. // Set Position.
  164. transform.setPosition( pPosition );
  165. // Push Delta.
  166. pushDelta( transform );
  167. };
  168. /// Pop Delta.
  169. void popDelta( void )
  170. {
  171. mPosition[0] = mPosition[1];
  172. mRotation[0] = mRotation[1];
  173. };
  174. };
  175. #endif // _VT_VINTERPCONTROLLER_H_