2 ===========================================================================
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
26 ===========================================================================
29 #ifndef __PHYSICS_RIGIDBODY_H__
30 #define __PHYSICS_RIGIDBODY_H__
33 ===================================================================================
37 Employs an impulse based dynamic simulation which is not very accurate but
38 relatively fast and still reliable due to the continuous collision detection.
40 ===================================================================================
43 extern const float RB_VELOCITY_MAX;
44 extern const int RB_VELOCITY_TOTAL_BITS;
45 extern const int RB_VELOCITY_EXPONENT_BITS;
46 extern const int RB_VELOCITY_MANTISSA_BITS;
48 typedef struct rididBodyIState_s {
49 idVec3 position; // position of trace model
50 idMat3 orientation; // orientation of trace model
51 idVec3 linearMomentum; // translational momentum relative to center of mass
52 idVec3 angularMomentum; // rotational momentum relative to center of mass
55 typedef struct rigidBodyPState_s {
56 int atRest; // set when simulation is suspended
57 float lastTimeStep; // length of last time step
58 idVec3 localOrigin; // origin relative to master
59 idMat3 localAxis; // axis relative to master
60 idVec6 pushVelocity; // push velocity
61 idVec3 externalForce; // external force relative to center of mass
62 idVec3 externalTorque; // external torque relative to center of mass
63 rigidBodyIState_t i; // state used for integration
66 class idPhysics_RigidBody : public idPhysics_Base {
70 CLASS_PROTOTYPE( idPhysics_RigidBody );
72 idPhysics_RigidBody( void );
73 ~idPhysics_RigidBody( void );
75 void Save( idSaveGame *savefile ) const;
76 void Restore( idRestoreGame *savefile );
79 void SetFriction( const float linear, const float angular, const float contact );
80 void SetBouncyness( const float b );
81 // same as above but drop to the floor first
82 void DropToFloor( void );
83 // no contact determination and contact friction
84 void NoContact( void );
85 // enable/disable activation by impact
86 void EnableImpact( void );
87 void DisableImpact( void );
89 public: // common physics interface
90 void SetClipModel( idClipModel *model, float density, int id = 0, bool freeOld = true );
91 idClipModel * GetClipModel( int id = 0 ) const;
92 int GetNumClipModels( void ) const;
94 void SetMass( float mass, int id = -1 );
95 float GetMass( int id = -1 ) const;
97 void SetContents( int contents, int id = -1 );
98 int GetContents( int id = -1 ) const;
100 const idBounds & GetBounds( int id = -1 ) const;
101 const idBounds & GetAbsBounds( int id = -1 ) const;
103 bool Evaluate( int timeStepMSec, int endTimeMSec );
104 void UpdateTime( int endTimeMSec );
105 int GetTime( void ) const;
107 void GetImpactInfo( const int id, const idVec3 &point, impactInfo_t *info ) const;
108 void ApplyImpulse( const int id, const idVec3 &point, const idVec3 &impulse );
109 void AddForce( const int id, const idVec3 &point, const idVec3 &force );
110 void Activate( void );
111 void PutToRest( void );
112 bool IsAtRest( void ) const;
113 int GetRestStartTime( void ) const;
114 bool IsPushable( void ) const;
116 void SaveState( void );
117 void RestoreState( void );
119 void SetOrigin( const idVec3 &newOrigin, int id = -1 );
120 void SetAxis( const idMat3 &newAxis, int id = -1 );
122 void Translate( const idVec3 &translation, int id = -1 );
123 void Rotate( const idRotation &rotation, int id = -1 );
125 const idVec3 & GetOrigin( int id = 0 ) const;
126 const idMat3 & GetAxis( int id = 0 ) const;
128 void SetLinearVelocity( const idVec3 &newLinearVelocity, int id = 0 );
129 void SetAngularVelocity( const idVec3 &newAngularVelocity, int id = 0 );
131 const idVec3 & GetLinearVelocity( int id = 0 ) const;
132 const idVec3 & GetAngularVelocity( int id = 0 ) const;
134 void ClipTranslation( trace_t &results, const idVec3 &translation, const idClipModel *model ) const;
135 void ClipRotation( trace_t &results, const idRotation &rotation, const idClipModel *model ) const;
136 int ClipContents( const idClipModel *model ) const;
138 void DisableClip( void );
139 void EnableClip( void );
141 void UnlinkClip( void );
142 void LinkClip( void );
144 bool EvaluateContacts( void );
146 void SetPushed( int deltaTime );
147 const idVec3 & GetPushedLinearVelocity( const int id = 0 ) const;
148 const idVec3 & GetPushedAngularVelocity( const int id = 0 ) const;
150 void SetMaster( idEntity *master, const bool orientated );
152 void WriteToSnapshot( idBitMsgDelta &msg ) const;
153 void ReadFromSnapshot( const idBitMsgDelta &msg );
156 // state of the rigid body
157 rigidBodyPState_t current;
158 rigidBodyPState_t saved;
160 // rigid body properties
161 float linearFriction; // translational friction
162 float angularFriction; // rotational friction
163 float contactFriction; // friction with contact surfaces
164 float bouncyness; // bouncyness
165 idClipModel * clipModel; // clip model used for collision detection
167 // derived properties
168 float mass; // mass of body
169 float inverseMass; // 1 / mass
170 idVec3 centerOfMass; // center of mass of trace model
171 idMat3 inertiaTensor; // mass distribution
172 idMat3 inverseInertiaTensor; // inverse inertia tensor
174 idODE * integrator; // integrator
175 bool dropToFloor; // true if dropping to the floor and putting to rest
176 bool testSolid; // true if testing for solid when dropping to the floor
177 bool noImpact; // if true do not activate when another object collides
178 bool noContact; // if true do not determine contacts and no contact friction
185 friend void RigidBodyDerivatives( const float t, const void *clientData, const float *state, float *derivatives );
186 void Integrate( const float deltaTime, rigidBodyPState_t &next );
187 bool CheckForCollisions( const float deltaTime, rigidBodyPState_t &next, trace_t &collision );
188 bool CollisionImpulse( const trace_t &collision, idVec3 &impulse );
189 void ContactFriction( float deltaTime );
190 void DropToFloorAndRest( void );
191 bool TestIfAtRest( void ) const;
193 void DebugDraw( void );
196 #endif /* !__PHYSICS_RIGIDBODY_H__ */