33 #include "qparticle.h"
34 #include "json/json.hpp"
36 #include "qmath_utils.h"
38 using json =nlohmann::json;
50 enum CollisionBehaviors{
56 vector<QParticle*> particles=vector<QParticle*>();
59 QVector position=QVector::Zero();
60 QVector globalPosition=QVector::Zero();
62 float globalRotation=0.0f;
63 vector<QSpring*> springs=vector<QSpring*>();
64 vector <QParticle*> polygon=vector<QParticle*>();
65 vector<vector<QParticle*>> subConvexPolygons=vector<vector<QParticle*>>();
66 float circumference=0.0f;
67 QBody *ownerBody=
nullptr;
68 CollisionBehaviors collisionBehavior=CollisionBehaviors::CIRCLES;
69 vector<vector<int>> UVMaps=vector<vector<int>>();
71 bool collisionBehaviorNeedsUpdate=
false;
74 void UpdateCollisionBehavior();
77 vector<float> lastPolygonCornerAngles;
78 float minAngleConstraintOfPolygon=M_PI*0.3;
81 void UpdateSubConvexPolygons();
82 void ApplyAngleConstraintsToPolygon();
83 bool CheckIsPolygonConcave(vector<QParticle*> polygonParticles);
85 static bool CheckIsReflex(
int indexA,
int indexB,
int indexC, vector<QParticle*> polygonParticles);
86 static void TriangulatePolygon(vector<QParticle*> &polygonParticles,vector<vector<int>> &triangles);
87 static void DecompositePolygon(vector<QParticle*> &polygonParticles,vector<vector<QParticle*>> &polygons);
88 bool subConvexPolygonsNeedsUpdate=
false;
151 return globalPosition;
159 return globalRotation;
164 for(
size_t n=0;n<GetSubConvexPolygonCount();n++){
166 res+=GetPolygonArea(GetSubConvexPolygonAt(n),
true);
168 for(
auto particle:particles){
169 if(particle->GetRadius()>0.5f){
170 res+=particle->GetRadius()*particle->GetRadius();
178 for(
size_t n=0;n<GetSubConvexPolygonCount();n++){
179 res+=GetPolygonArea(GetSubConvexPolygonAt(n),
true);
187 for(
size_t n=0;n<GetSubConvexPolygonCount();n++){
188 res+=GetPolygonArea(GetSubConvexPolygonAt(n));
190 for(
auto particle:particles){
191 if(particle->GetRadius()>0.5f){
192 res+=particle->GetRadius()*particle->GetRadius();
200 for(
size_t n=0;n<GetSubConvexPolygonCount();n++){
201 res+=GetPolygonArea(GetSubConvexPolygonAt(n));
210 for(
size_t n=0;n<GetSubConvexPolygonCount();n++){
211 auto polygon=GetSubConvexPolygonAt(n);
212 for(
int i=0;i<polygon.size();i++){
214 QParticle *np=polygon[(i+1)%polygon.size()];
234 if(collisionBehaviorNeedsUpdate){
235 UpdateCollisionBehavior();
236 collisionBehaviorNeedsUpdate=
false;
238 return collisionBehavior;
256 globalPosition=value;
284 QMesh * RemoveParticleAt(
int index);
292 int GetParticleCount();
303 for(
int i=0;i<particles.size();i++){
304 if(particles[i]==particle){
320 QMesh * SetPolygon(vector<QParticle *> polygonParticles);
328 QMesh * AddParticleToPolygon(
QParticle * particle,
int position=-1);
344 QMesh * RemoveParticleFromPolygonAt(
int index);
350 QMesh * RemovePolygon();
354 int GetPolygonParticleCount();
361 QParticle *GetParticleFromPolygon(
int index);
369 return minAngleConstraintOfPolygon;
378 minAngleConstraintOfPolygon=radian;
403 if (subConvexPolygonsNeedsUpdate==
true){
404 UpdateSubConvexPolygons();
405 subConvexPolygonsNeedsUpdate=
false;
407 return subConvexPolygons.size();
413 if (subConvexPolygonsNeedsUpdate==
true){
414 UpdateSubConvexPolygons();
415 subConvexPolygonsNeedsUpdate=
false;
417 return subConvexPolygons[index];
437 QMesh *RemoveSpringAt(
int index);
446 return springs.size();
452 return springs[index];
459 for(
int i=0;i<springs.size();i++)
460 if(springs[i]==spring)
472 return UVMaps.size();
479 return UVMaps[index];
486 QMesh * AddUVMap(vector<int> map);
493 QMesh * RemoveUVMapAt(
int index);
499 QMesh * ClearUVMaps();
508 QMesh * RemoveMatchingUVMaps(
int particleIndex);
523 static QMesh * CreateWithCircle(
float radius,
QVector centerPosition=QVector::Zero());
535 static QMesh * CreateWithPolygon(
float radius,
int sideCount,
QVector centerPosition=QVector::Zero(),
int polarGrid=-1,
bool enableSprings=
true,
bool enablePolygons=
true,
float particleRadius=0.5f);
547 static QMesh * CreateWithRect(
QVector size,
QVector centerPosition=QVector::Zero(),
QVector grid=QVector::Zero(),
bool enableSprings=
true,
bool enablePolygons=
true,
float particleRadius=0.5f);
555 static QMesh * CreateWithMeshData(
QMesh::MeshData &data,
bool enableSprings=
true,
bool enablePolygons=
true);
561 static vector<QMesh::MeshData> GetMeshDatasFromFile(
string filePath);
568 static vector<QMesh::MeshData> GetMeshDatasFromJsonData(std::string &jsonBasedData);
579 static float GetPolygonArea(vector<QParticle*> &polygonPoints,
bool withLocalPositions=
false);
589 static bool CheckCollisionBehaviors(
QMesh *meshA,
QMesh * meshB,CollisionBehaviors firstBehavior,CollisionBehaviors secondBehavior );
598 static MeshData GenerateRectangleMeshData(
QVector size,
QVector centerPosition=QVector::Zero(),
QVector grid=QVector::Zero(),
float particleRadius=0.5f);
608 static MeshData GeneratePolygonMeshData(
float radius,
int sideCount,
QVector centerPosition=QVector::Zero(),
int polarGrid=-1,
float particleRadius=0.5f);
614 static pair<QVector, float> GetAveragePositionAndRotation(vector<QParticle*> particleCollection);
623 static vector<QVector> GetMatchingParticlePositions(vector<QParticle*> particleCollection,
QVector targetPosition,
float targetRotation);
QBody objects are the base class for all types of bodies. Any class derived from QBody shares these m...
Definition: qbody.h:43
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
QVector GetPosition()
Definition: qparticle.h:71
QRaycast objects send a ray into the world and return collision results with body objects....
Definition: qraycast.h:38
QRigidBody is a type of body that is simulated with the dynamics of Rigid body. A rigid body is a typ...
Definition: qrigidbody.h:40
QSoftBody is a body type that defines deformable, soft objects in the physics world....
Definition: qsoftbody.h:35
You can apply distance constraints between 2 particles using the QSpring. The physics engine uses QSp...
Definition: qspring.h:39
A QWorld object is required to create a physics simulation. The QWorld class manages the entire physi...
Definition: qworld.h:51
vector< QVector > particlePositions
Definition: qmesh.h:95
vector< pair< int, int > > springList
Definition: qmesh.h:103
vector< bool > particleInternalValues
Definition: qmesh.h:99
vector< int > polygon
Definition: qmesh.h:114
vector< vector< int > > UVMaps
Definition: qmesh.h:122
vector< float > particleRadValues
Definition: qmesh.h:97
vector< pair< int, int > > internalSpringList
Definition: qmesh.h:108
Every QBody object requires meshes. In other traditional physics engines, the term 'shape' is used in...
Definition: qmesh.h:48
vector< QParticle * > & GetSubConvexPolygonAt(int index)
Definition: qmesh.h:412
QVector GetGlobalPosition()
Definition: qmesh.h:150
QSpring * GetSpringAt(int index)
Definition: qmesh.h:451
int GetSpringIndex(QSpring *spring)
Definition: qmesh.h:458
QVector GetPosition()
Definition: qmesh.h:146
float GetRotation()
Definition: qmesh.h:154
QMesh * SetGlobalPosition(QVector value)
Definition: qmesh.h:255
float GetGlobalRotation()
Definition: qmesh.h:158
int GetUVMapCount()
Definition: qmesh.h:471
float GetMinAngleConstraintOfPolygon()
Definition: qmesh.h:368
QMesh * AddClosedPolygon(vector< QParticle * > polygon)
float GetArea()
Definition: qmesh.h:185
int GetParticleIndex(QParticle *particle)
Definition: qmesh.h:302
vector< int > GetUVMapAt(int index)
Definition: qmesh.h:478
QBody * GetOwnerBody()
Definition: qmesh.h:227
float GetInitialPolygonsArea()
Definition: qmesh.h:176
QMesh * SetRotation(float value)
Definition: qmesh.h:263
int GetSpringCount()
Definition: qmesh.h:445
int GetSubConvexPolygonCount()
Definition: qmesh.h:402
QMesh * RemoveClosedPolygonAt(int index)
float GetCircumference()
Definition: qmesh.h:208
float GetInitialArea()
Definition: qmesh.h:162
QMesh * SetPosition(QVector value)
Definition: qmesh.h:247
float GetPolygonsArea()
Definition: qmesh.h:198
QMesh * SetMinAngleConstraintOfPolygon(float radian)
Definition: qmesh.h:377
CollisionBehaviors GetCollisionBehavior()
Definition: qmesh.h:233