2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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/>.
17 #ifndef HEADER_SUPERTUX_SUPERTUX_COLLISION_HPP
18 #define HEADER_SUPERTUX_SUPERTUX_COLLISION_HPP
20 #include "supertux/collision_hit.hpp"
22 #include <algorithm> /* min/max */
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;
53 speed_left = -infinity;
54 speed_right = infinity;
55 speed_top = -infinity;
56 speed_bottom = infinity;
59 bool has_constraints() const
61 float infinity = (std::numeric_limits<float>::has_infinity ?
62 std::numeric_limits<float>::infinity() :
63 std::numeric_limits<float>::max());
65 position_left > -infinity ||
66 position_right < infinity ||
67 position_top > -infinity ||
68 position_bottom < infinity;
73 void constrain_left (float position, float velocity)
75 position_left = std::max (position_left, position);
76 speed_left = std::max (speed_left, velocity);
79 void constrain_right (float position, float velocity)
81 position_right = std::min (position_right, position);
82 speed_right = std::min (speed_right, velocity);
85 void constrain_top (float position, float velocity)
87 position_top = std::max (position_top, position);
88 speed_top = std::max (speed_top, velocity);
91 void constrain_bottom (float position, float velocity)
93 position_bottom = std::min (position_bottom, position);
94 speed_bottom = std::min (speed_bottom, velocity);
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; }
102 float get_height (void) const { return (position_bottom - position_top); }
103 float get_width (void) const { return (position_right - position_left); }
105 float get_x_midpoint (void) const { return (.5f * (position_left + position_right)); }
107 Vector ground_movement;
112 float position_right;
114 float position_bottom;
122 /** checks if 2 rectangle intersect each other */
123 bool intersects(const Rectf& r1, const Rectf& r2);
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.
128 bool rectangle_aatriangle(Constraints* constraints, const Rectf& rect,
129 const AATriangle& triangle, const Vector& addl_ground_movement = Vector(0,0));
131 void set_rectangle_rectangle_constraints(Constraints* constraints,
132 const Rectf& r1, const Rectf& r2, const Vector& addl_ground_movement = Vector(0,0));
134 } // namespace collision