Physics.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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_H__
  21. #define __PHYSICS_H__
  22. /*
  23. ===============================================================================
  24. Physics abstract class
  25. A physics object is a tool to manipulate the position and orientation of
  26. an entity. The physics object is a container for idClipModels used for
  27. collision detection. The physics deals with moving these collision models
  28. through the world according to the laws of physics or other rules.
  29. The mass of a clip model is the volume of the clip model times the density.
  30. An arbitrary mass can however be set for specific clip models or the
  31. whole physics object. The contents of a clip model is a set of bit flags
  32. that define the contents. The clip mask defines the contents a clip model
  33. collides with.
  34. The linear velocity of a physics object is a vector that defines the
  35. translation of the center of mass in units per second. The angular velocity
  36. of a physics object is a vector that passes through the center of mass. The
  37. direction of this vector defines the axis of rotation and the magnitude
  38. defines the rate of rotation about the axis in radians per second.
  39. The gravity is the change in velocity per second due to gravitational force.
  40. Entities update their visual position and orientation from the physics
  41. using GetOrigin() and GetAxis(). Direct origin and axis changes of
  42. entities should go through the physics. In other words the physics origin
  43. and axis are updated first and the entity updates it's visual position
  44. from the physics.
  45. ===============================================================================
  46. */
  47. #define CONTACT_EPSILON 0.25f // maximum contact seperation distance
  48. class idEntity;
  49. typedef struct impactInfo_s {
  50. float invMass; // inverse mass
  51. idMat3 invInertiaTensor; // inverse inertia tensor
  52. idVec3 position; // impact position relative to center of mass
  53. idVec3 velocity; // velocity at the impact position
  54. } impactInfo_t;
  55. class idPhysics : public idClass {
  56. public:
  57. ABSTRACT_PROTOTYPE( idPhysics );
  58. virtual ~idPhysics( void );
  59. static int SnapTimeToPhysicsFrame( int t );
  60. // Must not be virtual
  61. void Save( idSaveGame *savefile ) const;
  62. void Restore( idRestoreGame *savefile );
  63. public: // common physics interface
  64. // set pointer to entity using physics
  65. virtual void SetSelf( idEntity *e ) = 0;
  66. // clip models
  67. virtual void SetClipModel( idClipModel *model, float density, int id = 0, bool freeOld = true ) = 0;
  68. virtual void SetClipBox( const idBounds &bounds, float density );
  69. virtual idClipModel * GetClipModel( int id = 0 ) const = 0;
  70. virtual int GetNumClipModels( void ) const = 0;
  71. // get/set the mass of a specific clip model or the whole physics object
  72. virtual void SetMass( float mass, int id = -1 ) = 0;
  73. virtual float GetMass( int id = -1 ) const = 0;
  74. // get/set the contents of a specific clip model or the whole physics object
  75. virtual void SetContents( int contents, int id = -1 ) = 0;
  76. virtual int GetContents( int id = -1 ) const = 0;
  77. // get/set the contents a specific clip model or the whole physics object collides with
  78. virtual void SetClipMask( int mask, int id = -1 ) = 0;
  79. virtual int GetClipMask( int id = -1 ) const = 0;
  80. // get the bounds of a specific clip model or the whole physics object
  81. virtual const idBounds & GetBounds( int id = -1 ) const = 0;
  82. virtual const idBounds & GetAbsBounds( int id = -1 ) const = 0;
  83. // evaluate the physics with the given time step, returns true if the object moved
  84. virtual bool Evaluate( int timeStepMSec, int endTimeMSec ) = 0;
  85. // update the time without moving
  86. virtual void UpdateTime( int endTimeMSec ) = 0;
  87. // get the last physics update time
  88. virtual int GetTime( void ) const = 0;
  89. // collision interaction between different physics objects
  90. virtual void GetImpactInfo( const int id, const idVec3 &point, impactInfo_t *info ) const = 0;
  91. virtual void ApplyImpulse( const int id, const idVec3 &point, const idVec3 &impulse ) = 0;
  92. virtual void AddForce( const int id, const idVec3 &point, const idVec3 &force ) = 0;
  93. virtual void Activate( void ) = 0;
  94. virtual void PutToRest( void ) = 0;
  95. virtual bool IsAtRest( void ) const = 0;
  96. virtual int GetRestStartTime( void ) const = 0;
  97. virtual bool IsPushable( void ) const = 0;
  98. // save and restore the physics state
  99. virtual void SaveState( void ) = 0;
  100. virtual void RestoreState( void ) = 0;
  101. // set the position and orientation in master space or world space if no master set
  102. virtual void SetOrigin( const idVec3 &newOrigin, int id = -1 ) = 0;
  103. virtual void SetAxis( const idMat3 &newAxis, int id = -1 ) = 0;
  104. // translate or rotate the physics object in world space
  105. virtual void Translate( const idVec3 &translation, int id = -1 ) = 0;
  106. virtual void Rotate( const idRotation &rotation, int id = -1 ) = 0;
  107. // get the position and orientation in world space
  108. virtual const idVec3 & GetOrigin( int id = 0 ) const = 0;
  109. virtual const idMat3 & GetAxis( int id = 0 ) const = 0;
  110. // set linear and angular velocity
  111. virtual void SetLinearVelocity( const idVec3 &newLinearVelocity, int id = 0 ) = 0;
  112. virtual void SetAngularVelocity( const idVec3 &newAngularVelocity, int id = 0 ) = 0;
  113. // get linear and angular velocity
  114. virtual const idVec3 & GetLinearVelocity( int id = 0 ) const = 0;
  115. virtual const idVec3 & GetAngularVelocity( int id = 0 ) const = 0;
  116. // gravity
  117. virtual void SetGravity( const idVec3 &newGravity ) = 0;
  118. virtual const idVec3 & GetGravity( void ) const = 0;
  119. virtual const idVec3 & GetGravityNormal( void ) const = 0;
  120. // get first collision when translating or rotating this physics object
  121. virtual void ClipTranslation( trace_t &results, const idVec3 &translation, const idClipModel *model ) const = 0;
  122. virtual void ClipRotation( trace_t &results, const idRotation &rotation, const idClipModel *model ) const = 0;
  123. virtual int ClipContents( const idClipModel *model ) const = 0;
  124. // disable/enable the clip models contained by this physics object
  125. virtual void DisableClip( void ) = 0;
  126. virtual void EnableClip( void ) = 0;
  127. // link/unlink the clip models contained by this physics object
  128. virtual void UnlinkClip( void ) = 0;
  129. virtual void LinkClip( void ) = 0;
  130. // contacts
  131. virtual bool EvaluateContacts( void ) = 0;
  132. virtual int GetNumContacts( void ) const = 0;
  133. virtual const contactInfo_t &GetContact( int num ) const = 0;
  134. virtual void ClearContacts( void ) = 0;
  135. virtual void AddContactEntity( idEntity *e ) = 0;
  136. virtual void RemoveContactEntity( idEntity *e ) = 0;
  137. // ground contacts
  138. virtual bool HasGroundContacts( void ) const = 0;
  139. virtual bool IsGroundEntity( int entityNum ) const = 0;
  140. virtual bool IsGroundClipModel( int entityNum, int id ) const = 0;
  141. // set the master entity for objects bound to a master
  142. virtual void SetMaster( idEntity *master, const bool orientated = true ) = 0;
  143. // set pushed state
  144. virtual void SetPushed( int deltaTime ) = 0;
  145. virtual const idVec3 & GetPushedLinearVelocity( const int id = 0 ) const = 0;
  146. virtual const idVec3 & GetPushedAngularVelocity( const int id = 0 ) const = 0;
  147. // get blocking info, returns NULL if the object is not blocked
  148. virtual const trace_t * GetBlockingInfo( void ) const = 0;
  149. virtual idEntity * GetBlockingEntity( void ) const = 0;
  150. // movement end times in msec for reached events at the end of predefined motion
  151. virtual int GetLinearEndTime( void ) const = 0;
  152. virtual int GetAngularEndTime( void ) const = 0;
  153. // networking
  154. virtual void WriteToSnapshot( idBitMsgDelta &msg ) const = 0;
  155. virtual void ReadFromSnapshot( const idBitMsgDelta &msg ) = 0;
  156. };
  157. #endif /* !__PHYSICS_H__ */