src/supertux/collision.hpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #ifndef HEADER_SUPERTUX_SUPERTUX_COLLISION_HPP
00018 #define HEADER_SUPERTUX_SUPERTUX_COLLISION_HPP
00019 
00020 #include "supertux/collision_hit.hpp"
00021 #include <limits>
00022 #include <algorithm> /* min/max */
00023 
00024 class Vector;
00025 class Rectf;
00026 class AATriangle;
00027 
00028 namespace collision {
00029 
00030 class Constraints
00031 {
00032 public:
00033   Constraints() :
00034     ground_movement(),
00035     hit(),
00036     position_left(),
00037     position_right(),
00038     position_top(),
00039     position_bottom(),
00040     speed_left(),
00041     speed_right(),
00042     speed_top(),
00043     speed_bottom()
00044   {
00045     float infinity = (std::numeric_limits<float>::has_infinity ? 
00046                       std::numeric_limits<float>::infinity() : 
00047                       std::numeric_limits<float>::max());
00048     position_left = -infinity;
00049     position_right = infinity;
00050     position_top = -infinity;
00051     position_bottom = infinity;
00052 
00053     speed_left = -infinity;
00054     speed_right = infinity;
00055     speed_top = -infinity;
00056     speed_bottom = infinity;
00057   }
00058 
00059   bool has_constraints() const 
00060   {
00061     float infinity = (std::numeric_limits<float>::has_infinity ?
00062                       std::numeric_limits<float>::infinity() : 
00063                       std::numeric_limits<float>::max());
00064     return
00065       position_left   > -infinity || 
00066       position_right  <  infinity || 
00067       position_top    > -infinity || 
00068       position_bottom <  infinity;
00069   }
00070 
00071 public:
00072 
00073   void constrain_left (float position, float velocity)
00074   {
00075     position_left = std::max (position_left, position);
00076     speed_left = std::max (speed_left, velocity);
00077   }
00078 
00079   void constrain_right (float position, float velocity)
00080   {
00081     position_right = std::min (position_right, position);
00082     speed_right = std::min (speed_right, velocity);
00083   }
00084 
00085   void constrain_top (float position, float velocity)
00086   {
00087     position_top = std::max (position_top, position);
00088     speed_top = std::max (speed_top, velocity);
00089   }
00090 
00091   void constrain_bottom (float position, float velocity)
00092   {
00093     position_bottom = std::min (position_bottom, position);
00094     speed_bottom = std::min (speed_bottom, velocity);
00095   }
00096 
00097   float get_position_left   (void) const { return position_left;   }
00098   float get_position_right  (void) const { return position_right;  }
00099   float get_position_top    (void) const { return position_top;    }
00100   float get_position_bottom (void) const { return position_bottom; }
00101 
00102   float get_height (void) const { return (position_bottom - position_top); }
00103   float get_width  (void) const { return (position_right - position_left); }
00104 
00105   float get_x_midpoint (void) const { return (.5f * (position_left + position_right)); }
00106 
00107   Vector ground_movement;
00108   CollisionHit hit;
00109 
00110 private:
00111   float position_left;
00112   float position_right;
00113   float position_top;
00114   float position_bottom;
00115 
00116   float speed_left;
00117   float speed_right;
00118   float speed_top;
00119   float speed_bottom;
00120 };
00121 
00123 bool intersects(const Rectf& r1, const Rectf& r2);
00124 
00128 bool rectangle_aatriangle(Constraints* constraints, const Rectf& rect,
00129                           const AATriangle& triangle, const Vector& addl_ground_movement = Vector(0,0));
00130 
00131 void set_rectangle_rectangle_constraints(Constraints* constraints,
00132                                          const Rectf& r1, const Rectf& r2, const Vector& addl_ground_movement = Vector(0,0));
00133 
00134 } // namespace collision
00135 
00136 #endif
00137 
00138 /* EOF */

Generated on Mon Apr 14 03:38:25 2014 for SuperTux by  doxygen 1.5.1