Quark Physics  1.0
2D Rigid and Soft Body Physics Engine
All Classes Functions Variables Enumerations Pages
qparticle.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 QPARTICLE_H
29 #define QPARTICLE_H
30 #include "qvector.h"
31 #include <vector>
32 #include <unordered_set>
33 #include "qaabb.h"
34 
35 class QBody;
36 class QMesh;
39 class QParticle
40 {
41  float r=0.5f;
42 
43  QVector globalPosition;
44  QVector prevGlobalPosition;
45 
46  QVector position;
47 
48  float mass=1.0f;
49 
50  QMesh *ownerMesh=nullptr;
51 
52  bool isInternal=false;
53 
54  QVector force=QVector::Zero();
55 
56  std::vector<QVector> accumulatedForces;
57 
58  bool enabled=true;
59 
60  bool lazy=false;
61 
62  QAABB aabb;
63 
64  bool aabbNeedsUpdate=true;
65 
66  void ClearOneTimeCollisions();
67 
68 protected:
69  std::unordered_set<QParticle*> springConnectedParticles;
70 
71  unordered_set<QBody*> oneTimeCollidedBodies;
72  unordered_set<QBody*> previousCollidedBodies;
73 
74  void ResetOneTimeCollisions();
75 
76  void UpdateAABB();
77 
78  //For Gravity-Free Feature of QArea Bodies
79  bool ignoreGravity=false;
80 
81 
82 
83 
84 
85 public:
86  QParticle();
87  QParticle(float posX,float posY,float radius=0.5f);
88  QParticle(QVector pos,float radius=0.5f);
89 
90 
91 
92  //Get Methods
95  return globalPosition;
96  }
99  return prevGlobalPosition;
100  }
103  return position;
104  }
106  float GetMass(){
107  return mass;
108  }
113  return ownerMesh;
114  }
116  float GetRadius(){
117  return r;
118  }
121  return isInternal;
122  }
125  return force;
126  }
130  bool GetEnabled(){
131  return enabled;
132  }
137  bool GetIsLazy(){
138  return lazy;
139  }
145  if(aabbNeedsUpdate==true){
146  UpdateAABB();
147  aabbNeedsUpdate=false;
148  }
149  return aabb;
150  }
151 
152  //Set Methods
177  QParticle *SetPosition(QVector value);
182  QParticle *AddPosition(QVector value);
183 
188  QParticle *SetMass(float value);
193  QParticle *SetOwnerMesh(QMesh *mesh);
198  QParticle *SetRadius(float radius);
203  QParticle *SetIsInternal(bool value);
204 
211  QParticle *SetEnabled(bool value);
212 
219  QParticle *SetIsLazy(bool value);
220 
221  //
225  QParticle *ApplyForce(QVector value);
230  QParticle *SetForce(QVector value);
235  QParticle *AddForce(QVector value);
236 
237 
238  //Accumulated Forces
248 
249  //Spring Connected Particles
255  bool IsConnectedWithSpring(QParticle *particle);
256 
257 
258  //Static Methods
265  static void ApplyForceToParticleSegment(QParticle *pA,QParticle *pB,QVector force,QVector fromPosition);
266 
270  bool manualDeletion=false;
271 
272 
273  static bool SortParticlesHorizontal(QParticle* pA,QParticle* pB){
274  if(pA->GetAABB().GetMin().x==pB->GetAABB().GetMin().x){
275  return pA->GetAABB().GetMax().y>pB->GetAABB().GetMax().y;
276  }
277  return pA->GetAABB().GetMin().x<pB->GetAABB().GetMin().x;
278  }
279 
280 
281  friend class QMesh;
282  friend class QBody;
283  friend class QManifold;
284  friend class QAreaBody;
285  friend class QCollision;
286 
287 
288 };
289 
290 #endif // QPARTICLE_H
Definition: qaabb.h:38
QAreaBody objects are objects that don't respond to collisions or receive any response from them,...
Definition: qareabody.h:38
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
QManifold retrieves collision data from collision tests between two QBody objects using QCollision me...
Definition: qmanifold.h:36
QParticle objects form the network structures of QMesh objects defined for all body object types....
Definition: qparticle.h:40
bool manualDeletion
Definition: qparticle.h:270
QParticle * AddGlobalPosition(QVector value)
Definition: qparticle.cpp:91
static void ApplyForceToParticleSegment(QParticle *pA, QParticle *pB, QVector force, QVector fromPosition)
Definition: qparticle.cpp:215
QVector GetForce()
Definition: qparticle.h:124
QParticle * SetIsLazy(bool value)
Definition: qparticle.cpp:157
QAABB GetAABB()
Definition: qparticle.h:144
QParticle * SetForce(QVector value)
Definition: qparticle.cpp:171
QParticle * AddAccumulatedForce(QVector value)
Definition: qparticle.cpp:184
QParticle * SetEnabled(bool value)
Definition: qparticle.cpp:151
bool GetIsLazy()
Definition: qparticle.h:137
QParticle * SetOwnerMesh(QMesh *mesh)
Definition: qparticle.cpp:129
bool GetEnabled()
Definition: qparticle.h:130
float GetMass()
Definition: qparticle.h:106
QParticle * SetMass(float value)
Definition: qparticle.cpp:124
QParticle * ApplyForce(QVector value)
Definition: qparticle.cpp:163
QVector GetGlobalPosition()
Definition: qparticle.h:94
QParticle * ApplyAccumulatedForces()
Definition: qparticle.cpp:196
QVector GetPreviousGlobalPosition()
Definition: qparticle.h:98
bool GetIsInternal()
Definition: qparticle.h:120
QVector GetPosition()
Definition: qparticle.h:102
QParticle * ClearAccumulatedForces()
Definition: qparticle.cpp:190
float GetRadius()
Definition: qparticle.h:116
QParticle * SetGlobalPosition(QVector value)
Definition: qparticle.cpp:77
QParticle * AddForce(QVector value)
Definition: qparticle.cpp:180
QParticle * SetPosition(QVector value)
Definition: qparticle.cpp:103
bool IsConnectedWithSpring(QParticle *particle)
Definition: qparticle.cpp:210
QParticle * SetPreviousGlobalPosition(QVector value)
Definition: qparticle.cpp:94
QParticle * SetRadius(float radius)
Definition: qparticle.cpp:134
QParticle * SetIsInternal(bool value)
Definition: qparticle.cpp:145
QParticle * AddPreviousGlobalPosition(QVector value)
Definition: qparticle.cpp:99
QParticle * AddPosition(QVector value)
Definition: qparticle.cpp:120
QMesh * GetOwnerMesh()
Definition: qparticle.h:112
Every QBody object requires meshes. In other traditional physics engines, the term 'shape' is used in...
Definition: qmesh.h:49
Definition: qvector.h:44