RayCast.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Physics/Collision/BackFaceMode.h>
  5. JPH_NAMESPACE_BEGIN
  6. /// Structure that holds a single ray cast
  7. template <class Vec, class Mat, class RayCastType>
  8. struct RayCastT
  9. {
  10. JPH_OVERRIDE_NEW_DELETE
  11. /// Constructors
  12. RayCastT(typename Vec::ArgType inOrigin, Vec3Arg inDirection) : mOrigin(inOrigin), mDirection(inDirection) { }
  13. RayCastT(const RayCastT<Vec, Mat, RayCastType> &) = default;
  14. /// Transform this ray using inTransform
  15. RayCastType Transformed(typename Mat::ArgType inTransform) const
  16. {
  17. Vec ray_origin = inTransform * mOrigin;
  18. Vec3 ray_direction(inTransform * (mOrigin + mDirection) - ray_origin);
  19. return { ray_origin, ray_direction };
  20. }
  21. /// Get point with fraction inFraction on ray (0 = start of ray, 1 = end of ray)
  22. inline Vec GetPointOnRay(float inFraction) const
  23. {
  24. return mOrigin + inFraction * mDirection;
  25. }
  26. Vec mOrigin; ///< Origin of the ray
  27. Vec3 mDirection; ///< Direction and length of the ray (anything beyond this length will not be reported as a hit)
  28. };
  29. struct RayCast : public RayCastT<Vec3, Mat44, RayCast>
  30. {
  31. using RayCastT<Vec3, Mat44, RayCast>::RayCastT;
  32. };
  33. struct RRayCast : public RayCastT<RVec3, RMat44, RRayCast>
  34. {
  35. using RayCastT<RVec3, RMat44, RRayCast>::RayCastT;
  36. /// Convert from RayCast, converts single to double precision
  37. explicit RRayCast(const RayCast &inRay) :
  38. RRayCast(RVec3(inRay.mOrigin), inRay.mDirection)
  39. {
  40. }
  41. /// Convert to RayCast, which implies casting from double precision to single precision
  42. explicit operator RayCast() const
  43. {
  44. return RayCast(Vec3(mOrigin), mDirection);
  45. }
  46. };
  47. /// Settings to be passed with a ray cast
  48. class RayCastSettings
  49. {
  50. public:
  51. JPH_OVERRIDE_NEW_DELETE
  52. /// How backfacing triangles should be treated
  53. EBackFaceMode mBackFaceMode = EBackFaceMode::IgnoreBackFaces;
  54. /// If convex shapes should be treated as solid. When true, a ray starting inside a convex shape will generate a hit at fraction 0.
  55. bool mTreatConvexAsSolid = true;
  56. };
  57. JPH_NAMESPACE_END