Physics_RigidBody.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition 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 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. 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.
  18. ===========================================================================
  19. */
  20. #ifndef __PHYSICS_RIGIDBODY_H__
  21. #define __PHYSICS_RIGIDBODY_H__
  22. /*
  23. ===================================================================================
  24. Rigid body physics
  25. Employs an impulse based dynamic simulation which is not very accurate but
  26. relatively fast and still reliable due to the continuous collision detection.
  27. ===================================================================================
  28. */
  29. extern const float RB_VELOCITY_MAX;
  30. extern const int RB_VELOCITY_TOTAL_BITS;
  31. extern const int RB_VELOCITY_EXPONENT_BITS;
  32. extern const int RB_VELOCITY_MANTISSA_BITS;
  33. typedef struct rididBodyIState_s {
  34. idVec3 position; // position of trace model
  35. idMat3 orientation; // orientation of trace model
  36. idVec3 linearMomentum; // translational momentum relative to center of mass
  37. idVec3 angularMomentum; // rotational momentum relative to center of mass
  38. rididBodyIState_s() :
  39. position( vec3_zero ),
  40. orientation( mat3_identity ),
  41. linearMomentum( vec3_zero ),
  42. angularMomentum( vec3_zero ) {
  43. }
  44. } rigidBodyIState_t;
  45. typedef struct rigidBodyPState_s {
  46. int atRest; // set when simulation is suspended
  47. float lastTimeStep; // length of last time step
  48. idVec3 localOrigin; // origin relative to master
  49. idMat3 localAxis; // axis relative to master
  50. idVec6 pushVelocity; // push velocity
  51. idVec3 externalForce; // external force relative to center of mass
  52. idVec3 externalTorque; // external torque relative to center of mass
  53. rigidBodyIState_t i; // state used for integration
  54. rigidBodyPState_s() :
  55. atRest( true ),
  56. lastTimeStep( 0 ),
  57. localOrigin( vec3_zero ),
  58. localAxis( mat3_identity ),
  59. pushVelocity( vec6_zero ),
  60. externalForce( vec3_zero ),
  61. externalTorque( vec3_zero ) {
  62. }
  63. } rigidBodyPState_t;
  64. class idPhysics_RigidBody : public idPhysics_Base {
  65. public:
  66. CLASS_PROTOTYPE( idPhysics_RigidBody );
  67. idPhysics_RigidBody();
  68. ~idPhysics_RigidBody();
  69. void Save( idSaveGame *savefile ) const;
  70. void Restore( idRestoreGame *savefile );
  71. // initialisation
  72. void SetFriction( const float linear, const float angular, const float contact );
  73. void SetBouncyness( const float b );
  74. // same as above but drop to the floor first
  75. void DropToFloor();
  76. // no contact determination and contact friction
  77. void NoContact();
  78. // enable/disable activation by impact
  79. void EnableImpact();
  80. void DisableImpact();
  81. public: // common physics interface
  82. void SetClipModel( idClipModel *model, float density, int id = 0, bool freeOld = true );
  83. idClipModel * GetClipModel( int id = 0 ) const;
  84. int GetNumClipModels() const;
  85. void SetMass( float mass, int id = -1 );
  86. float GetMass( int id = -1 ) const;
  87. void SetContents( int contents, int id = -1 );
  88. int GetContents( int id = -1 ) const;
  89. const idBounds & GetBounds( int id = -1 ) const;
  90. const idBounds & GetAbsBounds( int id = -1 ) const;
  91. bool Evaluate( int timeStepMSec, int endTimeMSec );
  92. bool Interpolate( const float fraction );
  93. void ResetInterpolationState( const idVec3 & origin, const idMat3 & axis );
  94. void UpdateTime( int endTimeMSec );
  95. int GetTime() const;
  96. void GetImpactInfo( const int id, const idVec3 &point, impactInfo_t *info ) const;
  97. void ApplyImpulse( const int id, const idVec3 &point, const idVec3 &impulse );
  98. void AddForce( const int id, const idVec3 &point, const idVec3 &force );
  99. void Activate();
  100. void PutToRest();
  101. bool IsAtRest() const;
  102. int GetRestStartTime() const;
  103. bool IsPushable() const;
  104. void SaveState();
  105. void RestoreState();
  106. void SetOrigin( const idVec3 &newOrigin, int id = -1 );
  107. void SetAxis( const idMat3 &newAxis, int id = -1 );
  108. void Translate( const idVec3 &translation, int id = -1 );
  109. void Rotate( const idRotation &rotation, int id = -1 );
  110. const idVec3 & GetOrigin( int id = 0 ) const;
  111. const idMat3 & GetAxis( int id = 0 ) const;
  112. void SetLinearVelocity( const idVec3 &newLinearVelocity, int id = 0 );
  113. void SetAngularVelocity( const idVec3 &newAngularVelocity, int id = 0 );
  114. const idVec3 & GetLinearVelocity( int id = 0 ) const;
  115. const idVec3 & GetAngularVelocity( int id = 0 ) const;
  116. void ClipTranslation( trace_t &results, const idVec3 &translation, const idClipModel *model ) const;
  117. void ClipRotation( trace_t &results, const idRotation &rotation, const idClipModel *model ) const;
  118. int ClipContents( const idClipModel *model ) const;
  119. void DisableClip();
  120. void EnableClip();
  121. void UnlinkClip();
  122. void LinkClip();
  123. bool EvaluateContacts();
  124. void SetPushed( int deltaTime );
  125. const idVec3 & GetPushedLinearVelocity( const int id = 0 ) const;
  126. const idVec3 & GetPushedAngularVelocity( const int id = 0 ) const;
  127. void SetMaster( idEntity *master, const bool orientated );
  128. void WriteToSnapshot( idBitMsg &msg ) const;
  129. void ReadFromSnapshot( const idBitMsg &msg );
  130. private:
  131. // state of the rigid body
  132. rigidBodyPState_t current;
  133. rigidBodyPState_t saved;
  134. // states for client interpolation
  135. rigidBodyPState_t previous;
  136. rigidBodyPState_t next;
  137. // rigid body properties
  138. float linearFriction; // translational friction
  139. float angularFriction; // rotational friction
  140. float contactFriction; // friction with contact surfaces
  141. float bouncyness; // bouncyness
  142. idClipModel * clipModel; // clip model used for collision detection
  143. // derived properties
  144. float mass; // mass of body
  145. float inverseMass; // 1 / mass
  146. idVec3 centerOfMass; // center of mass of trace model
  147. idMat3 inertiaTensor; // mass distribution
  148. idMat3 inverseInertiaTensor; // inverse inertia tensor
  149. idODE * integrator; // integrator
  150. bool dropToFloor; // true if dropping to the floor and putting to rest
  151. bool testSolid; // true if testing for solid when dropping to the floor
  152. bool noImpact; // if true do not activate when another object collides
  153. bool noContact; // if true do not determine contacts and no contact friction
  154. // master
  155. bool hasMaster;
  156. bool isOrientated;
  157. private:
  158. friend void RigidBodyDerivatives( const float t, const void *clientData, const float *state, float *derivatives );
  159. void Integrate( const float deltaTime, rigidBodyPState_t &next );
  160. bool CheckForCollisions( const float deltaTime, rigidBodyPState_t &next, trace_t &collision );
  161. bool CollisionImpulse( const trace_t &collision, idVec3 &impulse );
  162. void ContactFriction( float deltaTime );
  163. void DropToFloorAndRest();
  164. bool TestIfAtRest() const;
  165. void Rest();
  166. void DebugDraw();
  167. };
  168. #endif /* !__PHYSICS_RIGIDBODY_H__ */