2
0

Collision.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. #pragma once
  9. #include "Math.h"
  10. #include <vector>
  11. struct LineSegment
  12. {
  13. LineSegment(const Vector3& start, const Vector3& end);
  14. // Get point along segment where 0 <= t <= 1
  15. Vector3 PointOnSegment(float t) const;
  16. // Get minimum distance squared between point and line segment
  17. float MinDistSq(const Vector3& point) const;
  18. // Get MinDistSq between two line segments
  19. static float MinDistSq(const LineSegment& s1, const LineSegment& s2);
  20. Vector3 mStart;
  21. Vector3 mEnd;
  22. };
  23. struct Plane
  24. {
  25. Plane(const Vector3& normal, float d);
  26. // Construct plane from three points
  27. Plane(const Vector3& a, const Vector3& b, const Vector3& c);
  28. // Get the signed distance between the point and the plane
  29. float SignedDist(const Vector3& point) const;
  30. Vector3 mNormal;
  31. float mD;
  32. };
  33. struct Sphere
  34. {
  35. Sphere(const Vector3& center, float radius);
  36. bool Contains(const Vector3& point) const;
  37. Vector3 mCenter;
  38. float mRadius;
  39. };
  40. struct AABB
  41. {
  42. AABB(const Vector3& min, const Vector3& max);
  43. // Update min and max accounting for this point
  44. // (used when loading a model)
  45. void UpdateMinMax(const Vector3& point);
  46. // Rotated by a quaternion
  47. void Rotate(const Quaternion& q);
  48. bool Contains(const Vector3& point) const;
  49. float MinDistSq(const Vector3& point) const;
  50. Vector3 mMin;
  51. Vector3 mMax;
  52. };
  53. struct OBB
  54. {
  55. Vector3 mCenter;
  56. Quaternion mRotation;
  57. Vector3 mExtents;
  58. };
  59. struct Capsule
  60. {
  61. Capsule(const Vector3& start, const Vector3& end, float radius);
  62. // Get point along segment where 0 <= t <= 1
  63. Vector3 PointOnSegment(float t) const;
  64. bool Contains(const Vector3& point) const;
  65. LineSegment mSegment;
  66. float mRadius;
  67. };
  68. struct ConvexPolygon
  69. {
  70. bool Contains(const Vector2& point) const;
  71. // Vertices have a clockwise ordering
  72. std::vector<Vector2> mVertices;
  73. };
  74. // Intersection functions
  75. bool Intersect(const Sphere& a, const Sphere& b);
  76. bool Intersect(const AABB& a, const AABB& b);
  77. bool Intersect(const Capsule& a, const Capsule& b);
  78. bool Intersect(const Sphere& s, const AABB& box);
  79. bool Intersect(const LineSegment& l, const Sphere& s, float& outT);
  80. bool Intersect(const LineSegment& l, const Plane& p, float& outT);
  81. bool Intersect(const LineSegment& l, const AABB& b, float& outT,
  82. Vector3& outNorm);
  83. bool SweptSphere(const Sphere& P0, const Sphere& P1,
  84. const Sphere& Q0, const Sphere& Q1, float& t);