collision.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 _COLLISION_H_
  23. #define _COLLISION_H_
  24. #ifndef _DATACHUNKER_H_
  25. #include "core/dataChunker.h"
  26. #endif
  27. #ifndef _MPLANE_H_
  28. #include "math/mPlane.h"
  29. #endif
  30. #ifndef _MPOINT2_H_
  31. #include "math/mPoint2.h"
  32. #endif
  33. class SceneObject;
  34. class BaseMatInstance;
  35. //----------------------------------------------------------------------------
  36. struct Collision
  37. {
  38. SceneObject* object;
  39. Point3F point;
  40. VectorF normal;
  41. BaseMatInstance* material;
  42. // generate UV coordinate across (TSStatic) mesh based on
  43. // matching normals, this isn't done by default and is
  44. // primarily of interest in matching a collision point to
  45. // either a GUI control coordinate or finding a hit pixel in texture space
  46. bool generateTexCoord;
  47. // If generateTexCoord is set this will either be invalid (-1, -1)
  48. // or a value in texture space (assuming 0..1 UV mapping)
  49. Point2F texCoord;
  50. // Face and Face dot are currently only set by the extrudedPolyList
  51. // clipper. Values are otherwise undefined.
  52. U32 face; // Which face was hit
  53. F32 faceDot; // -Dot of face with poly normal
  54. F32 distance;
  55. Collision() :
  56. object( NULL ),
  57. material( NULL ),
  58. generateTexCoord(false),
  59. texCoord(-1.0f, -1.0f),
  60. face(0),
  61. faceDot(0.0f),
  62. distance(0)
  63. {
  64. }
  65. };
  66. class CollisionList
  67. {
  68. public:
  69. enum
  70. {
  71. MaxCollisions = 64
  72. };
  73. protected:
  74. dsize_t mCount;
  75. Collision mCollision[MaxCollisions];
  76. F32 mT;
  77. // MaxHeight is currently only set by the extrudedPolyList
  78. // clipper. It represents the maximum vertex z value of
  79. // the returned collision surfaces.
  80. F32 mMaxHeight;
  81. public:
  82. // Constructor
  83. CollisionList( /* const dsize_t reserveSize = MaxCollisions */ ) :
  84. mCount( 0 ), mT( 0.0f ), mMaxHeight( 0.0f )
  85. {
  86. }
  87. // Accessors
  88. S32 getCount() const { return mCount; }
  89. F32 getTime() const { return mT; }
  90. F32 getMaxHeight() const { return mMaxHeight; }
  91. const Collision &operator[] ( const dsize_t idx ) const
  92. {
  93. AssertFatal( idx < mCount, "Out of bounds index." );
  94. return mCollision[idx];
  95. }
  96. Collision &operator[] ( const dsize_t idx )
  97. {
  98. AssertFatal( idx < mCount, "Out of bounds index." );
  99. return mCollision[idx];
  100. }
  101. // Increment does NOT reset the collision which it returns. It is the job of
  102. // the caller to make sure that the entry has data properly assigned to it.
  103. Collision &increment()
  104. {
  105. return mCollision[mCount++];
  106. }
  107. void clear()
  108. {
  109. mCount = 0;
  110. }
  111. void setTime( const F32 t )
  112. {
  113. mT = t;
  114. }
  115. void setMaxHeight( const F32 height )
  116. {
  117. mMaxHeight = height;
  118. }
  119. };
  120. //----------------------------------------------------------------------------
  121. // BSP Collision tree
  122. // Solid nodes are represented by structures with NULL frontNode and
  123. // backNode pointers. The material field is only valid on a solid node.
  124. // There is no structure for empty nodes, frontNode or backNode
  125. // should be set to NULL to represent empty half-spaces.
  126. struct BSPNode
  127. {
  128. U32 material;
  129. PlaneF plane;
  130. BSPNode *frontNode, *backNode;
  131. };
  132. typedef Chunker<BSPNode> BSPTree;
  133. /// Extension of the collision structure to allow use with raycasting.
  134. /// @see Collision
  135. struct RayInfo : public Collision
  136. {
  137. RayInfo() : t(0), userData( NULL ) {}
  138. // The collision struct has object, point, normal & material.
  139. /// Distance along ray to contact point.
  140. F32 t;
  141. /// Set the point of intersection according to t and the given ray.
  142. ///
  143. /// Several pieces of code will not use ray information but rather rely
  144. /// on contact points directly, so it is a good thing to always set
  145. /// this in castRay functions.
  146. void setContactPoint( const Point3F& start, const Point3F& end )
  147. {
  148. Point3F startToEnd = end - start;
  149. startToEnd *= t;
  150. point = startToEnd + start;
  151. }
  152. /// A generic data void pointer.
  153. /// Passing a void* around to random objects of unknown class types that may
  154. /// interpret it differently would be very dangerous. Only use userData when
  155. /// you call castRay/etc on an individual object of a known type.
  156. void *userData;
  157. };
  158. #endif // _COLLISION_H_