collision.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. {
  61. }
  62. };
  63. class CollisionList
  64. {
  65. public:
  66. enum
  67. {
  68. MaxCollisions = 64
  69. };
  70. protected:
  71. dsize_t mCount;
  72. Collision mCollision[MaxCollisions];
  73. F32 mT;
  74. // MaxHeight is currently only set by the extrudedPolyList
  75. // clipper. It represents the maximum vertex z value of
  76. // the returned collision surfaces.
  77. F32 mMaxHeight;
  78. public:
  79. // Constructor
  80. CollisionList( /* const dsize_t reserveSize = MaxCollisions */ ) :
  81. mCount( 0 ), mT( 0.0f ), mMaxHeight( 0.0f )
  82. {
  83. }
  84. // Accessors
  85. S32 getCount() const { return mCount; }
  86. F32 getTime() const { return mT; }
  87. F32 getMaxHeight() const { return mMaxHeight; }
  88. const Collision &operator[] ( const dsize_t idx ) const
  89. {
  90. AssertFatal( idx < mCount, "Out of bounds index." );
  91. return mCollision[idx];
  92. }
  93. Collision &operator[] ( const dsize_t idx )
  94. {
  95. AssertFatal( idx < mCount, "Out of bounds index." );
  96. return mCollision[idx];
  97. }
  98. // Increment does NOT reset the collision which it returns. It is the job of
  99. // the caller to make sure that the entry has data properly assigned to it.
  100. Collision &increment()
  101. {
  102. return mCollision[mCount++];
  103. }
  104. void clear()
  105. {
  106. mCount = 0;
  107. }
  108. void setTime( const F32 t )
  109. {
  110. mT = t;
  111. }
  112. void setMaxHeight( const F32 height )
  113. {
  114. mMaxHeight = height;
  115. }
  116. };
  117. //----------------------------------------------------------------------------
  118. // BSP Collision tree
  119. // Solid nodes are represented by structures with NULL frontNode and
  120. // backNode pointers. The material field is only valid on a solid node.
  121. // There is no structure for empty nodes, frontNode or backNode
  122. // should be set to NULL to represent empty half-spaces.
  123. struct BSPNode
  124. {
  125. U32 material;
  126. PlaneF plane;
  127. BSPNode *frontNode, *backNode;
  128. };
  129. typedef Chunker<BSPNode> BSPTree;
  130. /// Extension of the collision structure to allow use with raycasting.
  131. /// @see Collision
  132. struct RayInfo : public Collision
  133. {
  134. RayInfo() : userData( NULL ) {}
  135. // The collision struct has object, point, normal & material.
  136. /// Distance along ray to contact point.
  137. F32 t;
  138. /// Set the point of intersection according to t and the given ray.
  139. ///
  140. /// Several pieces of code will not use ray information but rather rely
  141. /// on contact points directly, so it is a good thing to always set
  142. /// this in castRay functions.
  143. void setContactPoint( const Point3F& start, const Point3F& end )
  144. {
  145. Point3F startToEnd = end - start;
  146. startToEnd *= t;
  147. point = startToEnd + start;
  148. }
  149. /// A generic data void pointer.
  150. /// Passing a void* around to random objects of unknown class types that may
  151. /// interpret it differently would be very dangerous. Only use userData when
  152. /// you call castRay/etc on an individual object of a known type.
  153. void *userData;
  154. };
  155. #endif // _COLLISION_H_