Advertisement
Guest User

Untitled

a guest
Jul 13th, 2011
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. bool CheckCollision(Object &o1, Object &o2, sf::Vector2f &MTD)
  2. {
  3.     sf::Shape Shape1,Shape2;
  4.     // Test shapes
  5.     Shape1 = sf::Shape::Rectangle(0,0,100,100,sf::Color::White);
  6.     Shape2 = sf::Shape::Rectangle(-110,0,100,50,sf::Color::White);
  7.  
  8.     // Achsen-vector
  9.     std::vector<sf::Vector2f> Axis;
  10.  
  11.     // Alle Achsen abspeichern
  12.     for(int i = 0; i < Shape1.GetPointsCount(); i++)
  13.     {
  14.         Axis.insert(Axis.begin(),perproduct(Shape1.GetPointPosition(i)+o1.GetPosition()-Shape1.GetPointPosition(i + 1 == Shape1.GetPointsCount() ? 0 : i+1)+o1.GetPosition()));
  15.     }
  16.     for(int i = 0; i < Shape2.GetPointsCount(); i++)
  17.     {  
  18.         Axis.insert(Axis.begin(),perproduct(Shape2.GetPointPosition(i)+o2.GetPosition()-Shape2.GetPointPosition(i + 1 == Shape2.GetPointsCount() ? 0 : i+1)+o2.GetPosition()));
  19.     }
  20.  
  21.     // Jede Achse durchgehen
  22.     double overlap = 999999999;
  23.     for(int i = 0; i < Axis.size(); i++)
  24.     {
  25.         // Punkte auf Achse projizieren und minimal und maximal Wert herausfinden
  26.         double min1 = Dot(Axis[i],Shape1.GetPointPosition(0)+o1.GetPosition());
  27.         double max1 = min1;
  28.         for(int a = 0; a < Shape1.GetPointsCount(); a++)
  29.         {
  30.             double p = Dot(Axis[i],Shape1.GetPointPosition(a)+o1.GetPosition());
  31.             if( p < min1 )
  32.             {
  33.                 min1 = p;
  34.             }
  35.             else if( p > max1 )
  36.             {
  37.                 max1 = p;
  38.             }
  39.         }
  40.         double min2 = Dot(Axis[i],Shape2.GetPointPosition(0)+o2.GetPosition());
  41.         double max2 = min2;
  42.         for(int a = 0; a < Shape2.GetPointsCount(); a++)
  43.         {
  44.             double p = Dot(Axis[i],Shape2.GetPointPosition(a)+o2.GetPosition());
  45.             if( p < min2 )
  46.             {
  47.                 min2 = p;
  48.             }
  49.             else if( p > max2 )
  50.             {
  51.                 max2 = p;
  52.             }
  53.         }
  54.  
  55.         // Schauen auf keine Überlappung
  56.         if((min1 >= max2 && max1 >= max2) || ( min1 <= min2 && max1 <= min2))
  57.         {
  58.             return false;
  59.         }
  60.         else if(min1 <= max2 && min1 >= min2)
  61.         {
  62.             double o = max2 - min1;
  63.             if(o < overlap)
  64.             {
  65.                 overlap = o;
  66.                 MTD = Axis[i];
  67.             }
  68.         }
  69.         else if(max1 >= min2 && max1 <= max2)
  70.         {
  71.             double o = max1 - min2;
  72.             if(o < overlap)
  73.             {
  74.                 overlap = o;
  75.                 MTD = Axis[i];
  76.             }
  77.         }
  78.     }
  79.  
  80.     // Kollision!
  81.     return true;
  82.    
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement