Bug 456: Avoid hurting Tux in a ravine.
[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     left(),
35     right(),
36     top(),
37     bottom(),
38     ground_movement(),
39     hit()
40   {
41     float infinity = (std::numeric_limits<float>::has_infinity ? 
42                       std::numeric_limits<float>::infinity() : 
43                       std::numeric_limits<float>::max());
44     left = -infinity;
45     right = infinity;
46     top = -infinity;
47     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       left   > -infinity || 
57       right  <  infinity || 
58       top    > -infinity || 
59       bottom <  infinity;
60   }
61
62 public:
63   float left;
64   float right;
65   float top;
66   float bottom;
67
68   void max_left  (float left2  ) { left   = std::max(left  , left2  ); }
69   void min_right (float right2 ) { right  = std::min(right , right2 ); }
70   void max_top   (float top2   ) { top    = std::max(top   , top2   ); }
71   void min_bottom(float bottom2) { bottom = std::min(bottom, bottom2); }
72
73   Vector ground_movement;
74   CollisionHit hit;
75 };
76
77 /** checks if 2 rectangle intersect each other */
78 bool intersects(const Rectf& r1, const Rectf& r2);
79
80 /** does collision detection between a rectangle and an axis aligned triangle
81  * Returns true in case of a collision and fills in the hit structure then.
82  */
83 bool rectangle_aatriangle(Constraints* constraints, const Rectf& rect,
84                           const AATriangle& triangle, const Vector& addl_ground_movement = Vector(0,0));
85
86 void set_rectangle_rectangle_constraints(Constraints* constraints,
87                                          const Rectf& r1, const Rectf& r2, const Vector& addl_ground_movement = Vector(0,0));
88
89 } // namespace collision
90
91 #endif
92
93 /* EOF */