raycast.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "log.h"
  24. #include "physics_types.h"
  25. #include "PxQueryFiltering.h"
  26. #include "PxScene.h"
  27. #include "PxVec3.h"
  28. #include "event_stream.h"
  29. using physx::PxQueryFilterData;
  30. using physx::PxQueryFlag;
  31. using physx::PxHitFlag;
  32. using physx::PxHitFlags;
  33. using physx::PxRaycastHit;
  34. using physx::PxRaycastBuffer;
  35. using physx::PxScene;
  36. using physx::PxVec3;
  37. namespace crown
  38. {
  39. struct Vector3;
  40. struct Actor;
  41. ///
  42. /// @ingroup Physics
  43. struct RaycastHit
  44. {
  45. Vector3 position;
  46. float distance;
  47. Vector3 normal;
  48. Actor* actor;
  49. };
  50. ///
  51. /// @ingroup Physics
  52. struct Raycast
  53. {
  54. /// Constructor
  55. Raycast(PxScene* scene, CollisionMode::Enum mode, CollisionType::Enum type);
  56. /// Performs a raycast against objects in the scene. The ray is casted from position @a from, has direction @a dir and is long @a length
  57. /// If any actor is hit along the ray, @a EventStream is filled according to @a mode previously specified and callback will be called for processing.
  58. /// @a CollisionMode::ANY: the callback is called with just true or false depending on whether the ray hit anything or not.
  59. /// @a CollisionMode::CLOSEST: the first argument will tell if there was a hit or not, as before.
  60. /// If there was a hit, the callback will also be called with the position of the hit, the distance from the origin, the normal of the surface that
  61. /// was hit and the actor that was hit.
  62. /// @a CollisionMode::ALL: as @a CollisionMode::CLOSEST, with more tuples
  63. void cast(const Vector3& from, const Vector3& dir, const float length, Array<RaycastHit>& hits);
  64. CollisionMode::Enum mode() const;
  65. CollisionType::Enum type() const;
  66. private:
  67. PxScene* m_scene;
  68. PxRaycastHit m_hits[CE_MAX_RAY_INTERSECTIONS];
  69. PxRaycastBuffer m_buffer;
  70. PxQueryFilterData m_fd;
  71. CollisionMode::Enum m_mode;
  72. CollisionType::Enum m_type;
  73. };
  74. } // namespace crown