Renamed Rect to Rectf
[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
23 class Vector;
24 class Rectf;
25 class AATriangle;
26
27 namespace collision {
28
29 class Constraints
30 {
31 public:
32   Constraints() :
33     left(),
34     right(),
35     top(),
36     bottom(),
37     ground_movement(),
38     hit()
39   {
40     float infinity = (std::numeric_limits<float>::has_infinity ? 
41                       std::numeric_limits<float>::infinity() : 
42                       std::numeric_limits<float>::max());
43     left = -infinity;
44     right = infinity;
45     top = -infinity;
46     bottom = infinity;
47   }
48
49   bool has_constraints() const 
50   {
51     float infinity = (std::numeric_limits<float>::has_infinity ?
52                       std::numeric_limits<float>::infinity() : 
53                       std::numeric_limits<float>::max());
54     return
55       left   > -infinity || 
56       right  <  infinity || 
57       top    > -infinity || 
58       bottom <  infinity;
59   }
60
61 public:
62   float left;
63   float right;
64   float top;
65   float bottom;
66   Vector ground_movement;
67   CollisionHit hit;
68 };
69
70 /** checks if 2 rectangle intersect each other */
71 bool intersects(const Rectf& r1, const Rectf& r2);
72
73 /** does collision detection between a rectangle and an axis aligned triangle
74  * Returns true in case of a collision and fills in the hit structure then.
75  */
76 bool rectangle_aatriangle(Constraints* constraints, const Rectf& rect,
77                           const AATriangle& triangle, const Vector& addl_ground_movement = Vector(0,0));
78
79 void set_rectangle_rectangle_constraints(Constraints* constraints,
80                                          const Rectf& r1, const Rectf& r2, const Vector& addl_ground_movement = Vector(0,0));
81
82 } // namespace collision
83
84 #endif
85
86 /* EOF */