File: PhysicsWorld.h

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (273 lines) | stat: -rw-r--r-- 9,915 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * Copyright (C) 2006-2010 - Frictional Games
 *
 * This file is part of HPL1 Engine.
 */

#ifndef HPL_PHYSICS_WORLD_H
#define HPL_PHYSICS_WORLD_H

#include "hpl1/engine/graphics/GraphicsTypes.h"
#include "hpl1/engine/math/MathTypes.h"
#include "hpl1/engine/system/SystemTypes.h"
#include "common/stablemap.h"

#include "hpl1/engine/physics/CollideData.h"

#include "hpl1/engine/game/SaveGame.h"

namespace hpl {

class iCollideShape;
class iVertexBuffer;
class iPhysicsBody;
class iLowLevelGraphics;
class iPhysicsMaterial;
class iCharacterBody;
class iPhysicsJoint;
class iPhysicsJointBall;
class iPhysicsJointHinge;
class iPhysicsJointScrew;
class iPhysicsJointSlider;
class iPhysicsController;
class cWorld3D;
class cBoundingVolume;

typedef Common::List<iCollideShape *> tCollideShapeList;
typedef tCollideShapeList::iterator tCollideShapeListIt;

typedef Common::Array<iCollideShape *> tCollideShapeVec;
typedef tCollideShapeVec::iterator tCollideShapeVecIt;

typedef Common::List<iPhysicsBody *> tPhysicsBodyList;
typedef tPhysicsBodyList::iterator tPhysicsBodyListIt;

typedef Common::List<iPhysicsJoint *> tPhysicsJointList;
typedef tPhysicsJointList::iterator tPhysicsJointListIt;

typedef Common::List<iPhysicsController *> tPhysicsControllerList;
typedef tPhysicsControllerList::iterator tPhysicsControllerListIt;

typedef Common::List<iCharacterBody *> tCharacterBodyList;
typedef tCharacterBodyList::iterator tCharacterBodyListIt;

typedef Common::StableMap<tString, iPhysicsMaterial *> tPhysicsMaterialMap;
typedef tPhysicsMaterialMap::iterator tPhysicsMaterialMapIt;

typedef cSTLMapIterator<iPhysicsMaterial *, tPhysicsMaterialMap, tPhysicsMaterialMapIt> cPhysicsMaterialIterator;

typedef cSTLIterator<iPhysicsBody *, tPhysicsBodyList, tPhysicsBodyListIt> cPhysicsBodyIterator;
typedef cSTLIterator<iPhysicsJoint *, tPhysicsJointList, tPhysicsJointListIt> cPhysicsJointIterator;

enum ePhysicsAccuracy {
	ePhysicsAccuracy_Low,
	ePhysicsAccuracy_Medium,
	ePhysicsAccuracy_High,
	ePhysicsAccuracy_LastEnum
};

//----------------------------------------------------

struct cPhysicsRayParams {
	constexpr cPhysicsRayParams() {}
	float mfT = 0;
	float mfDist = 0;
	cVector3f mvNormal = {0, 0, 0};
	cVector3f mvPoint = {0, 0, 0};
};

class iPhysicsRayCallback {
public:
	virtual ~iPhysicsRayCallback() = default;
	virtual bool BeforeIntersect(iPhysicsBody *pBody) { return true; }
	virtual bool OnIntersect(iPhysicsBody *pBody, cPhysicsRayParams *apParams) = 0;
};

class iPhysicsWorldCollisionCallback {
public:
	virtual ~iPhysicsWorldCollisionCallback() = default;
	virtual void OnCollision(iPhysicsBody *apBody, cCollideData *apCollideData) = 0;
};

//----------------------------------------------------

class iPhysicsWorld {
public:
	iPhysicsWorld();
	virtual ~iPhysicsWorld();

	//########################################################################################
	//! \name General
	//########################################################################################
	//! @{

	void Update(float afTimeStep);
	virtual void Simulate(float afTimeStep) = 0;

	virtual void SetMaxTimeStep(float afTimeStep) = 0;
	virtual float GetMaxTimeStep() = 0;

	virtual void SetWorldSize(const cVector3f &avMin, const cVector3f &avMax) = 0;
	virtual cVector3f GetWorldSizeMin() = 0;
	virtual cVector3f GetWorldSizeMax() = 0;

	virtual void SetGravity(const cVector3f &avGravity) = 0;
	virtual cVector3f GetGravity() = 0;

	virtual void SetAccuracyLevel(ePhysicsAccuracy aAccuracy) = 0;
	virtual ePhysicsAccuracy GetAccuracyLevel() = 0;
	//! @}

	//########################################################################################
	//! \name Shapes
	//########################################################################################
	//! @{

	virtual iCollideShape *CreateNullShape() = 0;
	virtual iCollideShape *CreateBoxShape(const cVector3f &avSize, cMatrixf *apOffsetMtx) = 0;
	virtual iCollideShape *CreateSphereShape(const cVector3f &avRadii, cMatrixf *apOffsetMtx) = 0;
	virtual iCollideShape *CreateCylinderShape(float afRadius, float afHeight, cMatrixf *apOffsetMtx) = 0;
	virtual iCollideShape *CreateCapsuleShape(float afRadius, float afHeight, cMatrixf *apOffsetMtx) = 0;
	virtual iCollideShape *CreateMeshShape(iVertexBuffer *apVtxBuffer) = 0;
	virtual iCollideShape *CreateCompundShape(tCollideShapeVec &avShapes) = 0;
	void DestroyShape(iCollideShape *apShape);

	//! @}

	//########################################################################################
	//! \name Joints
	//########################################################################################
	//! @{

	virtual iPhysicsJointBall *CreateJointBall(const tString &asName,
											   const cVector3f &avPivotPoint,
											   iPhysicsBody *apParentBody, iPhysicsBody *apChildBody) = 0;
	virtual iPhysicsJointHinge *CreateJointHinge(const tString &asName,
												 const cVector3f &avPivotPoint, const cVector3f &avPinDir,
												 iPhysicsBody *apParentBody, iPhysicsBody *apChildBody) = 0;
	virtual iPhysicsJointSlider *CreateJointSlider(const tString &asName,
												   const cVector3f &avPivotPoint, const cVector3f &avPinDir,
												   iPhysicsBody *apParentBody, iPhysicsBody *apChildBody) = 0;
	virtual iPhysicsJointScrew *CreateJointScrew(const tString &asName,
												 const cVector3f &avPivotPoint, const cVector3f &avPinDir,
												 iPhysicsBody *apParentBody, iPhysicsBody *apChildBody) = 0;
	void DestroyJoint(iPhysicsJoint *apJoint);
	iPhysicsJoint *GetJoint(const tString &asName);
	cPhysicsJointIterator GetJointIterator();

	//! @}

	//########################################################################################
	//! \name Materials
	//########################################################################################
	//! @{

	virtual iPhysicsMaterial *CreateMaterial(const tString &asName) = 0;
	iPhysicsMaterial *GetMaterialFromName(const tString &asName);
	cPhysicsMaterialIterator GetMaterialIterator();

	//! @}

	//########################################################################################
	//! \name Bodies
	//########################################################################################
	//! @{

	virtual iPhysicsBody *CreateBody(const tString &asName, iCollideShape *apShape) = 0;
	void DestroyBody(iPhysicsBody *apBody);
	iPhysicsBody *GetBody(const tString &asName);
	cPhysicsBodyIterator GetBodyIterator();

	virtual iCharacterBody *CreateCharacterBody(const tString &asName, const cVector3f &avSize) = 0;
	void DestroyCharacterBody(iCharacterBody *apBody);
	iPhysicsBody *GetCharacterBody(const tString &asName);

	void GetBodiesInBV(cBoundingVolume *apBV, tPhysicsBodyList *apBodyList);
	void EnableBodiesInBV(cBoundingVolume *apBV, bool abEnabled);

	//! @}

	//########################################################################################
	//! \name Tools
	//########################################################################################
	//! @{

	void SetLogDebug(bool abX) { mbLogDebug = abX; }
	bool GetLogDebug() { return mbLogDebug; }

	void AddSaveData(cSaveDataHandler *apHandler);

	virtual iPhysicsController *CreateController(const tString &asName) = 0;
	void DestroyController(iPhysicsController *apController);

	tCollidePointVec *GetContactPoints() { return &mvContactPoints; }
	void SetSaveContactPoints(bool abX) { mbSaveContactPoints = abX; }
	bool GetSaveContactPoints() { return mbSaveContactPoints; }
	void RenderContactPoints(iLowLevelGraphics *apLowLevel, const cColor &aPointColor,
							 const cColor &aLineColor);

	virtual void CastRay(iPhysicsRayCallback *apCallback,
						 const cVector3f &avOrigin, const cVector3f &avEnd,
						 bool abCalcDist, bool abCalcNormal, bool abCalcPoint,
						 bool abUsePrefilter = false) = 0;

	virtual void RenderDebugGeometry(iLowLevelGraphics *apLowLevel, const cColor &aColor) = 0;

	virtual bool CheckShapeCollision(iCollideShape *apShapeA, const cMatrixf &a_mtxA,
									 iCollideShape *apShapeB, const cMatrixf &a_mtxB,
									 cCollideData &aCollideData, int alMaxPoints,
									 bool correctNormalDirection = false) = 0;

	bool CheckShapeWorldCollision(cVector3f *apNewPos,
								  iCollideShape *apShape, const cMatrixf &a_mtxTransform,
								  iPhysicsBody *apSkipBody = NULL, bool abSkipStatic = false,
								  bool abIsCharacter = false,
								  iPhysicsWorldCollisionCallback *apCallback = NULL,
								  bool abCollideCharacter = true,
								  bool abDebug = false);

	void DestroyAll();

	cWorld3D *GetWorld3D() { return mpWorld3D; }
	void SetWorld3D(cWorld3D *apWorld3D) { mpWorld3D = apWorld3D; }
	//! @}

protected:
	tCollideShapeList mlstShapes;
	tPhysicsBodyList mlstBodies;
	tCharacterBodyList mlstCharBodies;
	tPhysicsMaterialMap m_mapMaterials;
	tPhysicsJointList mlstJoints;
	tPhysicsControllerList mlstControllers;
	cWorld3D *mpWorld3D;

	bool mbLogDebug;

	tCollidePointVec mvContactPoints;
	bool mbSaveContactPoints;
};

} // namespace hpl

#endif // HPL_PHYSICS_WORLD_H