Physics_RigidBody.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 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 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 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. 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.
  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. } rigidBodyIState_t;
  39. typedef struct rigidBodyPState_s {
  40. int atRest; // set when simulation is suspended
  41. float lastTimeStep; // length of last time step
  42. idVec3 localOrigin; // origin relative to master
  43. idMat3 localAxis; // axis relative to master
  44. idVec6 pushVelocity; // push velocity
  45. idVec3 externalForce; // external force relative to center of mass
  46. idVec3 externalTorque; // external torque relative to center of mass
  47. rigidBodyIState_t i; // state used for integration
  48. } rigidBodyPState_t;
  49. class idPhysics_RigidBody : public idPhysics_Base {
  50. public:
  51. CLASS_PROTOTYPE( idPhysics_RigidBody );
  52. idPhysics_RigidBody( void );
  53. ~idPhysics_RigidBody( void );
  54. void Save( idSaveGame *savefile ) const;
  55. void Restore( idRestoreGame *savefile );
  56. // initialisation
  57. void SetFriction( const float linear, const float angular, const float contact );
  58. void SetBouncyness( const float b );
  59. // same as above but drop to the floor first
  60. void DropToFloor( void );
  61. // no contact determination and contact friction
  62. void NoContact( void );
  63. // enable/disable activation by impact
  64. void EnableImpact( void );
  65. void DisableImpact( void );
  66. public: // common physics interface
  67. void SetClipModel( idClipModel *model, float density, int id = 0, bool freeOld = true );
  68. idClipModel * GetClipModel( int id = 0 ) const;
  69. int GetNumClipModels( void ) const;
  70. void SetMass( float mass, int id = -1 );
  71. float GetMass( int id = -1 ) const;
  72. void SetContents( int contents, int id = -1 );
  73. int GetContents( int id = -1 ) const;
  74. const idBounds & GetBounds( int id = -1 ) const;
  75. const idBounds & GetAbsBounds( int id = -1 ) const;
  76. bool Evaluate( int timeStepMSec, int endTimeMSec );
  77. void UpdateTime( int endTimeMSec );
  78. int GetTime( void ) const;
  79. void GetImpactInfo( const int id, const idVec3 &point, impactInfo_t *info ) const;
  80. void ApplyImpulse( const int id, const idVec3 &point, const idVec3 &impulse );
  81. void AddForce( const int id, const idVec3 &point, const idVec3 &force );
  82. void Activate( void );
  83. void PutToRest( void );
  84. bool IsAtRest( void ) const;
  85. int GetRestStartTime( void ) const;
  86. bool IsPushable( void ) const;
  87. void SaveState( void );
  88. void RestoreState( void );
  89. void SetOrigin( const idVec3 &newOrigin, int id = -1 );
  90. void SetAxis( const idMat3 &newAxis, int id = -1 );
  91. void Translate( const idVec3 &translation, int id = -1 );
  92. void Rotate( const idRotation &rotation, int id = -1 );
  93. const idVec3 & GetOrigin( int id = 0 ) const;
  94. const idMat3 & GetAxis( int id = 0 ) const;
  95. void SetLinearVelocity( const idVec3 &newLinearVelocity, int id = 0 );
  96. void SetAngularVelocity( const idVec3 &newAngularVelocity, int id = 0 );
  97. const idVec3 & GetLinearVelocity( int id = 0 ) const;
  98. const idVec3 & GetAngularVelocity( int id = 0 ) const;
  99. void ClipTranslation( trace_t &results, const idVec3 &translation, const idClipModel *model ) const;
  100. void ClipRotation( trace_t &results, const idRotation &rotation, const idClipModel *model ) const;
  101. int ClipContents( const idClipModel *model ) const;
  102. void DisableClip( void );
  103. void EnableClip( void );
  104. void UnlinkClip( void );
  105. void LinkClip( void );
  106. bool EvaluateContacts( void );
  107. void SetPushed( int deltaTime );
  108. const idVec3 & GetPushedLinearVelocity( const int id = 0 ) const;
  109. const idVec3 & GetPushedAngularVelocity( const int id = 0 ) const;
  110. void SetMaster( idEntity *master, const bool orientated );
  111. void WriteToSnapshot( idBitMsgDelta &msg ) const;
  112. void ReadFromSnapshot( const idBitMsgDelta &msg );
  113. private:
  114. // state of the rigid body
  115. rigidBodyPState_t current;
  116. rigidBodyPState_t saved;
  117. // rigid body properties
  118. float linearFriction; // translational friction
  119. float angularFriction; // rotational friction
  120. float contactFriction; // friction with contact surfaces
  121. float bouncyness; // bouncyness
  122. idClipModel * clipModel; // clip model used for collision detection
  123. // derived properties
  124. float mass; // mass of body
  125. float inverseMass; // 1 / mass
  126. idVec3 centerOfMass; // center of mass of trace model
  127. idMat3 inertiaTensor; // mass distribution
  128. idMat3 inverseInertiaTensor; // inverse inertia tensor
  129. idODE * integrator; // integrator
  130. bool dropToFloor; // true if dropping to the floor and putting to rest
  131. bool testSolid; // true if testing for solid when dropping to the floor
  132. bool noImpact; // if true do not activate when another object collides
  133. bool noContact; // if true do not determine contacts and no contact friction
  134. // master
  135. bool hasMaster;
  136. bool isOrientated;
  137. private:
  138. friend void RigidBodyDerivatives( const float t, const void *clientData, const float *state, float *derivatives );
  139. void Integrate( const float deltaTime, rigidBodyPState_t &next );
  140. bool CheckForCollisions( const float deltaTime, rigidBodyPState_t &next, trace_t &collision );
  141. bool CollisionImpulse( const trace_t &collision, idVec3 &impulse );
  142. void ContactFriction( float deltaTime );
  143. void DropToFloorAndRest( void );
  144. bool TestIfAtRest( void ) const;
  145. void Rest( void );
  146. void DebugDraw( void );
  147. };
  148. #endif /* !__PHYSICS_RIGIDBODY_H__ */