Fix coverity #29604
[supertux.git] / src / 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_SUPERTUX_COLLISION_HPP
18 #define HEADER_SUPERTUX_SUPERTUX_COLLISION_HPP
19
20 #include "supertux/collision_hit.hpp"
21 #include <limits>
22 #include <algorithm> /* min/max */
23
24 class Vector;
25 class Rectf;
26 class AATriangle;
27
28 namespace collision {
29
30 class Constraints
31 {
32 public:
33   Constraints() :
34     ground_movement(),
35     hit(),
36     position_left(),
37     position_right(),
38     position_top(),
39     position_bottom(),
40     speed_left(),
41     speed_right(),
42     speed_top(),
43     speed_bottom()
44   {
45     float infinity = (std::numeric_limits<float>::has_infinity ?
46                       std::numeric_limits<float>::infinity() :
47                       std::numeric_limits<float>::max());
48     position_left = -infinity;
49     position_right = infinity;
50     position_top = -infinity;
51     position_bottom = infinity;
52
53     speed_left = -infinity;
54     speed_right = infinity;
55     speed_top = -infinity;
56     speed_bottom = infinity;
57   }
58
59   bool has_constraints() const
60   {
61     float infinity = (std::numeric_limits<float>::has_infinity ?
62                       std::numeric_limits<float>::infinity() :
63                       std::numeric_limits<float>::max());
64     return
65       position_left   > -infinity ||
66       position_right  <  infinity ||
67       position_top    > -infinity ||
68       position_bottom <  infinity;
69   }
70
71 public:
72
73   void constrain_left (float position, float velocity)
74   {
75     position_left = std::max (position_left, position);
76     speed_left = std::max (speed_left, velocity);
77   }
78
79   void constrain_right (float position, float velocity)
80   {
81     position_right = std::min (position_right, position);
82     speed_right = std::min (speed_right, velocity);
83   }
84
85   void constrain_top (float position, float velocity)
86   {
87     position_top = std::max (position_top, position);
88     speed_top = std::max (speed_top, velocity);
89   }
90
91   void constrain_bottom (float position, float velocity)
92   {
93     position_bottom = std::min (position_bottom, position);
94     speed_bottom = std::min (speed_bottom, velocity);
95   }
96
97   float get_position_left   (void) const { return position_left;   }
98   float get_position_right  (void) const { return position_right;  }
99   float get_position_top    (void) const { return position_top;    }
100   float get_position_bottom (void) const { return position_bottom; }
101
102   float get_height (void) const { return (position_bottom - position_top); }
103   float get_width  (void) const { return (position_right - position_left); }
104
105   float get_x_midpoint (void) const { return (.5f * (position_left + position_right)); }
106
107   Vector ground_movement;
108   CollisionHit hit;
109
110 private:
111   float position_left;
112   float position_right;
113   float position_top;
114   float position_bottom;
115
116   float speed_left;
117   float speed_right;
118   float speed_top;
119   float speed_bottom;
120 };
121
122 /** checks if 2 rectangle intersect each other */
123 bool intersects(const Rectf& r1, const Rectf& r2);
124
125 /** does collision detection between a rectangle and an axis aligned triangle
126  * Returns true in case of a collision and fills in the hit structure then.
127  */
128 bool rectangle_aatriangle(Constraints* constraints, const Rectf& rect,
129                           const AATriangle& triangle, const Vector& addl_ground_movement = Vector(0,0));
130
131 void set_rectangle_rectangle_constraints(Constraints* constraints,
132                                          const Rectf& r1, const Rectf& r2, const Vector& addl_ground_movement = Vector(0,0));
133
134 } // namespace collision
135
136 #endif
137
138 /* EOF */