convex.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _CONVEX_H_
  23. #define _CONVEX_H_
  24. #ifndef _MMATH_H_
  25. #include "math/mMath.h"
  26. #endif
  27. #ifndef _TVECTOR_H_
  28. #include "core/util/tVector.h"
  29. #endif
  30. struct Collision;
  31. class CollisionList;
  32. struct CollisionStateList;
  33. class AbstractPolyList;
  34. class SceneObject;
  35. class BaseMatInstance;
  36. class Convex;
  37. //----------------------------------------------------------------------------
  38. class ConvexFeature
  39. {
  40. public:
  41. struct Edge {
  42. S32 vertex[2];
  43. };
  44. struct Face {
  45. VectorF normal;
  46. S32 vertex[3];
  47. };
  48. Vector<Point3F> mVertexList;
  49. Vector<Edge> mEdgeList;
  50. Vector<Face> mFaceList;
  51. BaseMatInstance* material;
  52. SceneObject* mObject;
  53. ConvexFeature()
  54. : mVertexList(64), mEdgeList(128), mFaceList(64), material( 0 ), mObject(NULL)
  55. {
  56. VECTOR_SET_ASSOCIATION(mVertexList);
  57. VECTOR_SET_ASSOCIATION(mEdgeList);
  58. VECTOR_SET_ASSOCIATION(mFaceList);
  59. }
  60. void reset();
  61. bool collide(ConvexFeature& cf,CollisionList* cList, F32 tol = 0.1);
  62. void testVertex(const Point3F& v,CollisionList* cList,bool,F32 tol);
  63. void testEdge(ConvexFeature* cf,const Point3F& s1, const Point3F& e1, CollisionList* cList, F32 tol);
  64. bool inVolume(const Point3F& v);
  65. };
  66. //----------------------------------------------------------------------------
  67. // TODO: This sucks... replace with registration object
  68. // for assigning type ids.
  69. enum ConvexType {
  70. TSConvexType,
  71. BoxConvexType,
  72. TerrainConvexType,
  73. ShapeBaseConvexType,
  74. TSStaticConvexType,
  75. AtlasChunkConvexType, ///< @deprecated
  76. AtlasConvexType,
  77. TSPolysoupConvexType,
  78. MeshRoadConvexType,
  79. ConvexShapeCollisionConvexType,
  80. ForestConvexType
  81. };
  82. //----------------------------------------------------------------------------
  83. struct CollisionState
  84. {
  85. CollisionStateList* mLista;
  86. CollisionStateList* mListb;
  87. Convex* mA;
  88. Convex* mB;
  89. F32 mDist; // Current estimated distance
  90. VectorF mDistvec; // Vector between closest points
  91. //
  92. CollisionState();
  93. virtual ~CollisionState();
  94. virtual void swap();
  95. virtual void set(Convex* a,Convex* b,const MatrixF& a2w, const MatrixF& b2w);
  96. virtual F32 distance(const MatrixF& a2w, const MatrixF& b2w, const F32 dontCareDist,
  97. const MatrixF* w2a = NULL, const MatrixF* _w2b = NULL);
  98. };
  99. //----------------------------------------------------------------------------
  100. struct CollisionStateList
  101. {
  102. static CollisionStateList sFreeList;
  103. CollisionStateList* mNext;
  104. CollisionStateList* mPrev;
  105. CollisionState* mState;
  106. CollisionStateList();
  107. void linkAfter(CollisionStateList* next);
  108. void unlink();
  109. bool isEmpty() { return mNext == this; }
  110. static CollisionStateList* alloc();
  111. void free();
  112. };
  113. //----------------------------------------------------------------------------
  114. struct CollisionWorkingList
  115. {
  116. static CollisionWorkingList sFreeList;
  117. struct WLink {
  118. CollisionWorkingList* mNext;
  119. CollisionWorkingList* mPrev;
  120. } wLink;
  121. struct RLink {
  122. CollisionWorkingList* mNext;
  123. CollisionWorkingList* mPrev;
  124. } rLink;
  125. Convex* mConvex;
  126. void wLinkAfter(CollisionWorkingList* next);
  127. void rLinkAfter(CollisionWorkingList* next);
  128. void unlink();
  129. CollisionWorkingList();
  130. static CollisionWorkingList* alloc();
  131. void free();
  132. };
  133. //----------------------------------------------------------------------------
  134. class Convex {
  135. /// @name Linked list management
  136. /// @{
  137. /// Next item in linked list of Convexes.
  138. Convex* mNext;
  139. /// Previous item in linked list of Convexes.
  140. Convex* mPrev;
  141. /// Insert this Convex after the provided convex
  142. /// @param next
  143. void linkAfter(Convex* next);
  144. /// Remove this Convex from the linked list
  145. void unlink();
  146. /// @}
  147. U32 mTag;
  148. static U32 sTag;
  149. protected:
  150. CollisionStateList mList; ///< Objects we're testing against
  151. CollisionWorkingList mWorking; ///< Objects within our bounds
  152. CollisionWorkingList mReference; ///< Other convex testing against us
  153. SceneObject* mObject; ///< Object this Convex is built around
  154. ConvexType mType; ///< Type of Convex this is @see ConvexType
  155. public:
  156. /// Constructor
  157. Convex();
  158. /// Destructor
  159. virtual ~Convex();
  160. /// Registers another Convex by linking it after this one
  161. void registerObject(Convex *convex);
  162. /// Runs through the linked list of Convex objects and removes the ones
  163. /// with no references
  164. void collectGarbage();
  165. /// Deletes all convex objects in the list
  166. void nukeList();
  167. /// Returns the type of this Convex
  168. ConvexType getType() const { return mType; }
  169. /// Returns the object this Convex is built from
  170. SceneObject* getObject() const { return mObject; }
  171. /// Adds the provided Convex to the list of objects within the bounds of this Convex
  172. /// @param ptr Convex to add to the working list of this object
  173. void addToWorkingList(Convex* ptr);
  174. /// Returns the list of objects currently inside the bounds of this Convex
  175. CollisionWorkingList& getWorkingList() { return mWorking; }
  176. /// Finds the closest
  177. CollisionState* findClosestState(const MatrixF& mat, const Point3F& scale, const F32 dontCareDist = 1);
  178. /// Returns the list of objects this object is testing against
  179. CollisionStateList* getStateList() const { return mList.mNext; }
  180. /// Updates the CollisionStateList (mList) with new collision states and removing
  181. /// ones no longer under consideration
  182. /// @param mat Used as the matrix to create a bounding box for updating the list
  183. /// @param scale Used to scale the bounding box
  184. /// @param displacement Bounding box displacement (optional)
  185. void updateStateList(const MatrixF& mat, const Point3F& scale, const Point3F* displacement = NULL);
  186. /// Updates the working collision list of objects which are currently colliding with
  187. /// (inside the bounds of) this Convex.
  188. ///
  189. /// @param box Used as the bounding box.
  190. /// @param colMask Mask of objects to check against.
  191. void updateWorkingList(const Box3F& box, const U32 colMask);
  192. /// Clear out the working collision list of objects
  193. void clearWorkingList();
  194. /// Returns the transform of the object this is built around
  195. virtual const MatrixF& getTransform() const;
  196. /// Returns the scale of the object this is built around
  197. virtual const Point3F& getScale() const;
  198. /// Returns the bounding box for the object this is built around in world space
  199. virtual Box3F getBoundingBox() const;
  200. /// Returns the object space bounding box for the object this is built around
  201. /// transformed and scaled
  202. /// @param mat Matrix to transform the object-space box by
  203. /// @param scale Scaling factor to scale the bounding box by
  204. virtual Box3F getBoundingBox(const MatrixF& mat, const Point3F& scale) const;
  205. /// Returns the farthest point, along a vector, still bound by the convex
  206. /// @param v Vector
  207. virtual Point3F support(const VectorF& v) const;
  208. /// This is used by the GJK collision in Vehicle.
  209. /// The Convex class should add verts, edges, and faces to the passed
  210. /// ConvexFeature that face towards the passed normal vector. Verts added
  211. /// in this way should also be transformed by the passed matrix.
  212. /// @param mat Transform which should be applied to verts added to the ConvexFeature
  213. /// @param n Normal vector
  214. /// @param cf ConvexFeature to add data to.
  215. virtual void getFeatures(const MatrixF& mat,const VectorF& n, ConvexFeature* cf);
  216. /// Builds a collision poly list out of this convex
  217. /// @param list (Out) Poly list built
  218. virtual void getPolyList(AbstractPolyList* list);
  219. ///
  220. bool getCollisionInfo(const MatrixF& mat, const Point3F& scale, CollisionList* cList,F32 tol);
  221. /// Render convex(s) for debugging purposes.
  222. virtual void renderWorkingList();
  223. /// Render this convex for debugging purposes.
  224. virtual void render();
  225. };
  226. #endif // _CONVEX_H_