ScummVM API documentation
PhysicsWorld.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_WORLD_H
29 #define HPL_PHYSICS_WORLD_H
30 
31 #include "hpl1/engine/graphics/GraphicsTypes.h"
32 #include "hpl1/engine/math/MathTypes.h"
33 #include "hpl1/engine/system/SystemTypes.h"
34 #include "common/stablemap.h"
35 
36 #include "hpl1/engine/physics/CollideData.h"
37 
38 #include "hpl1/engine/game/SaveGame.h"
39 
40 namespace hpl {
41 
42 class iCollideShape;
43 class iVertexBuffer;
44 class iPhysicsBody;
45 class iLowLevelGraphics;
46 class iPhysicsMaterial;
47 class iCharacterBody;
48 class iPhysicsJoint;
49 class iPhysicsJointBall;
50 class iPhysicsJointHinge;
51 class iPhysicsJointScrew;
52 class iPhysicsJointSlider;
53 class iPhysicsController;
54 class cWorld3D;
55 class cBoundingVolume;
56 
57 typedef Common::List<iCollideShape *> tCollideShapeList;
58 typedef tCollideShapeList::iterator tCollideShapeListIt;
59 
60 typedef Common::Array<iCollideShape *> tCollideShapeVec;
61 typedef tCollideShapeVec::iterator tCollideShapeVecIt;
62 
63 typedef Common::List<iPhysicsBody *> tPhysicsBodyList;
64 typedef tPhysicsBodyList::iterator tPhysicsBodyListIt;
65 
66 typedef Common::List<iPhysicsJoint *> tPhysicsJointList;
67 typedef tPhysicsJointList::iterator tPhysicsJointListIt;
68 
69 typedef Common::List<iPhysicsController *> tPhysicsControllerList;
70 typedef tPhysicsControllerList::iterator tPhysicsControllerListIt;
71 
72 typedef Common::List<iCharacterBody *> tCharacterBodyList;
73 typedef tCharacterBodyList::iterator tCharacterBodyListIt;
74 
75 typedef Common::StableMap<tString, iPhysicsMaterial *> tPhysicsMaterialMap;
76 typedef tPhysicsMaterialMap::iterator tPhysicsMaterialMapIt;
77 
78 typedef cSTLMapIterator<iPhysicsMaterial *, tPhysicsMaterialMap, tPhysicsMaterialMapIt> cPhysicsMaterialIterator;
79 
80 typedef cSTLIterator<iPhysicsBody *, tPhysicsBodyList, tPhysicsBodyListIt> cPhysicsBodyIterator;
81 typedef cSTLIterator<iPhysicsJoint *, tPhysicsJointList, tPhysicsJointListIt> cPhysicsJointIterator;
82 
83 enum ePhysicsAccuracy {
84  ePhysicsAccuracy_Low,
85  ePhysicsAccuracy_Medium,
86  ePhysicsAccuracy_High,
87  ePhysicsAccuracy_LastEnum
88 };
89 
90 //----------------------------------------------------
91 
93  constexpr cPhysicsRayParams() {}
94  float mfT = 0;
95  float mfDist = 0;
96  cVector3f mvNormal = {0, 0, 0};
97  cVector3f mvPoint = {0, 0, 0};
98 };
99 
101 public:
102  virtual ~iPhysicsRayCallback() = default;
103  virtual bool BeforeIntersect(iPhysicsBody *pBody) { return true; }
104  virtual bool OnIntersect(iPhysicsBody *pBody, cPhysicsRayParams *apParams) = 0;
105 };
106 
108 public:
109  virtual ~iPhysicsWorldCollisionCallback() = default;
110  virtual void OnCollision(iPhysicsBody *apBody, cCollideData *apCollideData) = 0;
111 };
112 
113 //----------------------------------------------------
114 
116 public:
117  iPhysicsWorld();
118  virtual ~iPhysicsWorld();
119 
120  //########################################################################################
122  //########################################################################################
124 
125  void Update(float afTimeStep);
126  virtual void Simulate(float afTimeStep) = 0;
127 
128  virtual void SetMaxTimeStep(float afTimeStep) = 0;
129  virtual float GetMaxTimeStep() = 0;
130 
131  virtual void SetWorldSize(const cVector3f &avMin, const cVector3f &avMax) = 0;
132  virtual cVector3f GetWorldSizeMin() = 0;
133  virtual cVector3f GetWorldSizeMax() = 0;
134 
135  virtual void SetGravity(const cVector3f &avGravity) = 0;
136  virtual cVector3f GetGravity() = 0;
137 
138  virtual void SetAccuracyLevel(ePhysicsAccuracy aAccuracy) = 0;
139  virtual ePhysicsAccuracy GetAccuracyLevel() = 0;
141 
142  //########################################################################################
144  //########################################################################################
146 
147  virtual iCollideShape *CreateNullShape() = 0;
148  virtual iCollideShape *CreateBoxShape(const cVector3f &avSize, cMatrixf *apOffsetMtx) = 0;
149  virtual iCollideShape *CreateSphereShape(const cVector3f &avRadii, cMatrixf *apOffsetMtx) = 0;
150  virtual iCollideShape *CreateCylinderShape(float afRadius, float afHeight, cMatrixf *apOffsetMtx) = 0;
151  virtual iCollideShape *CreateCapsuleShape(float afRadius, float afHeight, cMatrixf *apOffsetMtx) = 0;
152  virtual iCollideShape *CreateMeshShape(iVertexBuffer *apVtxBuffer) = 0;
153  virtual iCollideShape *CreateCompundShape(tCollideShapeVec &avShapes) = 0;
154  void DestroyShape(iCollideShape *apShape);
155 
157 
158  //########################################################################################
160  //########################################################################################
162 
163  virtual iPhysicsJointBall *CreateJointBall(const tString &asName,
164  const cVector3f &avPivotPoint,
165  iPhysicsBody *apParentBody, iPhysicsBody *apChildBody) = 0;
166  virtual iPhysicsJointHinge *CreateJointHinge(const tString &asName,
167  const cVector3f &avPivotPoint, const cVector3f &avPinDir,
168  iPhysicsBody *apParentBody, iPhysicsBody *apChildBody) = 0;
169  virtual iPhysicsJointSlider *CreateJointSlider(const tString &asName,
170  const cVector3f &avPivotPoint, const cVector3f &avPinDir,
171  iPhysicsBody *apParentBody, iPhysicsBody *apChildBody) = 0;
172  virtual iPhysicsJointScrew *CreateJointScrew(const tString &asName,
173  const cVector3f &avPivotPoint, const cVector3f &avPinDir,
174  iPhysicsBody *apParentBody, iPhysicsBody *apChildBody) = 0;
175  void DestroyJoint(iPhysicsJoint *apJoint);
176  iPhysicsJoint *GetJoint(const tString &asName);
177  cPhysicsJointIterator GetJointIterator();
178 
180 
181  //########################################################################################
183  //########################################################################################
185 
186  virtual iPhysicsMaterial *CreateMaterial(const tString &asName) = 0;
187  iPhysicsMaterial *GetMaterialFromName(const tString &asName);
188  cPhysicsMaterialIterator GetMaterialIterator();
189 
191 
192  //########################################################################################
194  //########################################################################################
196 
197  virtual iPhysicsBody *CreateBody(const tString &asName, iCollideShape *apShape) = 0;
198  void DestroyBody(iPhysicsBody *apBody);
199  iPhysicsBody *GetBody(const tString &asName);
200  cPhysicsBodyIterator GetBodyIterator();
201 
202  virtual iCharacterBody *CreateCharacterBody(const tString &asName, const cVector3f &avSize) = 0;
203  void DestroyCharacterBody(iCharacterBody *apBody);
204  iPhysicsBody *GetCharacterBody(const tString &asName);
205 
206  void GetBodiesInBV(cBoundingVolume *apBV, tPhysicsBodyList *apBodyList);
207  void EnableBodiesInBV(cBoundingVolume *apBV, bool abEnabled);
208 
210 
211  //########################################################################################
213  //########################################################################################
215 
216  void SetLogDebug(bool abX) { mbLogDebug = abX; }
217  bool GetLogDebug() { return mbLogDebug; }
218 
219  void AddSaveData(cSaveDataHandler *apHandler);
220 
221  virtual iPhysicsController *CreateController(const tString &asName) = 0;
222  void DestroyController(iPhysicsController *apController);
223 
224  tCollidePointVec *GetContactPoints() { return &mvContactPoints; }
225  void SetSaveContactPoints(bool abX) { mbSaveContactPoints = abX; }
226  bool GetSaveContactPoints() { return mbSaveContactPoints; }
227  void RenderContactPoints(iLowLevelGraphics *apLowLevel, const cColor &aPointColor,
228  const cColor &aLineColor);
229 
230  virtual void CastRay(iPhysicsRayCallback *apCallback,
231  const cVector3f &avOrigin, const cVector3f &avEnd,
232  bool abCalcDist, bool abCalcNormal, bool abCalcPoint,
233  bool abUsePrefilter = false) = 0;
234 
235  virtual void RenderDebugGeometry(iLowLevelGraphics *apLowLevel, const cColor &aColor) = 0;
236 
237  virtual bool CheckShapeCollision(iCollideShape *apShapeA, const cMatrixf &a_mtxA,
238  iCollideShape *apShapeB, const cMatrixf &a_mtxB,
239  cCollideData &aCollideData, int alMaxPoints,
240  bool correctNormalDirection = false) = 0;
241 
242  bool CheckShapeWorldCollision(cVector3f *apNewPos,
243  iCollideShape *apShape, const cMatrixf &a_mtxTransform,
244  iPhysicsBody *apSkipBody = NULL, bool abSkipStatic = false,
245  bool abIsCharacter = false,
246  iPhysicsWorldCollisionCallback *apCallback = NULL,
247  bool abCollideCharacter = true,
248  bool abDebug = false);
249 
250  void DestroyAll();
251 
252  cWorld3D *GetWorld3D() { return mpWorld3D; }
253  void SetWorld3D(cWorld3D *apWorld3D) { mpWorld3D = apWorld3D; }
255 
256 protected:
257  tCollideShapeList mlstShapes;
258  tPhysicsBodyList mlstBodies;
259  tCharacterBodyList mlstCharBodies;
260  tPhysicsMaterialMap m_mapMaterials;
261  tPhysicsJointList mlstJoints;
262  tPhysicsControllerList mlstControllers;
263  cWorld3D *mpWorld3D;
264 
265  bool mbLogDebug;
266 
267  tCollidePointVec mvContactPoints;
268  bool mbSaveContactPoints;
269 };
270 
271 } // namespace hpl
272 
273 #endif // HPL_PHYSICS_WORLD_H
Definition: AI.h:36
Definition: str.h:59
Definition: PhysicsWorld.h:92
Definition: VertexBuffer.h:90
Definition: PhysicsWorld.h:100
Definition: PhysicsWorld.h:115
T * iterator
Definition: array.h:54
typename TreeT::BasicIterator iterator
Definition: stablemap.h:48
Definition: BoundingVolume.h:71
Definition: CollideShape.h:50
Definition: SaveGame.h:214
Definition: PhysicsJointScrew.h:48
Definition: PhysicsBody.h:117
Definition: PhysicsJointHinge.h:47
Definition: World3D.h:179
Definition: CollideData.h:46
ListInternal::Iterator< t_T > iterator
Definition: list.h:52
Definition: PhysicsMaterial.h:77
Definition: PhysicsJointBall.h:48
Definition: Color.h:37
Definition: CharacterBody.h:153
Definition: PhysicsController.h:122
Definition: PhysicsJointSlider.h:48
Definition: PhysicsJoint.h:135
Definition: PhysicsWorld.h:107
Definition: LowLevelGraphics.h:200