IK.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __GAME_IK_H__
  21. #define __GAME_IK_H__
  22. /*
  23. ===============================================================================
  24. IK base class with a simple fast two bone solver.
  25. ===============================================================================
  26. */
  27. #define IK_ANIM "ik_pose"
  28. class idIK {
  29. public:
  30. idIK( void );
  31. virtual ~idIK( void );
  32. void Save( idSaveGame *savefile ) const;
  33. void Restore( idRestoreGame *savefile );
  34. bool IsInitialized( void ) const;
  35. virtual bool Init( idEntity *self, const char *anim, const idVec3 &modelOffset );
  36. virtual void Evaluate( void );
  37. virtual void ClearJointMods( void );
  38. bool SolveTwoBones( const idVec3 &startPos, const idVec3 &endPos, const idVec3 &dir, float len0, float len1, idVec3 &jointPos );
  39. float GetBoneAxis( const idVec3 &startPos, const idVec3 &endPos, const idVec3 &dir, idMat3 &axis );
  40. protected:
  41. bool initialized;
  42. bool ik_activate;
  43. idEntity * self; // entity using the animated model
  44. idAnimator * animator; // animator on entity
  45. int modifiedAnim; // animation modified by the IK
  46. idVec3 modelOffset;
  47. };
  48. /*
  49. ===============================================================================
  50. IK controller for a walking character with an arbitrary number of legs.
  51. ===============================================================================
  52. */
  53. class idIK_Walk : public idIK {
  54. public:
  55. idIK_Walk( void );
  56. virtual ~idIK_Walk( void );
  57. void Save( idSaveGame *savefile ) const;
  58. void Restore( idRestoreGame *savefile );
  59. virtual bool Init( idEntity *self, const char *anim, const idVec3 &modelOffset );
  60. virtual void Evaluate( void );
  61. virtual void ClearJointMods( void );
  62. void EnableAll( void );
  63. void DisableAll( void );
  64. void EnableLeg( int num );
  65. void DisableLeg( int num );
  66. private:
  67. static const int MAX_LEGS = 8;
  68. idClipModel * footModel;
  69. int numLegs;
  70. int enabledLegs;
  71. jointHandle_t footJoints[MAX_LEGS];
  72. jointHandle_t ankleJoints[MAX_LEGS];
  73. jointHandle_t kneeJoints[MAX_LEGS];
  74. jointHandle_t hipJoints[MAX_LEGS];
  75. jointHandle_t dirJoints[MAX_LEGS];
  76. jointHandle_t waistJoint;
  77. idVec3 hipForward[MAX_LEGS];
  78. idVec3 kneeForward[MAX_LEGS];
  79. float upperLegLength[MAX_LEGS];
  80. float lowerLegLength[MAX_LEGS];
  81. idMat3 upperLegToHipJoint[MAX_LEGS];
  82. idMat3 lowerLegToKneeJoint[MAX_LEGS];
  83. float smoothing;
  84. float waistSmoothing;
  85. float footShift;
  86. float waistShift;
  87. float minWaistFloorDist;
  88. float minWaistAnkleDist;
  89. float footUpTrace;
  90. float footDownTrace;
  91. bool tiltWaist;
  92. bool usePivot;
  93. // state
  94. int pivotFoot;
  95. float pivotYaw;
  96. idVec3 pivotPos;
  97. bool oldHeightsValid;
  98. float oldWaistHeight;
  99. float oldAnkleHeights[MAX_LEGS];
  100. idVec3 waistOffset;
  101. };
  102. /*
  103. ===============================================================================
  104. IK controller for reaching a position with an arm or leg.
  105. ===============================================================================
  106. */
  107. class idIK_Reach : public idIK {
  108. public:
  109. idIK_Reach( void );
  110. virtual ~idIK_Reach( void );
  111. void Save( idSaveGame *savefile ) const;
  112. void Restore( idRestoreGame *savefile );
  113. virtual bool Init( idEntity *self, const char *anim, const idVec3 &modelOffset );
  114. virtual void Evaluate( void );
  115. virtual void ClearJointMods( void );
  116. private:
  117. static const int MAX_ARMS = 2;
  118. int numArms;
  119. int enabledArms;
  120. jointHandle_t handJoints[MAX_ARMS];
  121. jointHandle_t elbowJoints[MAX_ARMS];
  122. jointHandle_t shoulderJoints[MAX_ARMS];
  123. jointHandle_t dirJoints[MAX_ARMS];
  124. idVec3 shoulderForward[MAX_ARMS];
  125. idVec3 elbowForward[MAX_ARMS];
  126. float upperArmLength[MAX_ARMS];
  127. float lowerArmLength[MAX_ARMS];
  128. idMat3 upperArmToShoulderJoint[MAX_ARMS];
  129. idMat3 lowerArmToElbowJoint[MAX_ARMS];
  130. };
  131. #endif /* !__GAME_IK_H__ */