ScummVM API documentation
PhysicsJoint.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /*
23  * Copyright (C) 2006-2010 - Frictional Games
24  *
25  * This file is part of HPL1 Engine.
26  */
27 
28 #ifndef HPL_PHYSICS_JOINT_H
29 #define HPL_PHYSICS_JOINT_H
30 
31 #include "hpl1/engine/math/MathTypes.h"
32 #include "hpl1/engine/system/SystemTypes.h"
33 #include "common/stablemap.h"
34 
35 #include "hpl1/engine/game/SaveGame.h"
36 
37 #include "hpl1/engine/physics/PhysicsController.h"
38 
39 namespace hpl {
40 
41 class iPhysicsBody;
42 class iPhysicsWorld;
43 class cSoundEntity;
44 class iPhysicsJoint;
45 class iPhysicsController;
46 
47 typedef Common::StableMap<tString, iPhysicsController *> tPhysicsControllerMap;
48 typedef tPhysicsControllerMap::iterator tPhysicsControllerMapIt;
49 
50 typedef cSTLMapIterator<iPhysicsController *, tPhysicsControllerMap, tPhysicsControllerMapIt> cPhysicsControllerIterator;
51 
52 //-----------------------------------
53 
54 enum ePhysicsJointType {
55  ePhysicsJointType_Ball,
56  ePhysicsJointType_Hinge,
57  ePhysicsJointType_Slider,
58  ePhysicsJointType_Screw,
59  ePhysicsJointType_LastEnum
60 };
61 
62 //-----------------------------------
63 
64 enum ePhysicsJointSpeed {
65  ePhysicsJointSpeed_Linear,
66  ePhysicsJointSpeed_Angular,
67  ePhysicsJointSpeed_LastEnum
68 };
69 
70 //-----------------------------------
71 
73  kSerializableClassInit(cJointLimitEffect) public : tString msSound;
74  float mfMinSpeed;
75  float mfMaxSpeed;
76 };
77 
78 //-----------------------------------
79 
81 public:
82  virtual ~iPhysicsJointCallback() {}
83 
84  virtual void OnMinLimit(iPhysicsJoint *apJoint) = 0;
85  virtual void OnMaxLimit(iPhysicsJoint *apJoint) = 0;
86 
87  // Ugly trick to support joint script callback.
88  virtual bool IsScript() { return false; }
89 };
90 
91 //-----------------------------------
92 
93 kSaveData_BaseClass(iPhysicsJoint) {
94  kSaveData_ClassInit(iPhysicsJoint) public : tString msName;
95 
96  int mlParentBodyId;
97  int mlChildBodyId;
98 
99  cMatrixf m_mtxParentBodySetup;
100  cMatrixf m_mtxChildBodySetup;
101 
102  cVector3f mvPinDir;
103  cVector3f mvStartPivotPoint;
104 
106 
107  cJointLimitEffect mMaxLimit;
108  cJointLimitEffect mMinLimit;
109 
110  tString msMoveSound;
111 
112  float mfMinMoveSpeed;
113  float mfMinMoveFreq;
114  float mfMinMoveFreqSpeed;
115  float mfMinMoveVolume;
116  float mfMaxMoveFreq;
117  float mfMaxMoveFreqSpeed;
118  float mfMaxMoveVolume;
119  float mfMiddleMoveSpeed;
120  float mfMiddleMoveVolume;
121  int mMoveSpeedType;
122 
123  bool mbBreakable;
124  float mfBreakForce;
125  tString msBreakSound;
126  bool mbBroken;
127 
128  tString msCallbackMaxFunc;
129  tString msCallbackMinFunc;
130  bool mbAutoDeleteCallback;
131 };
132 
133 //-----------------------------------
134 
135 class iPhysicsJoint : public iSaveObject {
136  typedef iSaveObject super;
137 
138 public:
139  iPhysicsJoint(const tString &asName, iPhysicsBody *apParentBody, iPhysicsBody *apChildBody,
140  iPhysicsWorld *apWorld, const cVector3f &avPivotPoint);
141  virtual ~iPhysicsJoint();
142 
143  const tString &GetName() { return msName; }
144 
145  iPhysicsBody *GetParentBody() { return mpParentBody; }
146  iPhysicsBody *GetChildBody() { return mpChildBody; }
147 
148  void RemoveBody(iPhysicsBody *apBody);
149 
150  cVector3f GetPivotPoint() { return mvPivotPoint; }
151  cVector3f GetPinDir() { return mvPinDir; }
152 
153  virtual ePhysicsJointType GetType() = 0;
154 
155  virtual void SetCollideBodies(bool abX) = 0;
156  virtual bool GetCollideBodies() = 0;
157 
158  virtual void SetStiffness(float afX) = 0;
159  virtual float GetStiffness() = 0;
160 
161  virtual cVector3f GetVelocity() = 0;
162  virtual cVector3f GetAngularVelocity() = 0;
163  virtual cVector3f GetForce() = 0;
164 
165  virtual float GetDistance() = 0;
166  virtual float GetAngle() = 0;
167 
168  cJointLimitEffect *GetMaxLimit() { return &mMaxLimit; }
169  cJointLimitEffect *GetMinLimit() { return &mMinLimit; }
170 
171  void SetMoveSound(tString &asName) { msMoveSound = asName; }
172 
173  void SetMoveSpeedType(ePhysicsJointSpeed aType) { mMoveSpeedType = aType; }
174  void SetMinMoveSpeed(float afX) { mfMinMoveSpeed = afX; }
175  void SetMinMoveFreq(float afX) { mfMinMoveFreq = afX; }
176  void SetMinMoveFreqSpeed(float afX) { mfMinMoveFreqSpeed = afX; }
177  void SetMinMoveVolume(float afX) { mfMinMoveVolume = afX; }
178  void SetMaxMoveFreq(float afX) { mfMaxMoveFreq = afX; }
179  void SetMaxMoveVolume(float afX) { mfMaxMoveVolume = afX; }
180  void SetMaxMoveFreqSpeed(float afX) { mfMaxMoveFreqSpeed = afX; }
181  void SetMiddleMoveSpeed(float afX) { mfMiddleMoveSpeed = afX; }
182  void SetMiddleMoveVolume(float afX) { mfMiddleMoveVolume = afX; }
183 
184  void SetCallback(iPhysicsJointCallback *apCallback, bool abAutoDelete) {
185  mpCallback = apCallback;
186  mbAutoDeleteCallback = abAutoDelete;
187  }
188 
189  iPhysicsJointCallback *GetCallback() { return mpCallback; }
190 
191  bool CheckBreakage();
192 
193  void SetBreakable(bool abX) { mbBreakable = abX; }
194  bool IsBreakable() { return mbBreakable; }
195  void SetBreakForce(float afForce) { mfBreakForce = afForce; }
196  float GetBreakForce() { return mfBreakForce; }
197  void SetBreakSound(const tString &asSound) { msBreakSound = asSound; }
198 
199  void SetLimitAutoSleep(bool abX) { mbLimitAutoSleep = abX; }
200  void SetLimitAutoSleepDist(float afX) { mfLimitAutoSleepDist = afX; }
201  void SetLimitAutoSleepNumSteps(int alX) { mlLimitAutoSleepNumSteps = alX; }
202 
203  bool GetLimitAutoSleep() { return mbLimitAutoSleep; }
204  float GetLimitAutoSleepDist() { return mfLimitAutoSleepDist; }
205  int GetLimitAutoSleepNumSteps() { return mlLimitAutoSleepNumSteps; }
206 
207  void SetStickyMinDistance(float afX) { mfStickyMinDistance = afX; }
208  void SetStickyMaxDistance(float afX) { mfStickyMaxDistance = afX; }
209  float GetStickyMinDistance() { return mfStickyMinDistance; }
210  float GetStickyMaxDistance() { return mfStickyMaxDistance; }
211 
212  void Break();
213 
214  bool IsBroken() { return mbBroken; }
215 
216  void SetUserData(void *apUserData) { mpUserData = apUserData; }
217 
218  void AddController(iPhysicsController *apController);
219  iPhysicsController *GetController(const tString &asName);
220  bool ChangeController(const tString &asName);
221  cPhysicsControllerIterator GetControllerIterator();
222 
223  void SetAllControllersPaused(bool abX);
224 
225  void OnPhysicsUpdate();
226 
227  void SetSound(cSoundEntity *apSound) { mpSound = apSound; }
228  cSoundEntity *GetSound() { return mpSound; }
229 
230  // SaveObject implementation
231  virtual iSaveData *CreateSaveData();
232  virtual void SaveToSaveData(iSaveData *apSaveData);
233  virtual void LoadFromSaveData(iSaveData *apSaveData);
234  virtual void SaveDataSetup(cSaveObjectHandler *apSaveObjectHandler, cGame *apGame);
235 
236 protected:
237  tString msName;
238 
239  iPhysicsBody *mpParentBody;
240  iPhysicsBody *mpChildBody;
241  iPhysicsWorld *mpWorld;
242 
243  cMatrixf m_mtxParentBodySetup;
244  cMatrixf m_mtxChildBodySetup;
245 
246  cVector3f mvPinDir;
247  cVector3f mvPivotPoint;
248  cVector3f mvStartPivotPoint;
249 
250  cVector3f mvLocalPivot;
251 
252  float mfStickyMinDistance;
253  float mfStickyMaxDistance;
254 
255  tPhysicsControllerMap m_mapControllers;
256 
257  cJointLimitEffect mMaxLimit;
258  cJointLimitEffect mMinLimit;
259 
260  int mlSpeedCount;
261 
262  cMatrixf m_mtxPrevChild;
263  cMatrixf m_mtxPrevParent;
264 
265  tString msMoveSound;
266 
267  float mfMinMoveSpeed;
268  float mfMinMoveFreq;
269  float mfMinMoveFreqSpeed;
270  float mfMinMoveVolume;
271  float mfMaxMoveFreq;
272  float mfMaxMoveFreqSpeed;
273  float mfMaxMoveVolume;
274  float mfMiddleMoveSpeed;
275  float mfMiddleMoveVolume;
276  ePhysicsJointSpeed mMoveSpeedType;
277 
278  bool mbBreakable;
279  float mfBreakForce;
280  tString msBreakSound;
281  bool mbBroken;
282 
283  bool mbLimitAutoSleep;
284  float mfLimitAutoSleepDist;
285  int mlLimitAutoSleepNumSteps;
286 
287  cSoundEntity *mpSound;
288  bool mbHasCollided;
289 
290  iPhysicsJointCallback *mpCallback;
291  bool mbAutoDeleteCallback;
292 
293  int mlLimitStepCount;
294 
295  void *mpUserData;
296 
297  static void CheckLimitAutoSleep(iPhysicsJoint *apJoint, const float afMin, const float afMax,
298  const float afDist);
299 
300  void OnMaxLimit();
301  void OnMinLimit();
302  void OnNoLimit();
303 
304  void CalcSoundFreq(float afSpeed, float *apFreq, float *apVol);
305 
306  void LimitEffect(cJointLimitEffect *pEffect);
307 };
308 
309 } // namespace hpl
310 
311 #endif // HPL_PHYSICS_JOINT_H
Definition: AI.h:36
Definition: Game.h:91
Definition: str.h:59
Definition: PhysicsJoint.h:80
Definition: PhysicsWorld.h:115
Definition: PhysicsJoint.h:72
typename TreeT::BasicIterator iterator
Definition: stablemap.h:48
Definition: SerializeClass.h:230
Definition: SaveGame.h:130
Definition: PhysicsBody.h:117
Definition: SaveGame.h:111
Definition: Container.h:219
Definition: PhysicsController.h:122
Definition: SoundEntity.h:86
Definition: PhysicsJoint.h:135
Definition: SaveGame.h:183