supertux
collision.hpp
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 
17 #ifndef HEADER_SUPERTUX_COLLISION_COLLISION_HPP
18 #define HEADER_SUPERTUX_COLLISION_COLLISION_HPP
19 
20 #include <limits>
21 #include <algorithm>
22 
23 #include "collision/collision_hit.hpp"
24 #include "math/fwd.hpp"
25 
26 class Rectf;
27 class AATriangle;
28 
29 namespace collision {
30 
31 class Constraints final
32 {
33 public:
34  Constraints() :
35  hit(),
36  position_left(),
37  position_right(),
38  position_top(),
39  position_bottom()
40  {
41  float infinity = (std::numeric_limits<float>::has_infinity ?
42  std::numeric_limits<float>::infinity() :
43  std::numeric_limits<float>::max());
44  position_left = -infinity;
45  position_right = infinity;
46  position_top = -infinity;
47  position_bottom = infinity;
48  }
49 
50  bool has_constraints() const
51  {
52  float infinity = (std::numeric_limits<float>::has_infinity ?
53  std::numeric_limits<float>::infinity() :
54  std::numeric_limits<float>::max());
55  return
56  position_left > -infinity ||
57  position_right < infinity ||
58  position_top > -infinity ||
59  position_bottom < infinity;
60  }
61 
62 public:
63 
64  void constrain_left (float position)
65  {
66  position_left = std::max (position_left, position);
67  }
68 
69  void constrain_right (float position)
70  {
71  position_right = std::min (position_right, position);
72  }
73 
74  void constrain_top (float position)
75  {
76  position_top = std::max (position_top, position);
77  }
78 
79  void constrain_bottom (float position)
80  {
81  position_bottom = std::min (position_bottom, position);
82  }
83 
84  void merge_constraints (const Constraints& other);
85 
86  float get_position_left () const { return position_left; }
87  float get_position_right () const { return position_right; }
88  float get_position_top () const { return position_top; }
89  float get_position_bottom () const { return position_bottom; }
90 
91  float get_height () const { return (position_bottom - position_top); }
92  float get_width () const { return (position_right - position_left); }
93 
94  float get_x_midpoint () const { return (.5f * (position_left + position_right)); }
95 
96  CollisionHit hit;
97 
98 private:
99  float position_left;
100  float position_right;
101  float position_top;
102  float position_bottom;
103 };
104 
108 bool rectangle_aatriangle(Constraints* constraints, const Rectf& rect,
109  const AATriangle& triangle);
110 
111 bool rectangle_aatriangle(Constraints* constraints, const Rectf& rect,
112  const AATriangle& triangle,
113  bool& hits_rectangle_bottom);
114 
115 void set_rectangle_rectangle_constraints(Constraints* constraints, const Rectf& r1, const Rectf& r2);
116 
117 bool line_intersects_line(const Vector& line1_start, const Vector& line1_end, const Vector& line2_start, const Vector& line2_end);
118 bool intersects_line(const Rectf& r, const Vector& line_start, const Vector& line_end);
119 
120 } // namespace collision
121 
122 #endif
123 
124 /* EOF */
Definition: collision.cpp:24
Definition: rectf.hpp:31
Definition: collision.hpp:31
An axis-aligned triangle (ie.
Definition: aatriangle.hpp:26
This class collects data about a collision.
Definition: collision_hit.hpp:44