Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

287 lines
11 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef PHYSICS_OBJECT_H
  8. #define PHYSICS_OBJECT_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "vphysics_interface.h"
  13. class IVP_Real_Object;
  14. class IVP_Environment;
  15. class IVP_U_Float_Point;
  16. class IVP_SurfaceManager;
  17. class IVP_Controller;
  18. class CPhysicsEnvironment;
  19. struct vphysics_save_cphysicsobject_t
  20. {
  21. const CPhysCollide *pCollide;
  22. const char *pName;
  23. float sphereRadius;
  24. bool isStatic;
  25. bool collisionEnabled;
  26. bool gravityEnabled;
  27. bool dragEnabled;
  28. bool motionEnabled;
  29. bool isAsleep;
  30. bool isTrigger;
  31. bool asleepSinceCreation; // has this been asleep since creation?
  32. bool hasTouchedDynamic;
  33. bool hasShadowController;
  34. short collideType;
  35. unsigned short gameIndex;
  36. int hingeAxis;
  37. int materialIndex;
  38. float mass;
  39. Vector rotInertia;
  40. float speedDamping;
  41. float rotSpeedDamping;
  42. Vector massCenterOverride;
  43. unsigned int callbacks;
  44. unsigned int gameFlags;
  45. unsigned int contentsMask;
  46. float volume;
  47. float dragCoefficient;
  48. float angDragCoefficient;
  49. IPhysicsShadowController *pShadow;
  50. //bool m_shadowTempGravityDisable;
  51. Vector origin;
  52. QAngle angles;
  53. Vector velocity;
  54. AngularImpulse angVelocity;
  55. DECLARE_SIMPLE_DATADESC();
  56. };
  57. enum
  58. {
  59. OBJ_AWAKE = 0, // awake, simulating
  60. OBJ_STARTSLEEP = 1, // going to sleep, but not queried yet
  61. OBJ_SLEEP = 2, // sleeping, no state changes since last query
  62. };
  63. class CPhysicsObject : public IPhysicsObject
  64. {
  65. public:
  66. CPhysicsObject( void );
  67. virtual ~CPhysicsObject( void );
  68. void Init( const CPhysCollide *pCollisionModel, IVP_Real_Object *pObject, int materialIndex, float volume, float drag, float angDrag );
  69. // IPhysicsObject functions
  70. bool IsStatic() const;
  71. bool IsAsleep() const;
  72. bool IsTrigger() const;
  73. bool IsFluid() const;
  74. bool IsHinged() const { return (m_hingedAxis != 0) ? true : false; }
  75. bool IsCollisionEnabled() const;
  76. bool IsGravityEnabled() const;
  77. bool IsDragEnabled() const;
  78. bool IsMotionEnabled() const;
  79. bool IsMoveable() const;
  80. bool IsAttachedToConstraint( bool bExternalOnly ) const;
  81. void EnableCollisions( bool enable );
  82. // Enable / disable gravity for this object
  83. void EnableGravity( bool enable );
  84. // Enable / disable air friction / drag for this object
  85. void EnableDrag( bool enable );
  86. void EnableMotion( bool enable );
  87. void SetGameData( void *pAppData );
  88. void *GetGameData( void ) const;
  89. void SetCallbackFlags( unsigned short callbackflags );
  90. unsigned short GetCallbackFlags( void ) const;
  91. void SetGameFlags( unsigned short userFlags );
  92. unsigned short GetGameFlags( void ) const;
  93. void SetGameIndex( unsigned short gameIndex );
  94. unsigned short GetGameIndex( void ) const;
  95. void Wake();
  96. void Sleep();
  97. void RecheckCollisionFilter();
  98. void RecheckContactPoints();
  99. void SetMass( float mass );
  100. float GetMass( void ) const;
  101. float GetInvMass( void ) const;
  102. void SetInertia( const Vector &inertia );
  103. Vector GetInertia( void ) const;
  104. Vector GetInvInertia( void ) const;
  105. void GetDamping( float *speed, float *rot ) const;
  106. void SetDamping( const float *speed, const float *rot );
  107. void SetDragCoefficient( float *pDrag, float *pAngularDrag );
  108. void SetBuoyancyRatio( float ratio );
  109. int GetMaterialIndex() const { return GetMaterialIndexInternal(); }
  110. void SetMaterialIndex( int materialIndex );
  111. inline int GetMaterialIndexInternal( void ) const { return m_materialIndex; }
  112. unsigned int GetContents() const { return m_contentsMask; }
  113. void SetContents( unsigned int contents );
  114. float GetSphereRadius() const;
  115. Vector GetMassCenterLocalSpace() const;
  116. float GetEnergy() const;
  117. void SetPosition( const Vector &worldPosition, const QAngle &angles, bool isTeleport = false );
  118. void SetPositionMatrix( const matrix3x4_t& matrix, bool isTeleport = false );
  119. void GetPosition( Vector *worldPosition, QAngle *angles ) const;
  120. void GetPositionMatrix( matrix3x4_t *positionMatrix ) const;
  121. void SetVelocity( const Vector *velocity, const AngularImpulse *angularVelocity );
  122. void SetVelocityInstantaneous( const Vector *velocity, const AngularImpulse *angularVelocity );
  123. void AddVelocity( const Vector *velocity, const AngularImpulse *angularVelocity );
  124. void GetVelocity( Vector *velocity, AngularImpulse *angularVelocity ) const;
  125. void GetImplicitVelocity( Vector *velocity, AngularImpulse *angularVelocity ) const;
  126. void GetVelocityAtPoint( const Vector &worldPosition, Vector *pVelocity ) const;
  127. void LocalToWorld( Vector *worldPosition, const Vector &localPosition ) const;
  128. void WorldToLocal( Vector *localPosition, const Vector &worldPosition ) const;
  129. void LocalToWorldVector( Vector *worldVector, const Vector &localVector ) const;
  130. void WorldToLocalVector( Vector *localVector, const Vector &worldVector ) const;
  131. void ApplyForceCenter( const Vector &forceVector );
  132. void ApplyForceOffset( const Vector &forceVector, const Vector &worldPosition );
  133. void ApplyTorqueCenter( const AngularImpulse & );
  134. void CalculateForceOffset( const Vector &forceVector, const Vector &worldPosition, Vector *centerForce, AngularImpulse *centerTorque ) const;
  135. void CalculateVelocityOffset( const Vector &forceVector, const Vector &worldPosition, Vector *centerVelocity, AngularImpulse *centerAngularVelocity ) const;
  136. float CalculateLinearDrag( const Vector &unitDirection ) const;
  137. float CalculateAngularDrag( const Vector &objectSpaceRotationAxis ) const;
  138. bool GetContactPoint( Vector *contactPoint, IPhysicsObject **contactObject ) const;
  139. void SetShadow( float maxSpeed, float maxAngularSpeed, bool allowPhysicsMovement, bool allowPhysicsRotation );
  140. void UpdateShadow( const Vector &targetPosition, const QAngle &targetAngles, bool tempDisableGravity, float timeOffset );
  141. void RemoveShadowController();
  142. int GetShadowPosition( Vector *position, QAngle *angles ) const;
  143. IPhysicsShadowController *GetShadowController( void ) const;
  144. float ComputeShadowControl( const hlshadowcontrol_params_t &params, float secondsToArrival, float dt );
  145. const CPhysCollide *GetCollide( void ) const;
  146. char const *GetName() const;
  147. float GetDragInDirection( const IVP_U_Float_Point &dir ) const;
  148. float GetAngularDragInDirection( const IVP_U_Float_Point &angVelocity ) const;
  149. void BecomeTrigger();
  150. void RemoveTrigger();
  151. void BecomeHinged( int localAxis );
  152. void RemoveHinged();
  153. IPhysicsFrictionSnapshot *CreateFrictionSnapshot();
  154. void DestroyFrictionSnapshot( IPhysicsFrictionSnapshot *pSnapshot );
  155. void OutputDebugInfo() const;
  156. // local functions
  157. inline IVP_Real_Object *GetObject( void ) const { return m_pObject; }
  158. inline int CallbackFlags( void ) const { return m_callbacks; }
  159. inline void AddCallbackFlags( unsigned short flags ) { m_callbacks |= flags; }
  160. inline void RemoveCallbackFlags( unsigned short flags ) { m_callbacks &= ~flags; }
  161. inline bool HasTouchedDynamic();
  162. inline void SetTouchedDynamic();
  163. void NotifySleep( void );
  164. void NotifyWake( void );
  165. int GetSleepState( void ) const { return m_sleepState; }
  166. inline void ForceSilentDelete() { m_forceSilentDelete = true; }
  167. inline int GetActiveIndex( void ) const { return m_activeIndex; }
  168. inline void SetActiveIndex( int index ) { m_activeIndex = index; }
  169. inline float GetBuoyancyRatio( void ) const { return m_buoyancyRatio; }
  170. // returns true if the mass center is set to the default for the collision model
  171. bool IsMassCenterAtDefault() const;
  172. // is this object simulated, or controlled by game logic?
  173. bool IsControlledByGame() const;
  174. IVP_SurfaceManager *GetSurfaceManager( void ) const;
  175. void WriteToTemplate( vphysics_save_cphysicsobject_t &objectTemplate );
  176. void InitFromTemplate( CPhysicsEnvironment *pEnvironment, void *pGameData, const vphysics_save_cphysicsobject_t &objectTemplate );
  177. CPhysicsEnvironment *GetVPhysicsEnvironment();
  178. const CPhysicsEnvironment *GetVPhysicsEnvironment() const;
  179. private:
  180. // NOTE: Local to vphysics, used to save/restore shadow controller
  181. void RestoreShadowController( IPhysicsShadowController *pShadowController );
  182. friend bool RestorePhysicsObject( const physrestoreparams_t &params, CPhysicsObject **ppObject );
  183. bool IsControlling( const IVP_Controller *pController ) const;
  184. float GetVolume() const;
  185. void SetVolume( float volume );
  186. // the mass has changed, recompute the drag information
  187. void RecomputeDragBases();
  188. void ClampVelocity();
  189. // NOTE: If m_pGameData is not the first member, the constructor debug code must be modified
  190. void *m_pGameData;
  191. IVP_Real_Object *m_pObject;
  192. const CPhysCollide *m_pCollide;
  193. IPhysicsShadowController *m_pShadow;
  194. Vector m_dragBasis;
  195. Vector m_angDragBasis;
  196. // these 5 should pack into a short
  197. // pack new bools here
  198. bool m_shadowTempGravityDisable : 5;
  199. bool m_hasTouchedDynamic : 1;
  200. bool m_asleepSinceCreation : 1;
  201. bool m_forceSilentDelete : 1;
  202. unsigned char m_sleepState : 2;
  203. unsigned char m_hingedAxis : 3;
  204. unsigned char m_collideType : 3;
  205. unsigned short m_gameIndex;
  206. private:
  207. unsigned short m_materialIndex;
  208. unsigned short m_activeIndex;
  209. unsigned short m_callbacks;
  210. unsigned short m_gameFlags;
  211. unsigned int m_contentsMask;
  212. float m_volume;
  213. float m_buoyancyRatio;
  214. float m_dragCoefficient;
  215. float m_angDragCoefficient;
  216. friend CPhysicsObject *CreatePhysicsObject( CPhysicsEnvironment *pEnvironment, const CPhysCollide *pCollisionModel, int materialIndex, const Vector &position, const QAngle& angles, objectparams_t *pParams, bool isStatic );
  217. friend bool CPhysicsEnvironment::TransferObject( IPhysicsObject *pObject, IPhysicsEnvironment *pDestinationEnvironment ); //need direct access to m_pShadow for Portal mod's physics object transfer system
  218. };
  219. // If you haven't ever touched a dynamic object, there's no need to search for contacting objects to
  220. // wakeup when you are deleted. So cache a bit here when contacts are generated
  221. inline bool CPhysicsObject::HasTouchedDynamic()
  222. {
  223. return m_hasTouchedDynamic;
  224. }
  225. inline void CPhysicsObject::SetTouchedDynamic()
  226. {
  227. m_hasTouchedDynamic = true;
  228. }
  229. extern CPhysicsObject *CreatePhysicsObject( CPhysicsEnvironment *pEnvironment, const CPhysCollide *pCollisionModel, int materialIndex, const Vector &position, const QAngle &angles, objectparams_t *pParams, bool isStatic );
  230. extern CPhysicsObject *CreatePhysicsSphere( CPhysicsEnvironment *pEnvironment, float radius, int materialIndex, const Vector &position, const QAngle &angles, objectparams_t *pParams, bool isStatic );
  231. extern void PostRestorePhysicsObject();
  232. extern IPhysicsObject *CreateObjectFromBuffer( CPhysicsEnvironment *pEnvironment, void *pGameData, unsigned char *pBuffer, unsigned int bufferSize, bool enableCollisions );
  233. extern IPhysicsObject *CreateObjectFromBuffer_UseExistingMemory( CPhysicsEnvironment *pEnvironment, void *pGameData, unsigned char *pBuffer, unsigned int bufferSize, CPhysicsObject *pExistingMemory );
  234. #endif // PHYSICS_OBJECT_H