Collsn.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //******************************************************************************
  2. // collsn.h - This file contains the Collision Detection System
  3. //
  4. // MechCommander 2
  5. //
  6. //---------------------------------------------------------------------------//
  7. // Copyright (C) Microsoft Corporation. All rights reserved. //
  8. //===========================================================================//
  9. #ifndef COLLSN_H
  10. #define COLLSN_H
  11. //------------------------------------------------------------------------------
  12. // Include Files
  13. #ifndef MCLIB_H
  14. #include "mclib.h"
  15. #endif
  16. #ifndef DCOLLSN_H
  17. #include "dcollsn.h"
  18. #endif
  19. #ifndef DGAMEOBJ_H
  20. #include "dgameobj.h"
  21. #endif
  22. //------------------------------------------------------------------------------
  23. // Macro Definitions
  24. #ifndef NO_ERR
  25. #define NO_ERR 0
  26. #endif
  27. //------------------------------------------------------------------------------
  28. // classes
  29. struct CollisionGridNode
  30. {
  31. GameObjectPtr object;
  32. CollisionGridNodePtr next;
  33. };
  34. //------------------------------------------------------------------------------
  35. struct CollisionAlertRecord
  36. {
  37. GameObjectPtr object1;
  38. GameObjectPtr object2;
  39. float currentDistance;
  40. float timeToImpact;
  41. };
  42. typedef CollisionAlertRecord *CollisionAlertRecordPtr;
  43. //------------------------------------------------------------------------------
  44. class GlobalCollisionAlert
  45. {
  46. //Data Members
  47. //-------------
  48. protected:
  49. CollisionAlertRecordPtr collisionAlerts;
  50. unsigned long maxAlerts;
  51. unsigned long nextRecord;
  52. public:
  53. GlobalCollisionAlert (void)
  54. {
  55. collisionAlerts = NULL;
  56. maxAlerts = 0;
  57. }
  58. long init (unsigned long maxCollisionAlerts);
  59. void destroy (void);
  60. ~GlobalCollisionAlert (void)
  61. {
  62. destroy();
  63. }
  64. long addRecord (GameObjectPtr obj1, GameObjectPtr obj2, float distance, float time);
  65. CollisionAlertRecordPtr findAlert (GameObjectPtr object, CollisionAlertRecordPtr startRecord = NULL);
  66. void purgeRecords (void);
  67. };
  68. extern GlobalCollisionAlert *globalCollisionAlert;
  69. //------------------------------------------------------------------------------
  70. class CollisionGrid
  71. {
  72. //Data Members
  73. //-------------
  74. protected:
  75. unsigned long xGridWidth; //Number of gridNodes in x direction
  76. unsigned long yGridWidth; //Number of gridNodes in y direction
  77. //In theory we would need a z but not for a mech game!!
  78. unsigned long maxGridRadius; //Max radius in (m) of each grid node.
  79. unsigned long maxObjects; //Max number of objects in world.
  80. CollisionGridNodePtr giantObjects; //Collection of objects larger than maxGridRadius
  81. CollisionGridNodePtr *grid; //Pointer to array of gridNodes layed out in space
  82. CollisionGridNodePtr nodes; //Actual grid nodes available to layout in space.
  83. unsigned long nextAvailableNode; //next node in nodes which can be used.
  84. Stuff::Vector3D gridOrigin; //Center point of the grid.
  85. bool gridIsGo; //Have we already allocated everything?
  86. unsigned long gridSize;
  87. unsigned long nodeSize;
  88. float gridXOffset;
  89. float gridYOffset;
  90. float gridXCheck;
  91. float gridYCheck;
  92. //Member Functions
  93. //-----------------
  94. public:
  95. void * operator new (size_t mySize);
  96. void operator delete (void *us);
  97. void init (void)
  98. {
  99. giantObjects = NULL;
  100. grid = NULL;
  101. nodes = NULL;
  102. xGridWidth = yGridWidth = 0;
  103. maxGridRadius = 0;
  104. nextAvailableNode = 0;
  105. gridOrigin.Zero();
  106. gridIsGo = FALSE;
  107. }
  108. CollisionGrid (void)
  109. {
  110. init();
  111. }
  112. long init (Stuff::Vector3D &newOrigin);
  113. void destroy (void);
  114. ~CollisionGrid (void)
  115. {
  116. destroy();
  117. }
  118. long add (unsigned long gridIndex, GameObjectPtr object);
  119. long add (GameObjectPtr object);
  120. void createGrid (void); //Put all objects in world into grids
  121. void checkGrid (GameObjectPtr object, CollisionGridNodePtr area); //Check each object against grid
  122. };
  123. //------------------------------------------------------------------------------
  124. struct CollisionRecord
  125. {
  126. GameObjectPtr obj1; //Which objects hit each other
  127. GameObjectPtr obj2;
  128. float time; //When did they do it relative to current frame.
  129. };
  130. //------------------------------------------------------------------------------
  131. class CollisionSystem
  132. {
  133. //Data Members
  134. //-------------
  135. protected:
  136. CollisionGridPtr collisionGrid;
  137. public:
  138. static unsigned long xGridSize;
  139. static unsigned long yGridSize;
  140. static unsigned long gridRadius;
  141. static unsigned long maxObjects;
  142. static unsigned long maxCollisions;
  143. static unsigned long numCollisions;
  144. float warningDist; //Distance to worry about short term collision avoidance (in World Units!!)
  145. float alertTime; //Time to worry about short term collision avoidance.
  146. static UserHeapPtr collisionHeap;
  147. //Member Functions
  148. //-----------------
  149. protected:
  150. CollisionRecordPtr findNextPending (void);
  151. public:
  152. void * operator new (size_t mySize);
  153. void operator delete (void *us);
  154. void init (void)
  155. {
  156. collisionGrid = NULL;
  157. }
  158. CollisionSystem (void)
  159. {
  160. init();
  161. }
  162. long init (FitIniFile *scenarioFile);
  163. void destroy (void);
  164. ~CollisionSystem (void)
  165. {
  166. destroy();
  167. }
  168. void checkObjects (void);
  169. static void detectCollision (GameObjectPtr obj1, GameObjectPtr obj2);
  170. void detectStaticCollision (GameObjectPtr obj1, GameObjectPtr obj2);
  171. float timeToImpact (GameObjectPtr obj1, GameObjectPtr obj2);
  172. static void checkExtents (GameObjectPtr obj1, GameObjectPtr obj2, float time);
  173. };
  174. extern CollisionSystem *collisionSystem;
  175. //------------------------------------------------------------------------------
  176. #endif