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.

176 lines
9.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef PHYSICS_WORLD_H
  8. #define PHYSICS_WORLD_H
  9. #pragma once
  10. #include "vphysics_interface.h"
  11. #include "ivu_types.hxx"
  12. #include "utlvector.h"
  13. class IVP_Environment;
  14. class CSleepObjects;
  15. class CPhysicsListenerCollision;
  16. class CPhysicsListenerConstraint;
  17. class IVP_Listener_Collision;
  18. class IVP_Listener_Constraint;
  19. class IVP_Listener_Object;
  20. class IVP_Controller;
  21. class CPhysicsFluidController;
  22. class CCollisionSolver;
  23. class CPhysicsObject;
  24. class CDeleteQueue;
  25. class IVPhysicsDebugOverlay;
  26. struct constraint_limitedhingeparams_t;
  27. struct vphysics_save_iphysicsobject_t;
  28. class CPhysicsEnvironment : public IPhysicsEnvironment
  29. {
  30. public:
  31. CPhysicsEnvironment( void );
  32. ~CPhysicsEnvironment( void );
  33. virtual void SetDebugOverlay( CreateInterfaceFn debugOverlayFactory );
  34. virtual IVPhysicsDebugOverlay *GetDebugOverlay( void );
  35. void SetGravity( const Vector& gravityVector );
  36. IPhysicsObject *CreatePolyObject( const CPhysCollide *pCollisionModel, int materialIndex, const Vector& position, const QAngle& angles, objectparams_t *pParams );
  37. IPhysicsObject *CreatePolyObjectStatic( const CPhysCollide *pCollisionModel, int materialIndex, const Vector& position, const QAngle& angles, objectparams_t *pParams );
  38. virtual unsigned int GetObjectSerializeSize( IPhysicsObject *pObject ) const;
  39. virtual void SerializeObjectToBuffer( IPhysicsObject *pObject, unsigned char *pBuffer, unsigned int bufferSize );
  40. virtual IPhysicsObject *UnserializeObjectFromBuffer( void *pGameData, unsigned char *pBuffer, unsigned int bufferSize, bool enableCollisions );
  41. IPhysicsSpring *CreateSpring( IPhysicsObject *pObjectStart, IPhysicsObject *pObjectEnd, springparams_t *pParams );
  42. IPhysicsFluidController *CreateFluidController( IPhysicsObject *pFluidObject, fluidparams_t *pParams );
  43. IPhysicsConstraint *CreateRagdollConstraint( IPhysicsObject *pReferenceObject, IPhysicsObject *pAttachedObject, IPhysicsConstraintGroup *pGroup, const constraint_ragdollparams_t &ragdoll );
  44. virtual IPhysicsConstraint *CreateHingeConstraint( IPhysicsObject *pReferenceObject, IPhysicsObject *pAttachedObject, IPhysicsConstraintGroup *pGroup, const constraint_hingeparams_t &hinge );
  45. virtual IPhysicsConstraint *CreateLimitedHingeConstraint( IPhysicsObject *pReferenceObject, IPhysicsObject *pAttachedObject, IPhysicsConstraintGroup *pGroup, const constraint_limitedhingeparams_t &hinge );
  46. virtual IPhysicsConstraint *CreateFixedConstraint( IPhysicsObject *pReferenceObject, IPhysicsObject *pAttachedObject, IPhysicsConstraintGroup *pGroup, const constraint_fixedparams_t &fixed );
  47. virtual IPhysicsConstraint *CreateSlidingConstraint( IPhysicsObject *pReferenceObject, IPhysicsObject *pAttachedObject, IPhysicsConstraintGroup *pGroup, const constraint_slidingparams_t &sliding );
  48. virtual IPhysicsConstraint *CreateBallsocketConstraint( IPhysicsObject *pReferenceObject, IPhysicsObject *pAttachedObject, IPhysicsConstraintGroup *pGroup, const constraint_ballsocketparams_t &ballsocket );
  49. virtual IPhysicsConstraint *CreatePulleyConstraint( IPhysicsObject *pReferenceObject, IPhysicsObject *pAttachedObject, IPhysicsConstraintGroup *pGroup, const constraint_pulleyparams_t &pulley );
  50. virtual IPhysicsConstraint *CreateLengthConstraint( IPhysicsObject *pReferenceObject, IPhysicsObject *pAttachedObject, IPhysicsConstraintGroup *pGroup, const constraint_lengthparams_t &length );
  51. virtual IPhysicsConstraintGroup *CreateConstraintGroup( const constraint_groupparams_t &group );
  52. virtual void DestroyConstraintGroup( IPhysicsConstraintGroup *pGroup );
  53. void Simulate( float deltaTime );
  54. float GetSimulationTimestep() const;
  55. void SetSimulationTimestep( float timestep );
  56. float GetSimulationTime() const;
  57. float GetNextFrameTime() const;
  58. bool IsInSimulation() const;
  59. virtual void DestroyObject( IPhysicsObject * );
  60. virtual void DestroySpring( IPhysicsSpring * );
  61. virtual void DestroyFluidController( IPhysicsFluidController * );
  62. virtual void DestroyConstraint( IPhysicsConstraint * );
  63. virtual void SetCollisionEventHandler( IPhysicsCollisionEvent *pCollisionEvents );
  64. virtual void SetObjectEventHandler( IPhysicsObjectEvent *pObjectEvents );
  65. virtual void SetConstraintEventHandler( IPhysicsConstraintEvent *pConstraintEvents );
  66. virtual IPhysicsShadowController *CreateShadowController( IPhysicsObject *pObject, bool allowTranslation, bool allowRotation );
  67. virtual void DestroyShadowController( IPhysicsShadowController * );
  68. virtual IPhysicsMotionController *CreateMotionController( IMotionEvent *pHandler );
  69. virtual void DestroyMotionController( IPhysicsMotionController *pController );
  70. virtual IPhysicsPlayerController *CreatePlayerController( IPhysicsObject *pObject );
  71. virtual void DestroyPlayerController( IPhysicsPlayerController *pController );
  72. virtual IPhysicsVehicleController *CreateVehicleController( IPhysicsObject *pVehicleBodyObject, const vehicleparams_t &params, unsigned int nVehicleType, IPhysicsGameTrace *pGameTrace );
  73. virtual void DestroyVehicleController( IPhysicsVehicleController *pController );
  74. virtual void SetQuickDelete( bool bQuick )
  75. {
  76. m_deleteQuick = bQuick;
  77. }
  78. virtual bool ShouldQuickDelete() const { return m_deleteQuick; }
  79. virtual void TraceBox( trace_t *ptr, const Vector &mins, const Vector &maxs, const Vector &start, const Vector &end );
  80. virtual void SetCollisionSolver( IPhysicsCollisionSolver *pCollisionSolver );
  81. virtual void GetGravity( Vector *pGravityVector ) const;
  82. virtual int GetActiveObjectCount() const;
  83. virtual void GetActiveObjects( IPhysicsObject **pOutputObjectList ) const;
  84. virtual const IPhysicsObject **GetObjectList( int *pOutputObjectCount ) const;
  85. virtual bool TransferObject( IPhysicsObject *pObject, IPhysicsEnvironment *pDestinationEnvironment );
  86. IVP_Environment *GetIVPEnvironment( void ) { return m_pPhysEnv; }
  87. void ClearDeadObjects( void );
  88. IVP_Controller *GetDragController() { return m_pDragController; }
  89. const IVP_Controller *GetDragController() const { return m_pDragController; }
  90. virtual void SetAirDensity( float density );
  91. virtual float GetAirDensity( void ) const;
  92. virtual void ResetSimulationClock( void );
  93. virtual IPhysicsObject *CreateSphereObject( float radius, int materialIndex, const Vector &position, const QAngle &angles, objectparams_t *pParams, bool isStatic );
  94. virtual void CleanupDeleteList();
  95. virtual void EnableDeleteQueue( bool enable ) { m_queueDeleteObject = enable; }
  96. // debug
  97. virtual bool IsCollisionModelUsed( CPhysCollide *pCollide ) const;
  98. // trace against the physics world
  99. virtual void TraceRay( const Ray_t &ray, unsigned int fMask, IPhysicsTraceFilter *pTraceFilter, trace_t *pTrace );
  100. virtual void SweepCollideable( const CPhysCollide *pCollide, const Vector &vecAbsStart, const Vector &vecAbsEnd,
  101. const QAngle &vecAngles, unsigned int fMask, IPhysicsTraceFilter *pTraceFilter, trace_t *pTrace );
  102. // performance tuning
  103. virtual void GetPerformanceSettings( physics_performanceparams_t *pOutput ) const;
  104. virtual void SetPerformanceSettings( const physics_performanceparams_t *pSettings );
  105. // perf/cost statistics
  106. virtual void ReadStats( physics_stats_t *pOutput );
  107. virtual void ClearStats();
  108. virtual void EnableConstraintNotify( bool bEnable );
  109. // debug
  110. virtual void DebugCheckContacts(void);
  111. // Save/restore
  112. bool Save( const physsaveparams_t &params );
  113. void PreRestore( const physprerestoreparams_t &params );
  114. bool Restore( const physrestoreparams_t &params );
  115. void PostRestore();
  116. void PhantomAdd( CPhysicsObject *pObject );
  117. void PhantomRemove( CPhysicsObject *pObject );
  118. void AddPlayerController( IPhysicsPlayerController *pController );
  119. void RemovePlayerController( IPhysicsPlayerController *pController );
  120. IPhysicsPlayerController *FindPlayerController( IPhysicsObject *pObject );
  121. IPhysicsCollisionEvent *GetCollisionEventHandler();
  122. // a constraint is being disabled - report the game DLL as "broken"
  123. void NotifyConstraintDisabled( IPhysicsConstraint *pConstraint );
  124. private:
  125. IVP_Environment *m_pPhysEnv;
  126. IVP_Controller *m_pDragController;
  127. IVPhysicsDebugOverlay *m_pDebugOverlay; // Interface to use for drawing debug overlays.
  128. CUtlVector<IPhysicsObject *> m_objects;
  129. CUtlVector<IPhysicsObject *> m_deadObjects;
  130. CUtlVector<CPhysicsFluidController *> m_fluids;
  131. CUtlVector<IPhysicsPlayerController *> m_playerControllers;
  132. CSleepObjects *m_pSleepEvents;
  133. CPhysicsListenerCollision *m_pCollisionListener;
  134. CCollisionSolver *m_pCollisionSolver;
  135. CPhysicsListenerConstraint *m_pConstraintListener;
  136. CDeleteQueue *m_pDeleteQueue;
  137. int m_lastObjectThisTick;
  138. bool m_deleteQuick;
  139. bool m_inSimulation;
  140. bool m_queueDeleteObject;
  141. bool m_fixedTimestep;
  142. bool m_enableConstraintNotify;
  143. };
  144. extern IPhysicsEnvironment *CreatePhysicsEnvironment( void );
  145. class IVP_Synapse_Friction;
  146. class IVP_Real_Object;
  147. extern IVP_Real_Object *GetOppositeSynapseObject( IVP_Synapse_Friction *pfriction );
  148. extern IPhysicsObjectPairHash *CreateObjectPairHash();
  149. #endif // PHYSICS_WORLD_H