]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/game/IK.h
hello world
[icculus/iodoom3.git] / neo / game / IK.h
1 /*
2 ===========================================================================
3
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company. 
6
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).  
8
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code.  If not, see <http://www.gnu.org/licenses/>.
21
22 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.
23
24 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.
25
26 ===========================================================================
27 */
28
29 #ifndef __GAME_IK_H__
30 #define __GAME_IK_H__
31
32 /*
33 ===============================================================================
34
35   IK base class with a simple fast two bone solver.
36
37 ===============================================================================
38 */
39
40 #define IK_ANIM                         "ik_pose"
41
42 class idIK {
43 public:
44                                                         idIK( void );
45         virtual                                 ~idIK( void );
46
47         void                                    Save( idSaveGame *savefile ) const;
48         void                                    Restore( idRestoreGame *savefile );
49
50         bool                                    IsInitialized( void ) const;
51
52         virtual bool                    Init( idEntity *self, const char *anim, const idVec3 &modelOffset );
53         virtual void                    Evaluate( void );
54         virtual void                    ClearJointMods( void );
55
56         bool                                    SolveTwoBones( const idVec3 &startPos, const idVec3 &endPos, const idVec3 &dir, float len0, float len1, idVec3 &jointPos );
57         float                                   GetBoneAxis( const idVec3 &startPos, const idVec3 &endPos, const idVec3 &dir, idMat3 &axis );
58
59 protected:
60         bool                                    initialized;
61         bool                                    ik_activate;
62         idEntity *                              self;                           // entity using the animated model
63         idAnimator *                    animator;                       // animator on entity
64         int                                             modifiedAnim;           // animation modified by the IK
65         idVec3                                  modelOffset;
66 };
67
68
69 /*
70 ===============================================================================
71
72   IK controller for a walking character with an arbitrary number of legs.       
73
74 ===============================================================================
75 */
76
77 class idIK_Walk : public idIK {
78 public:
79
80                                                         idIK_Walk( void );
81         virtual                                 ~idIK_Walk( void );
82
83         void                                    Save( idSaveGame *savefile ) const;
84         void                                    Restore( idRestoreGame *savefile );
85
86         virtual bool                    Init( idEntity *self, const char *anim, const idVec3 &modelOffset );
87         virtual void                    Evaluate( void );
88         virtual void                    ClearJointMods( void );
89
90         void                                    EnableAll( void );
91         void                                    DisableAll( void );
92         void                                    EnableLeg( int num );
93         void                                    DisableLeg( int num );
94
95 private:
96         static const int                MAX_LEGS                = 8;
97
98         idClipModel *                   footModel;
99
100         int                                             numLegs;
101         int                                             enabledLegs;
102         jointHandle_t                   footJoints[MAX_LEGS];
103         jointHandle_t                   ankleJoints[MAX_LEGS];
104         jointHandle_t                   kneeJoints[MAX_LEGS];
105         jointHandle_t                   hipJoints[MAX_LEGS];
106         jointHandle_t                   dirJoints[MAX_LEGS];
107         jointHandle_t                   waistJoint;
108
109         idVec3                                  hipForward[MAX_LEGS];
110         idVec3                                  kneeForward[MAX_LEGS];
111
112         float                                   upperLegLength[MAX_LEGS];
113         float                                   lowerLegLength[MAX_LEGS];
114
115         idMat3                                  upperLegToHipJoint[MAX_LEGS];
116         idMat3                                  lowerLegToKneeJoint[MAX_LEGS];
117
118         float                                   smoothing;
119         float                                   waistSmoothing;
120         float                                   footShift;
121         float                                   waistShift;
122         float                                   minWaistFloorDist;
123         float                                   minWaistAnkleDist;
124         float                                   footUpTrace;
125         float                                   footDownTrace;
126         bool                                    tiltWaist;
127         bool                                    usePivot;
128
129         // state
130         int                                             pivotFoot;
131         float                                   pivotYaw;
132         idVec3                                  pivotPos;
133         bool                                    oldHeightsValid;
134         float                                   oldWaistHeight;
135         float                                   oldAnkleHeights[MAX_LEGS];
136         idVec3                                  waistOffset;
137 };
138
139
140 /*
141 ===============================================================================
142
143   IK controller for reaching a position with an arm or leg.
144
145 ===============================================================================
146 */
147
148 class idIK_Reach : public idIK {
149 public:
150
151                                                         idIK_Reach( void );
152         virtual                                 ~idIK_Reach( void );
153
154         void                                    Save( idSaveGame *savefile ) const;
155         void                                    Restore( idRestoreGame *savefile );
156
157         virtual bool                    Init( idEntity *self, const char *anim, const idVec3 &modelOffset );
158         virtual void                    Evaluate( void );
159         virtual void                    ClearJointMods( void );
160
161 private:
162
163         static const int                MAX_ARMS        = 2;
164
165         int                                             numArms;
166         int                                             enabledArms;
167         jointHandle_t                   handJoints[MAX_ARMS];
168         jointHandle_t                   elbowJoints[MAX_ARMS];
169         jointHandle_t                   shoulderJoints[MAX_ARMS];
170         jointHandle_t                   dirJoints[MAX_ARMS];
171
172         idVec3                                  shoulderForward[MAX_ARMS];
173         idVec3                                  elbowForward[MAX_ARMS];
174
175         float                                   upperArmLength[MAX_ARMS];
176         float                                   lowerArmLength[MAX_ARMS];
177
178         idMat3                                  upperArmToShoulderJoint[MAX_ARMS];
179         idMat3                                  lowerArmToElbowJoint[MAX_ARMS];
180 };
181
182 #endif /* !__GAME_IK_H__ */