collision.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #ifndef HEADER_SUPERTUX_COLLISION_COLLISION_HPP
  17. #define HEADER_SUPERTUX_COLLISION_COLLISION_HPP
  18. #include <limits>
  19. #include <algorithm>
  20. #include "collision/collision_hit.hpp"
  21. #include "math/fwd.hpp"
  22. class Rectf;
  23. class AATriangle;
  24. namespace collision {
  25. class Constraints final
  26. {
  27. public:
  28. Constraints() :
  29. hit(),
  30. position_left(),
  31. position_right(),
  32. position_top(),
  33. position_bottom()
  34. {
  35. float infinity = (std::numeric_limits<float>::has_infinity ?
  36. std::numeric_limits<float>::infinity() :
  37. std::numeric_limits<float>::max());
  38. position_left = -infinity;
  39. position_right = infinity;
  40. position_top = -infinity;
  41. position_bottom = infinity;
  42. }
  43. bool has_constraints() const
  44. {
  45. float infinity = (std::numeric_limits<float>::has_infinity ?
  46. std::numeric_limits<float>::infinity() :
  47. std::numeric_limits<float>::max());
  48. return
  49. position_left > -infinity ||
  50. position_right < infinity ||
  51. position_top > -infinity ||
  52. position_bottom < infinity;
  53. }
  54. public:
  55. void constrain_left (float position)
  56. {
  57. position_left = std::max (position_left, position);
  58. }
  59. void constrain_right (float position)
  60. {
  61. position_right = std::min (position_right, position);
  62. }
  63. void constrain_top (float position)
  64. {
  65. position_top = std::max (position_top, position);
  66. }
  67. void constrain_bottom (float position)
  68. {
  69. position_bottom = std::min (position_bottom, position);
  70. }
  71. void merge_constraints (const Constraints& other);
  72. float get_position_left () const { return position_left; }
  73. float get_position_right () const { return position_right; }
  74. float get_position_top () const { return position_top; }
  75. float get_position_bottom () const { return position_bottom; }
  76. float get_height () const { return (position_bottom - position_top); }
  77. float get_width () const { return (position_right - position_left); }
  78. float get_x_midpoint () const { return (.5f * (position_left + position_right)); }
  79. CollisionHit hit;
  80. private:
  81. float position_left;
  82. float position_right;
  83. float position_top;
  84. float position_bottom;
  85. };
  86. /** does collision detection between a rectangle and an axis aligned triangle
  87. * Returns true in case of a collision and fills in the hit structure then.
  88. */
  89. bool rectangle_aatriangle(Constraints* constraints, const Rectf& rect,
  90. const AATriangle& triangle);
  91. bool rectangle_aatriangle(Constraints* constraints, const Rectf& rect,
  92. const AATriangle& triangle,
  93. bool& hits_rectangle_bottom);
  94. void set_rectangle_rectangle_constraints(Constraints* constraints, const Rectf& r1, const Rectf& r2);
  95. bool line_intersects_line(const Vector& line1_start, const Vector& line1_end, const Vector& line2_start, const Vector& line2_end);
  96. bool intersects_line(const Rectf& r, const Vector& line_start, const Vector& line_end);
  97. } // namespace collision
  98. #endif
  99. /* EOF */