Quark Physics  1.0
2D Rigid and Soft Body Physics Engine
qcollision.h
1 
2 /************************************************************************************
3  * MIT License
4  *
5  * Copyright (c) 2023 Eray Zesen
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * https://github.com/erayzesen/QuarkPhysics
25  *
26 **************************************************************************************/
27 
28 #ifndef QCOLLISION_H
29 #define QCOLLISION_H
30 #include <iostream>
31 #include <vector>
32 #include "qparticle.h"
33 #include "qobjectpool.h"
34 
35 
36 class QWorld;
37 class QAABB;
38 using namespace std;
39 
40 
44 {
45 public:
46  QCollision(){}
47  ~QCollision();
48 
49 
50 
51 
53  struct Contact{
54  public:
62  float penetration;
64  vector<QParticle*> referenceParticles;
66  bool solved;
67  Contact(QParticle *particle,QVector position,QVector normal,float penetration,vector<QParticle*> referenceParticles ){
68  this->particle=particle;
69  this->position=position;
70  this->normal=normal;
71  this->penetration=penetration;
72  this->referenceParticles=referenceParticles;
73  this->solved=false;
74  }
75 
76  Contact(){
77 
78  }
79 
80  void Configure(QParticle *particle,QVector position,QVector normal,float penetration,vector<QParticle*> referenceParticles){
81  this->particle=particle;
82  this->position=position;
83  this->normal=normal;
84  this->penetration=penetration;
85  this->referenceParticles=referenceParticles;
86  this->solved=false;
87  }
88 
89 
90 
91  };
92 
93  static QObjectPool<QCollision::Contact> contactPool;
94 
95  static QObjectPool<QCollision::Contact> &GetContactPool();
96 
97 
98  struct Project{
99  public:
100  float min;
101  float max;
102  int minIndex;
103  int maxIndex;
104  Project(float projectMin,int minPointIndex,float projectMax,int maxPointIndex){
105  min=projectMin;
106  max=projectMax;
107  minIndex=minPointIndex;
108  maxIndex=maxPointIndex;
109  }
110  float Overlap(const Project other) const{
111  float penetration=0.0f;
112  if(other.min<min){
113  penetration=min-other.max;
114  }else{
115  penetration=other.min-max;
116  }
117  return penetration;
118  }
119 
120  };
121 
122 
123 
124  //Collision Methods
130  static void PolygonAndPolygon(vector<QParticle*> &particlesA,vector<QParticle*> &particlesB,vector<QCollision::Contact*> &contacts);
136  static void CircleAndPolygon(vector<QParticle*> &circleParticles,vector<QParticle*> &polygonParticles,vector<QCollision::Contact*> &contacts);
142  static void CircleAndCircle(vector<QParticle*> &particlesA,vector<QParticle*> &particlesB, QAABB boundingBoxB, vector<QCollision::Contact*> &contacts, float specifiedRadius=0.0f);
150  static void PolylineAndPolygon(vector<QParticle*> &polylineParticles,vector<QParticle*> &polygonParticles,vector<QCollision::Contact*> &contacts);
151 
159  static void CircleAndPolyline(vector<QParticle*> &circleParticles,vector<QParticle*> &polylineParticles,QAABB polylineAABB , vector<QCollision::Contact*> &contacts, bool circlesArePolygon=false);
160 
161 
162  //Geometry Helper Methods
170  static QVector LineIntersectionLine(QVector d1A,QVector d1B, QVector d2A, QVector d2B);
176  static bool PointInPolygon(QVector &point, vector<QParticle*> &polygon );
182  static bool PointInPolygon(QVector &point, vector<QVector> &polygon );
188  static bool PointInPolygon2(QVector point, vector<QParticle*> &polygon );
189 
190 
191 
192 private:
193 
194  //Collision Helper Methods
195  static void ClipContactParticles(QParticle *referenceParticles[], QParticle *incidentParticles[], vector<QCollision::Contact*> &contacts );
196  static Project ProjectToAxis(QVector &normal,vector<QParticle*> &polygon);
197  static pair<int,int> FindNearestSideOfPolygon(const QVector point, vector<QParticle*> polygonParticles,bool checkSideRange=false);
198  static int FindNearestParticleOfPolygon(QParticle * particle, vector<QParticle*> polygonParticles);
199  static int FindExtremeParticleOfAxis(vector<QParticle*> polygonParticles, QVector axisNormal);
200  static bool PointInPolygonWN(const QVector point, vector<QParticle*> polygonParticles);
201 
202 
203 };
204 
205 
206 
207 
208 #endif // QCOLLISION_H
Definition: qaabb.h:34
The QCollision class performs all collision detection operations. The relevant methods return contact...
Definition: qcollision.h:44
QParticle objects form the network structures of QMesh objects defined for all body object types....
Definition: qparticle.h:37
A QWorld object is required to create a physics simulation. The QWorld class manages the entire physi...
Definition: qworld.h:51
Contains all the contact information required to resolve collisions.
Definition: qcollision.h:53
QVector normal
Definition: qcollision.h:60
QVector position
Definition: qcollision.h:56
float penetration
Definition: qcollision.h:62
vector< QParticle * > referenceParticles
Definition: qcollision.h:64
QParticle * particle
Definition: qcollision.h:58
bool solved
Definition: qcollision.h:66
Definition: qcollision.h:98
Definition: qvector.h:44