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.

146 lines
3.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Physics constraint entities
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #ifndef PHYSCONSTRAINT_H
  8. #define PHYSCONSTRAINT_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "vphysics/constraints.h"
  13. struct hl_constraint_info_t
  14. {
  15. hl_constraint_info_t()
  16. {
  17. pObjects[0] = pObjects[1] = NULL;
  18. pGroup = NULL;
  19. anchorPosition[0].Init();
  20. anchorPosition[1].Init();
  21. swapped = false;
  22. massScale[0] = massScale[1] = 1.0f;
  23. }
  24. Vector anchorPosition[2];
  25. IPhysicsObject *pObjects[2];
  26. IPhysicsConstraintGroup *pGroup;
  27. float massScale[2];
  28. bool swapped;
  29. };
  30. abstract_class CPhysConstraint : public CLogicalEntity
  31. {
  32. DECLARE_CLASS( CPhysConstraint, CLogicalEntity );
  33. public:
  34. CPhysConstraint();
  35. ~CPhysConstraint();
  36. DECLARE_DATADESC();
  37. void Spawn( void );
  38. void Precache( void );
  39. void Activate( void );
  40. void ClearStaticFlag( IPhysicsObject *pObj );
  41. virtual void Deactivate();
  42. void OnBreak( void );
  43. void InputBreak( inputdata_t &inputdata );
  44. void InputOnBreak( inputdata_t &inputdata );
  45. void InputTurnOn( inputdata_t &inputdata );
  46. void InputTurnOff( inputdata_t &inputdata );
  47. int DrawDebugTextOverlays();
  48. void DrawDebugGeometryOverlays();
  49. void GetBreakParams( constraint_breakableparams_t &params, const hl_constraint_info_t &info );
  50. // the notify system calls this on the constrained entities - used to detect & follow teleports
  51. void NotifySystemEvent( CBaseEntity *pNotify, notify_system_event_t eventType, const notify_system_event_params_t &params );
  52. // gets called at setup time on first init and restore
  53. virtual void OnConstraintSetup( hl_constraint_info_t &info );
  54. // return the internal constraint object (used by sound gadgets)
  55. inline IPhysicsConstraint *GetPhysConstraint() { return m_pConstraint; }
  56. string_t GetNameAttach1( void ){ return m_nameAttach1; }
  57. string_t GetNameAttach2( void ){ return m_nameAttach2; }
  58. protected:
  59. void GetConstraintObjects( hl_constraint_info_t &info );
  60. void SetupTeleportationHandling( hl_constraint_info_t &info );
  61. bool ActivateConstraint( void );
  62. virtual IPhysicsConstraint *CreateConstraint( IPhysicsConstraintGroup *pGroup, const hl_constraint_info_t &info ) = 0;
  63. IPhysicsConstraint *m_pConstraint;
  64. // These are "template" values used to construct the hinge
  65. string_t m_nameAttach1;
  66. string_t m_nameAttach2;
  67. string_t m_breakSound;
  68. string_t m_nameSystem;
  69. float m_forceLimit;
  70. float m_torqueLimit;
  71. unsigned int m_teleportTick;
  72. float m_minTeleportDistance;
  73. COutputEvent m_OnBreak;
  74. };
  75. //-----------------------------------------------------------------------------
  76. // Purpose: Fixed breakable constraint
  77. //-----------------------------------------------------------------------------
  78. class CPhysFixed : public CPhysConstraint
  79. {
  80. DECLARE_CLASS( CPhysFixed, CPhysConstraint );
  81. public:
  82. IPhysicsConstraint *CreateConstraint( IPhysicsConstraintGroup *pGroup, const hl_constraint_info_t &info );
  83. // just for debugging - move to the position of the reference entity
  84. void MoveToRefPosition()
  85. {
  86. if ( m_pConstraint )
  87. {
  88. matrix3x4_t xformRef;
  89. m_pConstraint->GetConstraintTransform( &xformRef, NULL );
  90. IPhysicsObject *pObj = m_pConstraint->GetReferenceObject();
  91. if ( pObj && pObj->IsMoveable() )
  92. {
  93. Vector pos, posWorld;
  94. MatrixPosition( xformRef, pos );
  95. pObj->LocalToWorld(&posWorld, pos);
  96. SetAbsOrigin(posWorld);
  97. }
  98. }
  99. }
  100. int DrawDebugTextOverlays()
  101. {
  102. if ( m_debugOverlays & OVERLAY_TEXT_BIT )
  103. {
  104. MoveToRefPosition();
  105. }
  106. return BaseClass::DrawDebugTextOverlays();
  107. }
  108. void DrawDebugGeometryOverlays()
  109. {
  110. if ( m_debugOverlays & (OVERLAY_BBOX_BIT|OVERLAY_PIVOT_BIT|OVERLAY_ABSBOX_BIT) )
  111. {
  112. MoveToRefPosition();
  113. }
  114. BaseClass::DrawDebugGeometryOverlays();
  115. }
  116. };
  117. #endif // PHYSCONSTRAINT_H