Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OPC_RayCollider.h
Go to the documentation of this file.
1 /*
3  * OPCODE - Optimized Collision Detection
4  * Copyright (C) 2001 Pierre Terdiman
5  * Homepage: http://www.codercorner.com/Opcode.htm
6  */
8 
10 
16 
19 // Include Guard
20 #ifndef __OPC_RAYCOLLIDER_H__
21 #define __OPC_RAYCOLLIDER_H__
22 
24  {
25  public:
30 
32  float mDistance;
33  float mU, mV;
34  };
35 
37  {
38  public:
43 
44  inline_ udword GetNbFaces() const { return GetNbEntries()>>2; }
45  inline_ const CollisionFace* GetFaces() const { return (const CollisionFace*)GetEntries(); }
46 
48 
49  inline_ void AddFace(const CollisionFace& face) { Add(face.mFaceID).Add(face.mDistance).Add(face.mU).Add(face.mV); }
50  };
51 
52 #ifdef OPC_RAYHIT_CALLBACK
53 
59  typedef void (*HitCallback) (const CollisionFace& hit, void* user_data);
61 #endif
62 
64  {
65  public:
66  // Constructor / Destructor
67  RayCollider();
68  virtual ~RayCollider();
69 
71 
83  bool Collide(const Ray& world_ray, const Model& model, const Matrix4x4* world=null, udword* cache=null);
85  //
86  bool Collide(const Ray& world_ray, const AABBTree* tree, Container& box_indices);
87  // Settings
88 
89 #ifndef OPC_RAYHIT_CALLBACK
90 
98  inline_ void SetClosestHit(bool flag) { mClosestHit = flag; }
100 #endif
101 
109  inline_ void SetCulling(bool flag) { mCulling = flag; }
111 
113 
120  inline_ void SetMaxDist(float max_dist=MAX_FLOAT) { mMaxDist = max_dist; }
122 
123 #ifdef OPC_RAYHIT_CALLBACK
124  inline_ void SetHitCallback(HitCallback cb) { mHitCallback = cb; }
125  inline_ void SetUserData(void* user_data) { mUserData = user_data; }
126 #else
127 
135  inline_ void SetDestination(CollisionFaces* cf) { mStabbedFaces = cf; }
137 #endif
138  // Stats
140 
146  inline_ udword GetNbRayBVTests() const { return mNbRayBVTests; }
148 
150 
156  inline_ udword GetNbRayPrimTests() const { return mNbRayPrimTests; }
158 
159  // In-out test
161 
167  inline_ udword GetNbIntersections() const { return mNbIntersections; }
169 
171 
175  override(Collider) const char* ValidateSettings();
177 
178  protected:
179  // Ray in local space
183  Point mData, mData2;
184  // Stabbed faces
186 #ifdef OPC_RAYHIT_CALLBACK
187  HitCallback mHitCallback;
188  void* mUserData;
189 #else
191 #endif
192  // Stats
195  // In-out test
197  // Dequantization coeffs
200  // Settings
201  float mMaxDist;
202 #ifndef OPC_RAYHIT_CALLBACK
203  bool mClosestHit;
204 #endif
205  bool mCulling;
206  // Internal methods
207  void _SegmentStab(const AABBCollisionNode* node);
208  void _SegmentStab(const AABBNoLeafNode* node);
209  void _SegmentStab(const AABBQuantizedNode* node);
210  void _SegmentStab(const AABBQuantizedNoLeafNode* node);
211  void _SegmentStab(const AABBTreeNode* node, Container& box_indices);
212  void _RayStab(const AABBCollisionNode* node);
213  void _RayStab(const AABBNoLeafNode* node);
214  void _RayStab(const AABBQuantizedNode* node);
215  void _RayStab(const AABBQuantizedNoLeafNode* node);
216  void _RayStab(const AABBTreeNode* node, Container& box_indices);
217  // Overlap tests
218  inline_ bool RayAABBOverlap(const Point& center, const Point& extents);
219  inline_ bool SegmentAABBOverlap(const Point& center, const Point& extents);
220  inline_ bool RayTriOverlap(const Point& vert0, const Point& vert1, const Point& vert2);
221  // Init methods
222  bool InitQuery(const Ray& world_ray, const Matrix4x4* world=null, udword* face_id=null);
223  };
224 
225 #endif // __OPC_RAYCOLLIDER_H__