File: PhysicsBody.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 (332 lines) | stat: -rw-r--r-- 10,690 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* 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_BODY_H
#define HPL_PHYSICS_BODY_H

#include "hpl1/engine/graphics/GraphicsTypes.h"
#include "hpl1/engine/scene/Entity3D.h"

namespace hpl {

class iPhysicsWorld;
class iCollideShape;
class iPhysicsMaterial;
class iLowLevelGraphics;
class cNode3D;
class cSoundEntity;
class iPhysicsJoint;
class cPhysicsContactData;
class iCharacterBody;

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

class iPhysicsBody;
class iPhysicsBodyCallback {
public:
	virtual ~iPhysicsBodyCallback() {}
	virtual bool OnBeginCollision(iPhysicsBody *apBody, iPhysicsBody *apCollideBody) = 0;
	virtual void OnCollide(iPhysicsBody *apBody, iPhysicsBody *apCollideBody,
						   cPhysicsContactData *apContactData) = 0;
};

typedef Common::List<iPhysicsBodyCallback *> tPhysicsBodyCallbackList;
typedef tPhysicsBodyCallbackList::iterator tPhysicsBodyCallbackListIt;

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

class cSaveData_iCollideShape : public iSerializable {
	kSerializableClassInit(cSaveData_iCollideShape) public : int mType;
	cMatrixf m_mtxOffset;
	cVector3f mvSize;
};

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

kSaveData_ChildClass(iEntity3D, iPhysicsBody) {
	kSaveData_ClassInit(iPhysicsBody) public : cContainerList<cSaveData_iCollideShape> mlstShapes;

	tString msMaterial;

	bool mbBlocksSound;
	bool mbIsCharacter;
	bool mbCollideCharacter;

	cVector3f mvLinearVelocity;
	cVector3f mvAngularVelocity;
	float mfLinearDamping;
	float mfAngularDamping;
	float mfMaxLinearSpeed;
	float mfMaxAngularSpeed;

	float mfMass;

	bool mbEnabled;
	bool mbAutoDisable;
	bool mbContinuousCollision;

	bool mbGravity;

	bool mbCollide;

	virtual iSaveObject *CreateSaveObject(cSaveObjectHandler * apSaveObjectHandler, cGame * apGame);
	virtual int GetSaveCreatePrio();
};

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

struct cPhysicsBody_Buoyancy {
	cPhysicsBody_Buoyancy() : mbActive(false), mfDensity(1),
							  mfLinearViscosity(1), mfAngularViscosity(1) {}

	bool mbActive;

	float mfDensity;
	float mfLinearViscosity;
	float mfAngularViscosity;

	cPlanef mSurface;
};

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

class iPhysicsBody : public iEntity3D {
	typedef iEntity3D super;

public:
	iPhysicsBody(const tString &asName, iPhysicsWorld *apWorld, iCollideShape *apShape);
	virtual ~iPhysicsBody();

	void Destroy();

	virtual void SetMaterial(iPhysicsMaterial *apMaterial) = 0;
	iPhysicsMaterial *GetMaterial();

	cNode3D *GetNode();
	cNode3D *CreateNode();

	iCollideShape *GetShape();

	void AddJoint(iPhysicsJoint *apJoint);
	iPhysicsJoint *GetJoint(int alIndex);
	int GetJointNum();
	void RemoveJoint(iPhysicsJoint *apJoint);

	virtual void SetLinearVelocity(const cVector3f &avVel) = 0;
	virtual cVector3f GetLinearVelocity() const = 0;
	virtual void SetAngularVelocity(const cVector3f &avVel) = 0;
	virtual cVector3f GetAngularVelocity() const = 0;
	virtual void SetLinearDamping(float afDamping) = 0;
	virtual float GetLinearDamping() const = 0;
	virtual void SetAngularDamping(float afDamping) = 0;
	virtual float GetAngularDamping() const = 0;
	virtual void SetMaxLinearSpeed(float afSpeed) = 0;
	virtual float GetMaxLinearSpeed() const = 0;
	virtual void SetMaxAngularSpeed(float afDamping) = 0;
	virtual float GetMaxAngularSpeed() const = 0;
	virtual cMatrixf GetInertiaMatrix() = 0;

	cVector3f GetVelocityAtPosition(cVector3f avPos);

	virtual void SetMass(float afMass) = 0;
	virtual float GetMass() const = 0;
	virtual void SetMassCentre(const cVector3f &avCentre) = 0;
	virtual cVector3f GetMassCentre() const = 0;

	virtual void AddForce(const cVector3f &avForce) = 0;
	virtual void AddForceAtPosition(const cVector3f &avForce, const cVector3f &avPos) = 0;
	virtual void AddTorque(const cVector3f &avTorque) = 0;
	virtual void AddImpulse(const cVector3f &avImpulse) = 0;
	virtual void AddImpulseAtPosition(const cVector3f &avImpulse, const cVector3f &avPos) = 0;

	virtual void SetEnabled(bool abEnabled) = 0;
	virtual bool GetEnabled() const = 0;
	virtual void SetAutoDisable(bool abEnabled) = 0;
	virtual bool GetAutoDisable() const = 0;
#if 0
	virtual void SetAutoDisableLinearThreshold(float afThresold) = 0;
	virtual float GetAutoDisableLinearThreshold() const = 0;
	virtual void SetAutoDisableAngularThreshold(float afThresold) = 0;
	virtual float GetAutoDisableAngularThreshold() const = 0;
	virtual void SetAutoDisableNumSteps(int alNum) = 0;
	virtual int GetAutoDisableNumSteps() const = 0;
#endif
	virtual void SetContinuousCollision(bool abOn) = 0;
	virtual bool GetContinuousCollision() = 0;

	virtual void SetGravity(bool abEnabled) = 0;
	virtual bool GetGravity() const = 0;

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

	void UpdateBeforeSimulate(float afTimeStep);
	void UpdateAfterSimulate(float afTimeStep);

	cBoundingVolume *GetBV() { return &mBoundingVolume; }

	void SetBlocksSound(bool abX) { mbBlocksSound = abX; }
	bool GetBlocksSound() { return mbBlocksSound; }

	void SetBlocksLight(bool abX) { mbBlocksLight = abX; }
	bool GetBlocksLight() { return mbBlocksLight; }

	void SetScrapeSoundEntity(cSoundEntity *apEntity) { mpScrapeSoundEntity = apEntity; }
	cSoundEntity *GetScrapeSoundEntity() { return mpScrapeSoundEntity; }
	void SetScrapeBody(iPhysicsBody *apBody) { mpScrapeBody = apBody; }
	iPhysicsBody *GetScrapeBody() { return mpScrapeBody; }
	const cMatrixf &GetPreveScrapeMatrix() { return m_mtxPrevScrapeMatrix; }
	void SetPreveScrapeMatrix(const cMatrixf &a_mtxMtx) { m_mtxPrevScrapeMatrix = a_mtxMtx; }

	void SetRollSoundEntity(cSoundEntity *apEntity) { mpRollSoundEntity = apEntity; }
	cSoundEntity *GetRollSoundEntity() { return mpRollSoundEntity; }

	void SetHasImpact(bool abX) { mbHasImpact = abX; }
	bool HasImpact() { return mbHasImpact; }
	void SetHasSlide(bool abX) { mbHasSlide = abX; }
	bool HasSlide() { return mbHasSlide; }

	bool HasCollision() { return mbHasCollision; }

	void SetUserData(void *apUserData) { mpUserData = apUserData; }
	void *GetUserData() { return mpUserData; }

	void AddBodyCallback(iPhysicsBodyCallback *apCallback);
	void RemoveBodyCallback(iPhysicsBodyCallback *apCallback);

	bool OnBeginCollision(iPhysicsBody *apBody);
	void OnCollide(iPhysicsBody *apBody, cPhysicsContactData *apContactData);

	void SetCollide(bool abX) { mbCollide = abX; }
	bool GetCollide() { return mbCollide; }

	void SetIsCharacter(bool abX) { mbIsCharacter = abX; }
	bool IsCharacter() { return mbIsCharacter; }

	void SetCollideCharacter(bool abX) { mbCollideCharacter = abX; }
	bool GetCollideCharacter() { return mbCollideCharacter; }

	void SetCharacterBody(iCharacterBody *apCharBody) { mpCharacterBody = apCharBody; }
	iCharacterBody *GetCharacterBody() { return mpCharacterBody; }

	void SetIsRagDoll(bool abX) { mbIsRagDoll = abX; }
	bool IsRagDoll() { return mbIsRagDoll; }

	void SetCollideRagDoll(bool abX) { mbCollideRagDoll = abX; }
	bool GetCollideRagDoll() { return mbCollideRagDoll; }

	void SetVolatile(bool abX) { mbVolatile = abX; }
	bool IsVolatile() { return mbVolatile; }

	void SetPushedByCharacterGravity(bool abX) { mbPushedByCharacterGravity = abX; }
	bool GetPushedByCharacterGravity() { return mbPushedByCharacterGravity; }

	void SetBuoyancyId(int alX) { mlBuoyancyId = alX; }
	void SetBuoyancyActive(bool abX) { mBuoyancy.mbActive = abX; }
	void SetBuoyancyDensity(float afX) { mBuoyancy.mfDensity = afX; }
	void SetBuoyancyLinearViscosity(float afX) { mBuoyancy.mfLinearViscosity = afX; }
	void SetBuoyancyAngularViscosity(float afX) { mBuoyancy.mfAngularViscosity = afX; }
	void SetBuoyancySurface(const cPlanef &aP) { mBuoyancy.mSurface = aP; }

	int GetBuoyancyId() { return mlBuoyancyId; }
	bool GetBuoyancyActive() { return mBuoyancy.mbActive; }
	float GetBuoyancyDensity() { return mBuoyancy.mfDensity; }
	float GetBuoyancyLinearViscosity() { return mBuoyancy.mfLinearViscosity; }
	float GetBuoyancyAngularViscosity() { return mBuoyancy.mfAngularViscosity; }
	cPlanef SetBuoyancySurface() { return mBuoyancy.mSurface; }

	void SetCanAttachCharacter(bool abX) { mbCanAttachCharacter = abX; }
	bool GetCanAttachCharacter() { return mbCanAttachCharacter; }
	void AddAttachedCharacter(iCharacterBody *apChar);
	void RemoveAttachedCharacter(iCharacterBody *apChar);

	iPhysicsWorld *GetWorld() { return mpWorld; }

	void DisableAfterSimulation() { mbDisableAfterSimulation = true; }

	// Entity implementation
	tString GetEntityType() { return "Body"; }

	// SaveObject implementation
	virtual iSaveData *CreateSaveData();
	virtual void SaveToSaveData(iSaveData *apSaveData);
	virtual void LoadFromSaveData(iSaveData *apSaveData);
	virtual void SaveDataSetup(cSaveObjectHandler *apSaveObjectHandler, cGame *apGame);

	virtual void DeleteLowLevel() = 0;

protected:
	void CreateSaveCollideShapes(cContainerList<cSaveData_iCollideShape> *apShapeList);

	iPhysicsWorld *mpWorld;
	iCollideShape *mpShape;
	iPhysicsMaterial *mpMaterial;
	cNode3D *mpNode;

	iCharacterBody *mpCharacterBody;

	Common::Array<iPhysicsJoint *> mvJoints;

	Common::List<iCharacterBody *> mlstAttachedCharacters;

	iPhysicsBody *mpScrapeBody;
	cSoundEntity *mpScrapeSoundEntity;
	cSoundEntity *mpRollSoundEntity;
	cMatrixf m_mtxPrevScrapeMatrix;
	bool mbHasImpact;
	bool mbHasSlide;
	int mlSlideCount;
	int mlImpactCount;

	bool mbPushedByCharacterGravity;

	bool mbBlocksSound;
	bool mbBlocksLight;
	bool mbIsCharacter;
	bool mbCollideCharacter;
	bool mbIsRagDoll;
	bool mbCollideRagDoll;
	bool mbVolatile;

	bool mbCanAttachCharacter;

	cPhysicsBody_Buoyancy mBuoyancy;
	int mlBuoyancyId;

	bool mbDisableAfterSimulation;

	bool mbHasCollision;

	tPhysicsBodyCallbackList mlstBodyCallbacks;

	void *mpUserData;

	bool mbCollide;
};

} // namespace hpl

#endif // HPL_PHYSICS_BODY_H