convex.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. PlaneConvexType
  82. };
  83. //----------------------------------------------------------------------------
  84. struct CollisionState
  85. {
  86. CollisionStateList* mLista;
  87. CollisionStateList* mListb;
  88. Convex* mA;
  89. Convex* mB;
  90. F32 mDist; // Current estimated distance
  91. VectorF mDistvec; // Vector between closest points
  92. //
  93. CollisionState();
  94. virtual ~CollisionState();
  95. virtual void swap();
  96. virtual void set(Convex* a,Convex* b,const MatrixF& a2w, const MatrixF& b2w);
  97. virtual F32 distance(const MatrixF& a2w, const MatrixF& b2w, const F32 dontCareDist,
  98. const MatrixF* w2a = NULL, const MatrixF* _w2b = NULL);
  99. };
  100. //----------------------------------------------------------------------------
  101. struct CollisionStateList
  102. {
  103. static CollisionStateList sFreeList;
  104. CollisionStateList* mNext;
  105. CollisionStateList* mPrev;
  106. CollisionState* mState;
  107. CollisionStateList();
  108. void linkAfter(CollisionStateList* next);
  109. void unlink();
  110. bool isEmpty() { return mNext == this; }
  111. static CollisionStateList* alloc();
  112. void free();
  113. };
  114. //----------------------------------------------------------------------------
  115. struct CollisionWorkingList
  116. {
  117. static CollisionWorkingList sFreeList;
  118. struct WLink {
  119. CollisionWorkingList* mNext;
  120. CollisionWorkingList* mPrev;
  121. } wLink;
  122. struct RLink {
  123. CollisionWorkingList* mNext;
  124. CollisionWorkingList* mPrev;
  125. } rLink;
  126. Convex* mConvex;
  127. void wLinkAfter(CollisionWorkingList* next);
  128. void rLinkAfter(CollisionWorkingList* next);
  129. void unlink();
  130. CollisionWorkingList();
  131. static CollisionWorkingList* alloc();
  132. void free();
  133. };
  134. //----------------------------------------------------------------------------
  135. class Convex {
  136. /// @name Linked list management
  137. /// @{
  138. /// Next item in linked list of Convexes.
  139. Convex* mNext;
  140. /// Previous item in linked list of Convexes.
  141. Convex* mPrev;
  142. /// Insert this Convex after the provided convex
  143. /// @param next
  144. void linkAfter(Convex* next);
  145. /// Remove this Convex from the linked list
  146. void unlink();
  147. /// @}
  148. U32 mTag;
  149. static U32 sTag;
  150. protected:
  151. CollisionStateList mList; ///< Objects we're testing against
  152. CollisionWorkingList mWorking; ///< Objects within our bounds
  153. CollisionWorkingList mReference; ///< Other convex testing against us
  154. SceneObject* mObject; ///< Object this Convex is built around
  155. ConvexType mType; ///< Type of Convex this is @see ConvexType
  156. public:
  157. /// Constructor
  158. Convex();
  159. /// Destructor
  160. virtual ~Convex();
  161. /// Registers another Convex by linking it after this one
  162. void registerObject(Convex *convex);
  163. /// Runs through the linked list of Convex objects and removes the ones
  164. /// with no references
  165. void collectGarbage();
  166. /// Deletes all convex objects in the list
  167. void nukeList();
  168. /// Returns the type of this Convex
  169. ConvexType getType() const { return mType; }
  170. /// Returns the object this Convex is built from
  171. SceneObject* getObject() const { return mObject; }
  172. /// Adds the provided Convex to the list of objects within the bounds of this Convex
  173. /// @param ptr Convex to add to the working list of this object
  174. void addToWorkingList(Convex* ptr);
  175. /// Returns the list of objects currently inside the bounds of this Convex
  176. CollisionWorkingList& getWorkingList() { return mWorking; }
  177. /// Finds the closest
  178. CollisionState* findClosestState(const MatrixF& mat, const Point3F& scale, const F32 dontCareDist = 1);
  179. /// Returns the list of objects this object is testing against
  180. CollisionStateList* getStateList() const { return mList.mNext; }
  181. /// Updates the CollisionStateList (mList) with new collision states and removing
  182. /// ones no longer under consideration
  183. /// @param mat Used as the matrix to create a bounding box for updating the list
  184. /// @param scale Used to scale the bounding box
  185. /// @param displacement Bounding box displacement (optional)
  186. void updateStateList(const MatrixF& mat, const Point3F& scale, const Point3F* displacement = NULL);
  187. /// Updates the working collision list of objects which are currently colliding with
  188. /// (inside the bounds of) this Convex.
  189. ///
  190. /// @param box Used as the bounding box.
  191. /// @param colMask Mask of objects to check against.
  192. void updateWorkingList(const Box3F& box, const U32 colMask);
  193. /// Clear out the working collision list of objects
  194. void clearWorkingList();
  195. /// Returns the transform of the object this is built around
  196. virtual const MatrixF& getTransform() const;
  197. /// Returns the scale of the object this is built around
  198. virtual const Point3F& getScale() const;
  199. /// Returns the bounding box for the object this is built around in world space
  200. virtual Box3F getBoundingBox() const;
  201. /// Returns the object space bounding box for the object this is built around
  202. /// transformed and scaled
  203. /// @param mat Matrix to transform the object-space box by
  204. /// @param scale Scaling factor to scale the bounding box by
  205. virtual Box3F getBoundingBox(const MatrixF& mat, const Point3F& scale) const;
  206. /// Returns the farthest point, along a vector, still bound by the convex
  207. /// @param v Vector
  208. virtual Point3F support(const VectorF& v) const;
  209. /// This is used by the GJK collision in Vehicle.
  210. /// The Convex class should add verts, edges, and faces to the passed
  211. /// ConvexFeature that face towards the passed normal vector. Verts added
  212. /// in this way should also be transformed by the passed matrix.
  213. /// @param mat Transform which should be applied to verts added to the ConvexFeature
  214. /// @param n Normal vector
  215. /// @param cf ConvexFeature to add data to.
  216. virtual void getFeatures(const MatrixF& mat,const VectorF& n, ConvexFeature* cf);
  217. /// Builds a collision poly list out of this convex
  218. /// @param list (Out) Poly list built
  219. virtual void getPolyList(AbstractPolyList* list);
  220. ///
  221. bool getCollisionInfo(const MatrixF& mat, const Point3F& scale, CollisionList* cList,F32 tol);
  222. /// Render convex(s) for debugging purposes.
  223. virtual void renderWorkingList();
  224. /// Render this convex for debugging purposes.
  225. virtual void render();
  226. };
  227. #endif // _CONVEX_H_