Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool CheckCollision(Object &o1, Object &o2, sf::Vector2f &MTD)
- {
- sf::Shape Shape1,Shape2;
- // Test shapes
- Shape1 = sf::Shape::Rectangle(0,0,100,100,sf::Color::White);
- Shape2 = sf::Shape::Rectangle(-110,0,100,50,sf::Color::White);
- // Achsen-vector
- std::vector<sf::Vector2f> Axis;
- // Alle Achsen abspeichern
- for(int i = 0; i < Shape1.GetPointsCount(); i++)
- {
- Axis.insert(Axis.begin(),perproduct(Shape1.GetPointPosition(i)+o1.GetPosition()-Shape1.GetPointPosition(i + 1 == Shape1.GetPointsCount() ? 0 : i+1)+o1.GetPosition()));
- }
- for(int i = 0; i < Shape2.GetPointsCount(); i++)
- {
- Axis.insert(Axis.begin(),perproduct(Shape2.GetPointPosition(i)+o2.GetPosition()-Shape2.GetPointPosition(i + 1 == Shape2.GetPointsCount() ? 0 : i+1)+o2.GetPosition()));
- }
- // Jede Achse durchgehen
- double overlap = 999999999;
- for(int i = 0; i < Axis.size(); i++)
- {
- // Punkte auf Achse projizieren und minimal und maximal Wert herausfinden
- double min1 = Dot(Axis[i],Shape1.GetPointPosition(0)+o1.GetPosition());
- double max1 = min1;
- for(int a = 0; a < Shape1.GetPointsCount(); a++)
- {
- double p = Dot(Axis[i],Shape1.GetPointPosition(a)+o1.GetPosition());
- if( p < min1 )
- {
- min1 = p;
- }
- else if( p > max1 )
- {
- max1 = p;
- }
- }
- double min2 = Dot(Axis[i],Shape2.GetPointPosition(0)+o2.GetPosition());
- double max2 = min2;
- for(int a = 0; a < Shape2.GetPointsCount(); a++)
- {
- double p = Dot(Axis[i],Shape2.GetPointPosition(a)+o2.GetPosition());
- if( p < min2 )
- {
- min2 = p;
- }
- else if( p > max2 )
- {
- max2 = p;
- }
- }
- // Schauen auf keine Überlappung
- if((min1 >= max2 && max1 >= max2) || ( min1 <= min2 && max1 <= min2))
- {
- return false;
- }
- else if(min1 <= max2 && min1 >= min2)
- {
- double o = max2 - min1;
- if(o < overlap)
- {
- overlap = o;
- MTD = Axis[i];
- }
- }
- else if(max1 >= min2 && max1 <= max2)
- {
- double o = max1 - min2;
- if(o < overlap)
- {
- overlap = o;
- MTD = Axis[i];
- }
- }
- }
- // Kollision!
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement