ISceneCollisionManager.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #ifndef IRR_I_SCENE_COLLISION_MANAGER_H_INCLUDED
  5. #define IRR_I_SCENE_COLLISION_MANAGER_H_INCLUDED
  6. #include "IReferenceCounted.h"
  7. #include "vector3d.h"
  8. #include "triangle3d.h"
  9. #include "position2d.h"
  10. #include "line3d.h"
  11. namespace irr
  12. {
  13. namespace scene
  14. {
  15. class ISceneNode;
  16. class ICameraSceneNode;
  17. class ITriangleSelector;
  18. class IMeshBuffer;
  19. struct SCollisionHit
  20. {
  21. //! Point of collision
  22. core::vector3df Intersection;
  23. //! Triangle with which we collided
  24. core::triangle3df Triangle;
  25. //! Triangle selector which contained the colliding triangle (useful when having MetaTriangleSelector)
  26. const ITriangleSelector* TriangleSelector;
  27. //! Node which contained the triangle (is 0 when selector doesn't have that information)
  28. ISceneNode* Node;
  29. //! Meshbuffer which contained the triangle (is 0 when the selector doesn't have that information, only works when selectors are created per meshbuffer)
  30. const IMeshBuffer* MeshBuffer;
  31. //! Index of selected material of the triangle in the SceneNode. Usually only valid when MeshBuffer is also set, otherwise always 0
  32. irr::u32 MaterialIndex;
  33. SCollisionHit() : TriangleSelector(0), Node(0), MeshBuffer(0), MaterialIndex(0)
  34. {}
  35. };
  36. //! The Scene Collision Manager provides methods for performing collision tests and picking on scene nodes.
  37. class ISceneCollisionManager : public virtual IReferenceCounted
  38. {
  39. public:
  40. //! Finds the nearest collision point of a line and lots of triangles, if there is one.
  41. /** \param hitResult: Contains collision result when there was a collision detected.
  42. \param ray: Line with which collisions are tested.
  43. \param selector: TriangleSelector to be used for the collision check.
  44. \return true if a collision was detected and false if not. */
  45. virtual bool getCollisionPoint(SCollisionHit& hitResult, const core::line3d<f32>& ray,
  46. ITriangleSelector* selector) = 0;
  47. //! Finds the nearest collision point of a line and lots of triangles, if there is one.
  48. /** \param ray: Line with which collisions are tested.
  49. \param selector: TriangleSelector containing the triangles. It
  50. can be created for example using
  51. ISceneManager::createTriangleSelector() or
  52. ISceneManager::createTriangleOctreeSelector().
  53. \param outCollisionPoint: If a collision is detected, this will
  54. contain the position of the nearest collision to the line-start.
  55. \param outTriangle: If a collision is detected, this will
  56. contain the triangle with which the ray collided.
  57. \param outNode: If a collision is detected, this will contain
  58. the scene node associated with the triangle that was hit.
  59. \return True if a collision was detected and false if not. */
  60. virtual bool getCollisionPoint(const core::line3d<f32>& ray,
  61. ITriangleSelector* selector, core::vector3df& outCollisionPoint,
  62. core::triangle3df& outTriangle, ISceneNode*& outNode)
  63. {
  64. SCollisionHit hitResult;
  65. if ( getCollisionPoint(hitResult, ray, selector) )
  66. {
  67. outCollisionPoint = hitResult.Intersection;
  68. outTriangle = hitResult.Triangle;
  69. outNode = hitResult.Node;
  70. return true;
  71. }
  72. return false;
  73. }
  74. //! Collides a moving ellipsoid with a 3d world with gravity and returns the resulting new position of the ellipsoid.
  75. /** This can be used for moving a character in a 3d world: The
  76. character will slide at walls and is able to walk up stairs.
  77. The method used how to calculate the collision result position
  78. is based on the paper "Improved Collision detection and
  79. Response" by Kasper Fauerby.
  80. \param selector: TriangleSelector containing the triangles of
  81. the world. It can be created for example using
  82. ISceneManager::createTriangleSelector() or
  83. ISceneManager::createTriangleOctreeSelector().
  84. \param ellipsoidPosition: Position of the ellipsoid.
  85. \param ellipsoidRadius: Radius of the ellipsoid.
  86. \param ellipsoidDirectionAndSpeed: Direction and speed of the
  87. movement of the ellipsoid.
  88. \param triout: Optional parameter where the last triangle
  89. causing a collision is stored, if there is a collision.
  90. \param hitPosition: Return value for the position of the collision
  91. \param outFalling: Is set to true if the ellipsoid is falling
  92. down, caused by gravity.
  93. \param outNode: the node with which the ellipsoid collided (if any)
  94. \param slidingSpeed: DOCUMENTATION NEEDED.
  95. \param gravityDirectionAndSpeed: Direction and force of gravity.
  96. \return New position of the ellipsoid. */
  97. virtual core::vector3df getCollisionResultPosition(
  98. ITriangleSelector* selector,
  99. const core::vector3df &ellipsoidPosition,
  100. const core::vector3df& ellipsoidRadius,
  101. const core::vector3df& ellipsoidDirectionAndSpeed,
  102. core::triangle3df& triout,
  103. core::vector3df& hitPosition,
  104. bool& outFalling,
  105. ISceneNode*& outNode,
  106. f32 slidingSpeed = 0.0005f,
  107. const core::vector3df& gravityDirectionAndSpeed
  108. = core::vector3df(0.0f, 0.0f, 0.0f)) = 0;
  109. //! Returns a 3d ray which would go through the 2d screen coordinates.
  110. /** \param pos: Screen coordinates in pixels.
  111. \param camera: Camera from which the ray starts. If null, the
  112. active camera is used.
  113. \return Ray starting from the position of the camera and ending
  114. at a length of the far value of the camera at a position which
  115. would be behind the 2d screen coordinates. */
  116. virtual core::line3d<f32> getRayFromScreenCoordinates(
  117. const core::position2d<s32>& pos, const ICameraSceneNode* camera = 0) = 0;
  118. //! Calculates 2d screen position from a 3d position.
  119. /** \param pos: 3D position in world space to be transformed
  120. into 2d.
  121. \param camera: Camera to be used. If null, the currently active
  122. camera is used.
  123. \param useViewPort: Calculate screen coordinates relative to
  124. the current view port. Please note that unless the driver does
  125. not take care of the view port, it is usually best to get the
  126. result in absolute screen coordinates (flag=false).
  127. \return 2d screen coordinates which a object in the 3d world
  128. would have if it would be rendered to the screen. If the 3d
  129. position is behind the camera, it is set to (-1000,-1000). In
  130. most cases you can ignore this fact, because if you use this
  131. method for drawing a decorator over a 3d object, it will be
  132. clipped by the screen borders. */
  133. virtual core::position2d<s32> getScreenCoordinatesFrom3DPosition(
  134. const core::vector3df& pos, const ICameraSceneNode* camera=0, bool useViewPort=false) = 0;
  135. //! Gets the scene node, which is currently visible under the given screen coordinates, viewed from the currently active camera.
  136. /** The collision tests are done using a bounding box for each
  137. scene node. You can limit the recursive search so just all children of the specified root are tested.
  138. \param pos: Position in pixel screen coordinates, under which
  139. the returned scene node will be.
  140. \param idBitMask: Only scene nodes with an id with bits set
  141. like in this mask will be tested. If the BitMask is 0, this
  142. feature is disabled.
  143. Please note that the default node id of -1 will match with
  144. every bitmask != 0
  145. \param bNoDebugObjects: Doesn't take debug objects into account
  146. when true. These are scene nodes with IsDebugObject() = true.
  147. \param root If different from 0, the search is limited to the children of this node.
  148. \return Visible scene node under screen coordinates with
  149. matching bits in its id. If there is no scene node under this
  150. position, 0 is returned. */
  151. virtual ISceneNode* getSceneNodeFromScreenCoordinatesBB(const core::position2d<s32>& pos,
  152. s32 idBitMask=0, bool bNoDebugObjects=false, ISceneNode* root=0) =0;
  153. //! Returns the nearest scene node which collides with a 3d ray and whose id matches a bitmask.
  154. /** The collision tests are done using a bounding box for each
  155. scene node. The recursive search can be limited be specifying a scene node.
  156. \param ray Line with which collisions are tested.
  157. \param idBitMask Only scene nodes with an id which matches at
  158. least one of the bits contained in this mask will be tested.
  159. However, if this parameter is 0, then all nodes are checked.
  160. \param bNoDebugObjects: Doesn't take debug objects into account when true. These
  161. are scene nodes with IsDebugObject() = true.
  162. \param root If different from 0, the search is limited to the children of this node.
  163. \return Scene node nearest to ray.start, which collides with
  164. the ray and matches the idBitMask, if the mask is not null. If
  165. no scene node is found, 0 is returned. */
  166. virtual ISceneNode* getSceneNodeFromRayBB(const core::line3d<f32>& ray,
  167. s32 idBitMask=0, bool bNoDebugObjects=false, ISceneNode* root=0) =0;
  168. //! Get the scene node, which the given camera is looking at and whose id matches the bitmask.
  169. /** A ray is simply cast from the position of the camera to
  170. the view target position, and all scene nodes are tested
  171. against this ray. The collision tests are done using a bounding
  172. box for each scene node.
  173. \param camera: Camera from which the ray is cast.
  174. \param idBitMask: Only scene nodes with an id which matches at least one of the
  175. bits contained in this mask will be tested. However, if this parameter is 0, then
  176. all nodes are checked.
  177. feature is disabled.
  178. Please note that the default node id of -1 will match with
  179. every bitmask != 0
  180. \param bNoDebugObjects: Doesn't take debug objects into account
  181. when true. These are scene nodes with IsDebugObject() = true.
  182. \return Scene node nearest to the camera, which collides with
  183. the ray and matches the idBitMask, if the mask is not null. If
  184. no scene node is found, 0 is returned. */
  185. virtual ISceneNode* getSceneNodeFromCameraBB(const ICameraSceneNode* camera,
  186. s32 idBitMask=0, bool bNoDebugObjects = false) = 0;
  187. //! Perform a ray/box and ray/triangle collision check on a hierarchy of scene nodes.
  188. /** This checks all scene nodes under the specified one, first by ray/bounding
  189. box, and then by accurate ray/triangle collision, finding the nearest collision,
  190. and the scene node containing it. It returns the node hit, and (via output
  191. parameters) the position of the collision, and the triangle that was hit.
  192. All scene nodes in the hierarchy tree under the specified node are checked. Only
  193. nodes that are visible, with an ID that matches at least one bit in the supplied
  194. bitmask, and which have a triangle selector are considered as candidates for being hit.
  195. You do not have to build a meta triangle selector; the individual triangle selectors
  196. of each candidate scene node are used automatically.
  197. \param ray: Line with which collisions are tested.
  198. \param outCollisionPoint: If a collision is detected, this will contain the
  199. position of the nearest collision.
  200. \param outTriangle: If a collision is detected, this will contain the triangle
  201. with which the ray collided.
  202. \param idBitMask: Only scene nodes with an id which matches at least one of the
  203. bits contained in this mask will be tested. However, if this parameter is 0, then
  204. all nodes are checked.
  205. \param collisionRootNode: the scene node at which to begin checking. Only this
  206. node and its children will be checked. If you want to check the entire scene,
  207. pass 0, and the root scene node will be used (this is the default).
  208. \param noDebugObjects: when true, debug objects are not considered viable targets.
  209. Debug objects are scene nodes with IsDebugObject() = true.
  210. \return Returns the scene node containing the hit triangle nearest to ray.start.
  211. If no collision is detected, then 0 is returned. */
  212. virtual ISceneNode* getSceneNodeAndCollisionPointFromRay(
  213. SCollisionHit& hitResult,
  214. const core::line3df& ray,
  215. s32 idBitMask = 0,
  216. ISceneNode * collisionRootNode = 0,
  217. bool noDebugObjects = false) = 0;
  218. //! Perform a ray/box and ray/triangle collision check on a hierarchy of scene nodes.
  219. /** Works same as other getSceneNodeAndCollisionPointFromRay but returns less information.
  220. (was written before the other getSceneNodeAndCollisionPointFromRay implementation).
  221. \param ray: Line with which collisions are tested.
  222. \param outCollisionPoint: If a collision is detected, this will contain the
  223. position of the nearest collision.
  224. \param outTriangle: If a collision is detected, this will contain the triangle
  225. with which the ray collided.
  226. \param idBitMask: Only scene nodes with an id which matches at least one of the
  227. bits contained in this mask will be tested. However, if this parameter is 0, then
  228. all nodes are checked.
  229. \param collisionRootNode: the scene node at which to begin checking. Only this
  230. node and its children will be checked. If you want to check the entire scene,
  231. pass 0, and the root scene node will be used (this is the default).
  232. \param noDebugObjects: when true, debug objects are not considered viable targets.
  233. Debug objects are scene nodes with IsDebugObject() = true.
  234. \return Returns the scene node containing the hit triangle nearest to ray.start.
  235. If no collision is detected, then 0 is returned. */
  236. virtual ISceneNode* getSceneNodeAndCollisionPointFromRay(
  237. const core::line3df& ray,
  238. core::vector3df& outCollisionPoint,
  239. core::triangle3df& outTriangle,
  240. s32 idBitMask = 0,
  241. ISceneNode * collisionRootNode = 0,
  242. bool noDebugObjects = false)
  243. {
  244. SCollisionHit hitResult;
  245. ISceneNode* node = getSceneNodeAndCollisionPointFromRay(hitResult, ray, idBitMask, collisionRootNode, noDebugObjects);
  246. if ( node )
  247. {
  248. outCollisionPoint = hitResult.Intersection;
  249. outTriangle = hitResult.Triangle;
  250. }
  251. return node;
  252. }
  253. };
  254. } // end namespace scene
  255. } // end namespace irr
  256. #endif