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.

175 lines
7.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef PHYSICS_SHARED_H
  7. #define PHYSICS_SHARED_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. class IPhysics;
  12. class IPhysicsEnvironment;
  13. class IPhysicsSurfaceProps;
  14. class IPhysicsCollision;
  15. class IPhysicsObject;
  16. class IPhysicsObjectPairHash;
  17. class CSoundPatch;
  18. extern IPhysicsObject *g_PhysWorldObject;
  19. extern IPhysics *physics;
  20. extern IPhysicsCollision *physcollision;
  21. extern IPhysicsEnvironment *physenv;
  22. #ifdef PORTAL
  23. extern IPhysicsEnvironment *physenv_main;
  24. #endif
  25. extern IPhysicsSurfaceProps *physprops;
  26. extern IPhysicsObjectPairHash *g_EntityCollisionHash;
  27. extern const objectparams_t g_PhysDefaultObjectParams;
  28. // Compute enough energy of a reference mass travelling at speed
  29. // makes numbers more intuitive
  30. #define MASS_SPEED2ENERGY(mass, speed) ((speed)*(speed)*(mass))
  31. // energy of a 10kg mass moving at speed
  32. #define MASS10_SPEED2ENERGY(speed) MASS_SPEED2ENERGY(10,speed)
  33. #define MASS_ENERGY2SPEED(mass,energy) (FastSqrt((energy)/mass))
  34. #define ENERGY_VOLUME_SCALE (1.0f / 15500.0f)
  35. #define FLUID_TIME_MAX 2.0f // keep track of last time hitting fluid for up to 2 seconds
  36. // VPHYSICS object game-specific flags
  37. #define FVPHYSICS_DMG_SLICE 0x0001 // does slice damage, not just blunt damage
  38. #define FVPHYSICS_CONSTRAINT_STATIC 0x0002 // object is constrained to the world, so it should behave like a static
  39. #define FVPHYSICS_PLAYER_HELD 0x0004 // object is held by the player, so have a very inelastic collision response
  40. #define FVPHYSICS_PART_OF_RAGDOLL 0x0008 // object is part of a client or server ragdoll
  41. #define FVPHYSICS_MULTIOBJECT_ENTITY 0x0010 // object is part of a multi-object entity
  42. #define FVPHYSICS_HEAVY_OBJECT 0x0020 // HULK SMASH! (Do large damage even if the mass is small)
  43. #define FVPHYSICS_PENETRATING 0x0040 // This object is currently stuck inside another object
  44. #define FVPHYSICS_NO_PLAYER_PICKUP 0x0080 // Player can't pick this up for some game rule reason
  45. #define FVPHYSICS_WAS_THROWN 0x0100 // Player threw this object
  46. #define FVPHYSICS_DMG_DISSOLVE 0x0200 // does dissolve damage, not just blunt damage
  47. #define FVPHYSICS_NO_IMPACT_DMG 0x0400 // don't do impact damage to anything
  48. #define FVPHYSICS_NO_NPC_IMPACT_DMG 0x0800 // Don't do impact damage to NPC's. This is temporary for NPC's shooting combine balls (sjb)
  49. #define FVPHYSICS_NO_SELF_COLLISIONS 0x8000 // don't collide with other objects that are part of the same entity
  50. //-----------------------------------------------------------------------------
  51. // Purpose: A little cache of current objects making noises
  52. //-----------------------------------------------------------------------------
  53. struct friction_t
  54. {
  55. CSoundPatch *patch;
  56. CBaseEntity *pObject;
  57. float flLastUpdateTime;
  58. float flLastEffectTime;
  59. };
  60. enum
  61. {
  62. TOUCH_START=0,
  63. TOUCH_END,
  64. };
  65. struct touchevent_t
  66. {
  67. CBaseEntity *pEntity0;
  68. CBaseEntity *pEntity1;
  69. int touchType;
  70. Vector endPoint; //sv
  71. Vector normal; //sv
  72. };
  73. struct fluidevent_t
  74. {
  75. EHANDLE hEntity;
  76. float impactTime;
  77. };
  78. void PhysFrictionSound( CBaseEntity *pEntity, IPhysicsObject *pObject, float energy, int surfaceProps, int surfacePropsHit );
  79. void PhysFrictionSound( CBaseEntity *pEntity, IPhysicsObject *pObject, const char *pSoundName, HSOUNDSCRIPTHANDLE& handle, float flVolume );
  80. void PhysCleanupFrictionSounds( CBaseEntity *pEntity );
  81. void PhysFrictionEffect( Vector &vecPos, Vector vecVel, float energy, int surfaceProps, int surfacePropsHit );
  82. // Convenience routine
  83. // ORs gameFlags with the physics object's current game flags
  84. inline unsigned short PhysSetGameFlags( IPhysicsObject *pPhys, unsigned short gameFlags )
  85. {
  86. unsigned short flags = pPhys->GetGameFlags();
  87. flags |= gameFlags;
  88. pPhys->SetGameFlags( flags );
  89. return flags;
  90. }
  91. // mask off gameFlags
  92. inline unsigned short PhysClearGameFlags( IPhysicsObject *pPhys, unsigned short gameFlags )
  93. {
  94. unsigned short flags = pPhys->GetGameFlags();
  95. flags &= ~gameFlags;
  96. pPhys->SetGameFlags( flags );
  97. return flags;
  98. }
  99. // Create a vphysics object based on a model
  100. IPhysicsObject *PhysModelCreate( CBaseEntity *pEntity, int modelIndex, const Vector &origin, const QAngle &angles, solid_t *pSolid = NULL );
  101. IPhysicsObject *PhysModelCreateBox( CBaseEntity *pEntity, const Vector &mins, const Vector &maxs, const Vector &origin, bool isStatic );
  102. IPhysicsObject *PhysModelCreateOBB( CBaseEntity *pEntity, const Vector &mins, const Vector &maxs, const Vector &origin, const QAngle &angle, bool isStatic );
  103. // Create a vphysics object based on a BSP model (unmoveable)
  104. IPhysicsObject *PhysModelCreateUnmoveable( CBaseEntity *pEntity, int modelIndex, const Vector &origin, const QAngle &angles );
  105. // Create a vphysics object based on an existing collision model
  106. IPhysicsObject *PhysModelCreateCustom( CBaseEntity *pEntity, const CPhysCollide *pModel, const Vector &origin, const QAngle &angles, const char *pName, bool isStatic, solid_t *pSolid = NULL );
  107. // Create a bbox collision model (these may be shared among entities, they are auto-deleted at end of level. do not manage)
  108. CPhysCollide *PhysCreateBbox( const Vector &mins, const Vector &maxs );
  109. // Create a vphysics sphere object
  110. IPhysicsObject *PhysSphereCreate( CBaseEntity *pEntity, float radius, const Vector &origin, solid_t &solid );
  111. // Destroy a physics object created using PhysModelCreate...()
  112. void PhysDestroyObject( IPhysicsObject *pObject, CBaseEntity *pEntity = NULL );
  113. void PhysDisableObjectCollisions( IPhysicsObject *pObject0, IPhysicsObject *pObject1 );
  114. void PhysDisableEntityCollisions( IPhysicsObject *pObject0, IPhysicsObject *pObject1 );
  115. void PhysDisableEntityCollisions( CBaseEntity *pEntity0, CBaseEntity *pEntity1 );
  116. void PhysEnableObjectCollisions( IPhysicsObject *pObject0, IPhysicsObject *pObject1 );
  117. void PhysEnableEntityCollisions( IPhysicsObject *pObject0, IPhysicsObject *pObject1 );
  118. void PhysEnableEntityCollisions( CBaseEntity *pEntity0, CBaseEntity *pEntity1 );
  119. bool PhysEntityCollisionsAreDisabled( CBaseEntity *pEntity0, CBaseEntity *pEntity1 );
  120. // create the world physics objects
  121. IPhysicsObject *PhysCreateWorld_Shared( CBaseEntity *pWorld, vcollide_t *pWorldCollide, const objectparams_t &defaultParams );
  122. // parse the parameters for a single solid from the model's collision data
  123. bool PhysModelParseSolid( solid_t &solid, CBaseEntity *pEntity, int modelIndex );
  124. // parse the parameters for a solid matching a particular index
  125. bool PhysModelParseSolidByIndex( solid_t &solid, CBaseEntity *pEntity, int modelIndex, int solidIndex );
  126. void PhysParseSurfaceData( class IPhysicsSurfaceProps *pProps, class IFileSystem *pFileSystem );
  127. // fill out this solid_t with the AABB defaults (high inertia/no rotation)
  128. void PhysGetDefaultAABBSolid( solid_t &solid );
  129. // Compute an output velocity based on sliding along the current contact points
  130. // in the closest direction toward inputVelocity.
  131. void PhysComputeSlideDirection( IPhysicsObject *pPhysics, const Vector &inputVelocity, const AngularImpulse &inputAngularVelocity,
  132. Vector *pOutputVelocity, Vector *pOutputAngularVelocity, float minMass );
  133. void PhysForceClearVelocity( IPhysicsObject *pPhys );
  134. bool PhysHasContactWithOtherInDirection( IPhysicsObject *pPhysics, const Vector &dir );
  135. //-----------------------------------------------------------------------------
  136. // Singleton access
  137. //-----------------------------------------------------------------------------
  138. IGameSystem* PhysicsGameSystem();
  139. #endif // PHYSICS_SHARED_H