CollisionModel.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition 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 BFG Edition 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 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition 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 BFG Edition 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 __COLLISIONMODELMANAGER_H__
  21. #define __COLLISIONMODELMANAGER_H__
  22. /*
  23. ===============================================================================
  24. Trace model vs. polygonal model collision detection.
  25. Short translations are the least expensive. Retrieving contact points is
  26. about as cheap as a short translation. Position tests are more expensive
  27. and rotations are most expensive.
  28. There is no position test at the start of a translation or rotation. In other
  29. words if a translation with start != end or a rotation with angle != 0 starts
  30. in solid, this goes unnoticed and the collision result is undefined.
  31. A translation with start == end or a rotation with angle == 0 performs
  32. a position test and fills in the trace_t structure accordingly.
  33. ===============================================================================
  34. */
  35. // contact type
  36. typedef enum {
  37. CONTACT_NONE, // no contact
  38. CONTACT_EDGE, // trace model edge hits model edge
  39. CONTACT_MODELVERTEX, // model vertex hits trace model polygon
  40. CONTACT_TRMVERTEX // trace model vertex hits model polygon
  41. } contactType_t;
  42. // contact info
  43. typedef struct {
  44. contactType_t type; // contact type
  45. idVec3 point; // point of contact
  46. idVec3 normal; // contact plane normal
  47. float dist; // contact plane distance
  48. int contents; // contents at other side of surface
  49. const idMaterial * material; // surface material
  50. int modelFeature; // contact feature on model
  51. int trmFeature; // contact feature on trace model
  52. int entityNum; // entity the contact surface is a part of
  53. int id; // id of clip model the contact surface is part of
  54. } contactInfo_t;
  55. // trace result
  56. typedef struct trace_s {
  57. float fraction; // fraction of movement completed, 1.0 = didn't hit anything
  58. idVec3 endpos; // final position of trace model
  59. idMat3 endAxis; // final axis of trace model
  60. contactInfo_t c; // contact information, only valid if fraction < 1.0
  61. } trace_t;
  62. typedef int cmHandle_t;
  63. #define CM_CLIP_EPSILON 0.25f // always stay this distance away from any model
  64. #define CM_BOX_EPSILON 1.0f // should always be larger than clip epsilon
  65. #define CM_MAX_TRACE_DIST 4096.0f // maximum distance a trace model may be traced, point traces are unlimited
  66. class idCollisionModelManager {
  67. public:
  68. virtual ~idCollisionModelManager() {}
  69. // Loads collision models from a map file.
  70. virtual void LoadMap( const idMapFile *mapFile ) = 0;
  71. // Frees all the collision models.
  72. virtual void FreeMap() = 0;
  73. virtual void Preload( const char *mapName ) = 0;
  74. // Gets the clip handle for a model.
  75. virtual cmHandle_t LoadModel( const char *modelName ) = 0;
  76. // Sets up a trace model for collision with other trace models.
  77. virtual cmHandle_t SetupTrmModel( const idTraceModel &trm, const idMaterial *material ) = 0;
  78. // Creates a trace model from a collision model, returns true if succesfull.
  79. virtual bool TrmFromModel( const char *modelName, idTraceModel &trm ) = 0;
  80. // Gets the name of a model.
  81. virtual const char * GetModelName( cmHandle_t model ) const = 0;
  82. // Gets the bounds of a model.
  83. virtual bool GetModelBounds( cmHandle_t model, idBounds &bounds ) const = 0;
  84. // Gets all contents flags of brushes and polygons of a model ored together.
  85. virtual bool GetModelContents( cmHandle_t model, int &contents ) const = 0;
  86. // Gets a vertex of a model.
  87. virtual bool GetModelVertex( cmHandle_t model, int vertexNum, idVec3 &vertex ) const = 0;
  88. // Gets an edge of a model.
  89. virtual bool GetModelEdge( cmHandle_t model, int edgeNum, idVec3 &start, idVec3 &end ) const = 0;
  90. // Gets a polygon of a model.
  91. virtual bool GetModelPolygon( cmHandle_t model, int polygonNum, idFixedWinding &winding ) const = 0;
  92. // Translates a trace model and reports the first collision if any.
  93. virtual void Translation( trace_t *results, const idVec3 &start, const idVec3 &end,
  94. const idTraceModel *trm, const idMat3 &trmAxis, int contentMask,
  95. cmHandle_t model, const idVec3 &modelOrigin, const idMat3 &modelAxis ) = 0;
  96. // Rotates a trace model and reports the first collision if any.
  97. virtual void Rotation( trace_t *results, const idVec3 &start, const idRotation &rotation,
  98. const idTraceModel *trm, const idMat3 &trmAxis, int contentMask,
  99. cmHandle_t model, const idVec3 &modelOrigin, const idMat3 &modelAxis ) = 0;
  100. // Returns the contents touched by the trace model or 0 if the trace model is in free space.
  101. virtual int Contents( const idVec3 &start,
  102. const idTraceModel *trm, const idMat3 &trmAxis, int contentMask,
  103. cmHandle_t model, const idVec3 &modelOrigin, const idMat3 &modelAxis ) = 0;
  104. // Stores all contact points of the trace model with the model, returns the number of contacts.
  105. virtual int Contacts( contactInfo_t *contacts, const int maxContacts, const idVec3 &start, const idVec6 &dir, const float depth,
  106. const idTraceModel *trm, const idMat3 &trmAxis, int contentMask,
  107. cmHandle_t model, const idVec3 &modelOrigin, const idMat3 &modelAxis ) = 0;
  108. // Tests collision detection.
  109. virtual void DebugOutput( const idVec3 &origin ) = 0;
  110. // Draws a model.
  111. virtual void DrawModel( cmHandle_t model, const idVec3 &modelOrigin, const idMat3 &modelAxis,
  112. const idVec3 &viewOrigin, const float radius ) = 0;
  113. // Prints model information, use -1 handle for accumulated model info.
  114. virtual void ModelInfo( cmHandle_t model ) = 0;
  115. // Lists all loaded models.
  116. virtual void ListModels() = 0;
  117. // Writes a collision model file for the given map entity.
  118. virtual bool WriteCollisionModelForMapEntity( const idMapEntity *mapEnt, const char *filename, const bool testTraceModel = true ) = 0;
  119. };
  120. extern idCollisionModelManager * collisionModelManager;
  121. #endif /* !__COLLISIONMODELMANAGER_H__ */