convex.h

Engine/source/collision/convex.h

More...

Classes:

Public Enumerations

enum
ConvexType {
  TSConvexType 
  BoxConvexType 
  TerrainConvexType 
  ShapeBaseConvexType 
  TSStaticConvexType 
  AtlasChunkConvexType 
  AtlasConvexType 
  TSPolysoupConvexType 
  MeshRoadConvexType 
  ConvexShapeCollisionConvexType 
  ForestConvexType 
}

Detailed Description

Public Enumerations

ConvexType

Enumerator

TSConvexType
BoxConvexType
TerrainConvexType
ShapeBaseConvexType
TSStaticConvexType
AtlasChunkConvexType

Deprecated:

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