ScummVM API documentation
PhysicsBody.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_BODY_H
29 #define HPL_PHYSICS_BODY_H
30 
31 #include "hpl1/engine/graphics/GraphicsTypes.h"
32 #include "hpl1/engine/scene/Entity3D.h"
33 
34 namespace hpl {
35 
36 class iPhysicsWorld;
37 class iCollideShape;
38 class iPhysicsMaterial;
39 class iLowLevelGraphics;
40 class cNode3D;
41 class cSoundEntity;
42 class iPhysicsJoint;
43 class cPhysicsContactData;
44 class iCharacterBody;
45 
46 //------------------------------------------
47 
48 class iPhysicsBody;
50 public:
51  virtual ~iPhysicsBodyCallback() {}
52  virtual bool OnBeginCollision(iPhysicsBody *apBody, iPhysicsBody *apCollideBody) = 0;
53  virtual void OnCollide(iPhysicsBody *apBody, iPhysicsBody *apCollideBody,
54  cPhysicsContactData *apContactData) = 0;
55 };
56 
59 
60 //------------------------------------------
61 
63  kSerializableClassInit(cSaveData_iCollideShape) public : int mType;
64  cMatrixf m_mtxOffset;
65  cVector3f mvSize;
66 };
67 
68 //------------------------------------------
69 
70 kSaveData_ChildClass(iEntity3D, iPhysicsBody) {
71  kSaveData_ClassInit(iPhysicsBody) public : cContainerList<cSaveData_iCollideShape> mlstShapes;
72 
73  tString msMaterial;
74 
75  bool mbBlocksSound;
76  bool mbIsCharacter;
77  bool mbCollideCharacter;
78 
79  cVector3f mvLinearVelocity;
80  cVector3f mvAngularVelocity;
81  float mfLinearDamping;
82  float mfAngularDamping;
83  float mfMaxLinearSpeed;
84  float mfMaxAngularSpeed;
85 
86  float mfMass;
87 
88  bool mbEnabled;
89  bool mbAutoDisable;
90  bool mbContinuousCollision;
91 
92  bool mbGravity;
93 
94  bool mbCollide;
95 
96  virtual iSaveObject *CreateSaveObject(cSaveObjectHandler * apSaveObjectHandler, cGame * apGame);
97  virtual int GetSaveCreatePrio();
98 };
99 
100 //------------------------------------------
101 
103  cPhysicsBody_Buoyancy() : mbActive(false), mfDensity(1),
104  mfLinearViscosity(1), mfAngularViscosity(1) {}
105 
106  bool mbActive;
107 
108  float mfDensity;
109  float mfLinearViscosity;
110  float mfAngularViscosity;
111 
112  cPlanef mSurface;
113 };
114 
115 //------------------------------------------
116 
117 class iPhysicsBody : public iEntity3D {
118  typedef iEntity3D super;
119 
120 public:
121  iPhysicsBody(const tString &asName, iPhysicsWorld *apWorld, iCollideShape *apShape);
122  virtual ~iPhysicsBody();
123 
124  void Destroy();
125 
126  virtual void SetMaterial(iPhysicsMaterial *apMaterial) = 0;
127  iPhysicsMaterial *GetMaterial();
128 
129  cNode3D *GetNode();
130  cNode3D *CreateNode();
131 
132  iCollideShape *GetShape();
133 
134  void AddJoint(iPhysicsJoint *apJoint);
135  iPhysicsJoint *GetJoint(int alIndex);
136  int GetJointNum();
137  void RemoveJoint(iPhysicsJoint *apJoint);
138 
139  virtual void SetLinearVelocity(const cVector3f &avVel) = 0;
140  virtual cVector3f GetLinearVelocity() const = 0;
141  virtual void SetAngularVelocity(const cVector3f &avVel) = 0;
142  virtual cVector3f GetAngularVelocity() const = 0;
143  virtual void SetLinearDamping(float afDamping) = 0;
144  virtual float GetLinearDamping() const = 0;
145  virtual void SetAngularDamping(float afDamping) = 0;
146  virtual float GetAngularDamping() const = 0;
147  virtual void SetMaxLinearSpeed(float afSpeed) = 0;
148  virtual float GetMaxLinearSpeed() const = 0;
149  virtual void SetMaxAngularSpeed(float afDamping) = 0;
150  virtual float GetMaxAngularSpeed() const = 0;
151  virtual cMatrixf GetInertiaMatrix() = 0;
152 
153  cVector3f GetVelocityAtPosition(cVector3f avPos);
154 
155  virtual void SetMass(float afMass) = 0;
156  virtual float GetMass() const = 0;
157  virtual void SetMassCentre(const cVector3f &avCentre) = 0;
158  virtual cVector3f GetMassCentre() const = 0;
159 
160  virtual void AddForce(const cVector3f &avForce) = 0;
161  virtual void AddForceAtPosition(const cVector3f &avForce, const cVector3f &avPos) = 0;
162  virtual void AddTorque(const cVector3f &avTorque) = 0;
163  virtual void AddImpulse(const cVector3f &avImpulse) = 0;
164  virtual void AddImpulseAtPosition(const cVector3f &avImpulse, const cVector3f &avPos) = 0;
165 
166  virtual void SetEnabled(bool abEnabled) = 0;
167  virtual bool GetEnabled() const = 0;
168  virtual void SetAutoDisable(bool abEnabled) = 0;
169  virtual bool GetAutoDisable() const = 0;
170 #if 0
171  virtual void SetAutoDisableLinearThreshold(float afThresold) = 0;
172  virtual float GetAutoDisableLinearThreshold() const = 0;
173  virtual void SetAutoDisableAngularThreshold(float afThresold) = 0;
174  virtual float GetAutoDisableAngularThreshold() const = 0;
175  virtual void SetAutoDisableNumSteps(int alNum) = 0;
176  virtual int GetAutoDisableNumSteps() const = 0;
177 #endif
178  virtual void SetContinuousCollision(bool abOn) = 0;
179  virtual bool GetContinuousCollision() = 0;
180 
181  virtual void SetGravity(bool abEnabled) = 0;
182  virtual bool GetGravity() const = 0;
183 
184  virtual void RenderDebugGeometry(iLowLevelGraphics *apLowLevel, const cColor &aColor) = 0;
185 
186  void UpdateBeforeSimulate(float afTimeStep);
187  void UpdateAfterSimulate(float afTimeStep);
188 
189  cBoundingVolume *GetBV() { return &mBoundingVolume; }
190 
191  void SetBlocksSound(bool abX) { mbBlocksSound = abX; }
192  bool GetBlocksSound() { return mbBlocksSound; }
193 
194  void SetBlocksLight(bool abX) { mbBlocksLight = abX; }
195  bool GetBlocksLight() { return mbBlocksLight; }
196 
197  void SetScrapeSoundEntity(cSoundEntity *apEntity) { mpScrapeSoundEntity = apEntity; }
198  cSoundEntity *GetScrapeSoundEntity() { return mpScrapeSoundEntity; }
199  void SetScrapeBody(iPhysicsBody *apBody) { mpScrapeBody = apBody; }
200  iPhysicsBody *GetScrapeBody() { return mpScrapeBody; }
201  const cMatrixf &GetPreveScrapeMatrix() { return m_mtxPrevScrapeMatrix; }
202  void SetPreveScrapeMatrix(const cMatrixf &a_mtxMtx) { m_mtxPrevScrapeMatrix = a_mtxMtx; }
203 
204  void SetRollSoundEntity(cSoundEntity *apEntity) { mpRollSoundEntity = apEntity; }
205  cSoundEntity *GetRollSoundEntity() { return mpRollSoundEntity; }
206 
207  void SetHasImpact(bool abX) { mbHasImpact = abX; }
208  bool HasImpact() { return mbHasImpact; }
209  void SetHasSlide(bool abX) { mbHasSlide = abX; }
210  bool HasSlide() { return mbHasSlide; }
211 
212  bool HasCollision() { return mbHasCollision; }
213 
214  void SetUserData(void *apUserData) { mpUserData = apUserData; }
215  void *GetUserData() { return mpUserData; }
216 
217  void AddBodyCallback(iPhysicsBodyCallback *apCallback);
218  void RemoveBodyCallback(iPhysicsBodyCallback *apCallback);
219 
220  bool OnBeginCollision(iPhysicsBody *apBody);
221  void OnCollide(iPhysicsBody *apBody, cPhysicsContactData *apContactData);
222 
223  void SetCollide(bool abX) { mbCollide = abX; }
224  bool GetCollide() { return mbCollide; }
225 
226  void SetIsCharacter(bool abX) { mbIsCharacter = abX; }
227  bool IsCharacter() { return mbIsCharacter; }
228 
229  void SetCollideCharacter(bool abX) { mbCollideCharacter = abX; }
230  bool GetCollideCharacter() { return mbCollideCharacter; }
231 
232  void SetCharacterBody(iCharacterBody *apCharBody) { mpCharacterBody = apCharBody; }
233  iCharacterBody *GetCharacterBody() { return mpCharacterBody; }
234 
235  void SetIsRagDoll(bool abX) { mbIsRagDoll = abX; }
236  bool IsRagDoll() { return mbIsRagDoll; }
237 
238  void SetCollideRagDoll(bool abX) { mbCollideRagDoll = abX; }
239  bool GetCollideRagDoll() { return mbCollideRagDoll; }
240 
241  void SetVolatile(bool abX) { mbVolatile = abX; }
242  bool IsVolatile() { return mbVolatile; }
243 
244  void SetPushedByCharacterGravity(bool abX) { mbPushedByCharacterGravity = abX; }
245  bool GetPushedByCharacterGravity() { return mbPushedByCharacterGravity; }
246 
247  void SetBuoyancyId(int alX) { mlBuoyancyId = alX; }
248  void SetBuoyancyActive(bool abX) { mBuoyancy.mbActive = abX; }
249  void SetBuoyancyDensity(float afX) { mBuoyancy.mfDensity = afX; }
250  void SetBuoyancyLinearViscosity(float afX) { mBuoyancy.mfLinearViscosity = afX; }
251  void SetBuoyancyAngularViscosity(float afX) { mBuoyancy.mfAngularViscosity = afX; }
252  void SetBuoyancySurface(const cPlanef &aP) { mBuoyancy.mSurface = aP; }
253 
254  int GetBuoyancyId() { return mlBuoyancyId; }
255  bool GetBuoyancyActive() { return mBuoyancy.mbActive; }
256  float GetBuoyancyDensity() { return mBuoyancy.mfDensity; }
257  float GetBuoyancyLinearViscosity() { return mBuoyancy.mfLinearViscosity; }
258  float GetBuoyancyAngularViscosity() { return mBuoyancy.mfAngularViscosity; }
259  cPlanef SetBuoyancySurface() { return mBuoyancy.mSurface; }
260 
261  void SetCanAttachCharacter(bool abX) { mbCanAttachCharacter = abX; }
262  bool GetCanAttachCharacter() { return mbCanAttachCharacter; }
263  void AddAttachedCharacter(iCharacterBody *apChar);
264  void RemoveAttachedCharacter(iCharacterBody *apChar);
265 
266  iPhysicsWorld *GetWorld() { return mpWorld; }
267 
268  void DisableAfterSimulation() { mbDisableAfterSimulation = true; }
269 
270  // Entity implementation
271  tString GetEntityType() { return "Body"; }
272 
273  // SaveObject implementation
274  virtual iSaveData *CreateSaveData();
275  virtual void SaveToSaveData(iSaveData *apSaveData);
276  virtual void LoadFromSaveData(iSaveData *apSaveData);
277  virtual void SaveDataSetup(cSaveObjectHandler *apSaveObjectHandler, cGame *apGame);
278 
279  virtual void DeleteLowLevel() = 0;
280 
281 protected:
282  void CreateSaveCollideShapes(cContainerList<cSaveData_iCollideShape> *apShapeList);
283 
284  iPhysicsWorld *mpWorld;
285  iCollideShape *mpShape;
286  iPhysicsMaterial *mpMaterial;
287  cNode3D *mpNode;
288 
289  iCharacterBody *mpCharacterBody;
290 
292 
293  Common::List<iCharacterBody *> mlstAttachedCharacters;
294 
295  iPhysicsBody *mpScrapeBody;
296  cSoundEntity *mpScrapeSoundEntity;
297  cSoundEntity *mpRollSoundEntity;
298  cMatrixf m_mtxPrevScrapeMatrix;
299  bool mbHasImpact;
300  bool mbHasSlide;
301  int mlSlideCount;
302  int mlImpactCount;
303 
304  bool mbPushedByCharacterGravity;
305 
306  bool mbBlocksSound;
307  bool mbBlocksLight;
308  bool mbIsCharacter;
309  bool mbCollideCharacter;
310  bool mbIsRagDoll;
311  bool mbCollideRagDoll;
312  bool mbVolatile;
313 
314  bool mbCanAttachCharacter;
315 
316  cPhysicsBody_Buoyancy mBuoyancy;
317  int mlBuoyancyId;
318 
319  bool mbDisableAfterSimulation;
320 
321  bool mbHasCollision;
322 
323  tPhysicsBodyCallbackList mlstBodyCallbacks;
324 
325  void *mpUserData;
326 
327  bool mbCollide;
328 };
329 
330 } // namespace hpl
331 
332 #endif // HPL_PHYSICS_BODY_H
Definition: AI.h:36
Definition: Game.h:91
Definition: str.h:59
Definition: array.h:52
Definition: PhysicsWorld.h:115
Definition: BoundingVolume.h:71
Definition: SerializeClass.h:230
Definition: PhysicsBody.h:102
Definition: SaveGame.h:130
Definition: PhysicsBody.h:49
Definition: CollideShape.h:50
Definition: Node3D.h:52
Definition: PhysicsBody.h:117
Definition: SaveGame.h:111
Definition: Container.h:219
Definition: PhysicsMaterial.h:77
Definition: PhysicsMaterial.h:58
Definition: Color.h:37
Definition: CharacterBody.h:153
Definition: Entity3D.h:81
Definition: list_intern.h:51
Definition: SoundEntity.h:86
Definition: PhysicsJoint.h:135
Definition: SaveGame.h:183
Definition: LowLevelGraphics.h:200
Definition: PhysicsBody.h:62