Quark Physics  1.0
2D Rigid and Soft Body Physics Engine
qsoftbody.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 QSOFTBODY_H
29 #define QSOFTBODY_H
30 #include "qbody.h"
31 #include <utility>
34 class QSoftBody : public QBody
35 {
36  //#Softbody Properties
37  float rigidity=1.0f;
38  float enableAreaPreserving=false;
39  float areaPreservingRate=0.8f;
40  float areaPreservingRigidity=1.0f;
41  float targetPreservationArea=0.0f;
42 
43  float particleSpesificMass=1.0f;
44  bool enableParticleSpesificMass=false;
45 
46 
47  float circumference=0.0f;
48  bool enableAreaStability=false;
49  bool enablePassivationOfInternalSprings=false;
50 
51 
52  bool enableSelfCollisions=false;
53  float selfCollisionParticleRadius=0.0f;
54 
55  bool enableShapeMatching=false;
56  float shapeMatchingRate=0.4f;
57  bool ApplyShapeMatchingInternals=false;
58  bool enableShapeMatchingFixedTransform=false;
59  QVector shapeMatchingFixedPosition=QVector::Zero();
60  float shapeMatchingFixedRotation=0.0f;
61 
62 
63  //Helper methods
64 
65  float safe_asin(float value){
66  if(value<-1.0f){
67  return asin(-1.0);
68  }else if(value>1.0f){
69  return asin(1.0);
70  }else{
71  return asin(value);
72  }
73  }
74 
75  bool IsPolygonCW(vector<QParticle*> polygon);
76 
77 
78 public:
79  QSoftBody();
80 
81 
82 
83  //Properties Set Methods (it returns this)
88  QSoftBody * SetRigidity(float value){
89  rigidity=value;
90  return this;
91  };
97  areaPreservingRate=value;
98  return this;
99  }
105  areaPreservingRigidity=value;
106  return this;
107  }
113  enableAreaPreserving=value;
114  if(value==true){
115  targetPreservationArea=GetTotalPolygonsInitialArea();
116  }
117  return this;
118  }
119 
125  targetPreservationArea=value;
126  return this;
127  }
133  enableSelfCollisions=value;
134  return this;
135  }
136 
142  selfCollisionParticleRadius=value;
143  return this;
144  }
145 
151  enablePassivationOfInternalSprings=value;
152  return this;
153  }
159  QSoftBody *SetShapeMatchingEnabled(bool value, bool withoutInternals=false){
160  enableShapeMatching=value;
161  ApplyShapeMatchingInternals=!withoutInternals;
162  return this;
163  }
164 
170  shapeMatchingRate=value;
171  return this;
172  }
173 
179  enableShapeMatchingFixedTransform=value;
180  return this;
181  }
182 
188  shapeMatchingFixedPosition=value;
189  return this;
190  }
191 
197  shapeMatchingFixedRotation=value;
198  return this;
199  }
205  particleSpesificMass=value;
206  return this;
207  }
208 
214  enableParticleSpesificMass=value;
215  return this;
216  }
217 
218  //Properties Get Methods
220  float GetMass(){
221  if(enableParticleSpesificMass)
222  return particleSpesificMass;
223  else
224  return mass;
225  }
227  float GetRigidity(){
228  return rigidity;
229  };
232  return areaPreservingRate;
233  };
236  return areaPreservingRigidity;
237  };
240  return enableAreaPreserving;
241  }
244  return targetPreservationArea;
245  }
248  return enableSelfCollisions;
249  }
252  return selfCollisionParticleRadius;
253  }
256  return enablePassivationOfInternalSprings;
257  }
260  return enableShapeMatching;
261  }
262 
263 
266  return shapeMatchingRate;
267  }
268 
271  return enableShapeMatchingFixedTransform;
272  }
273 
276  return shapeMatchingFixedPosition;
277  }
278 
281  return shapeMatchingFixedRotation;
282  }
283 
286  return particleSpesificMass;
287  }
290  return enableParticleSpesificMass;
291  }
292 
293 
294 
295 
296 
297 
298  //
300  virtual void Update();
302  virtual void PostUpdate();
304  void PreserveAreas();
306  void ApplyShapeMatching();
307 
308 
309 };
310 
311 #endif // QSOFTBODY_H
QBody objects are the base class for all types of bodies. Any class derived from QBody shares these m...
Definition: qbody.h:43
float GetTotalPolygonsInitialArea()
Definition: qbody.h:226
QSoftBody is a body type that defines deformable, soft objects in the physics world....
Definition: qsoftbody.h:35
QSoftBody * SetShapeMatchingEnabled(bool value, bool withoutInternals=false)
Definition: qsoftbody.h:159
QVector GetShapeMatchingFixedPosition()
Definition: qsoftbody.h:275
float GetTargetPreservationArea()
Definition: qsoftbody.h:243
float GetAreaPreservingRigidity()
Definition: qsoftbody.h:235
QSoftBody * SetRigidity(float value)
Definition: qsoftbody.h:88
float GetParticleSpesificMass()
Definition: qsoftbody.h:285
QSoftBody * SetAreaPreservingRigidity(float value)
Definition: qsoftbody.h:104
QSoftBody * SetAreaPreservingRate(float value)
Definition: qsoftbody.h:96
QSoftBody * SetTargetPreservationArea(float value)
Definition: qsoftbody.h:124
QSoftBody * SetAreaPreservingEnabled(bool value)
Definition: qsoftbody.h:112
void PreserveAreas()
Definition: qsoftbody.cpp:154
float GetShapeMatchingRate()
Definition: qsoftbody.h:265
bool GetShapeMatchingEnabled()
Definition: qsoftbody.h:259
bool GetSelfCollisionsEnabled()
Definition: qsoftbody.h:247
QSoftBody * SetPassivationOfInternalSpringsEnabled(bool value)
Definition: qsoftbody.h:150
QSoftBody * SetParticleSpesificMassEnabled(bool value)
Definition: qsoftbody.h:213
float GetRigidity()
Definition: qsoftbody.h:227
float GetShapeMatchingFixedRotation()
Definition: qsoftbody.h:280
QSoftBody * SetSelfCollisionsEnabled(bool value)
Definition: qsoftbody.h:132
void ApplyShapeMatching()
Definition: qsoftbody.cpp:228
QSoftBody * SetShapeMatchingFixedRotation(float value)
Definition: qsoftbody.h:196
virtual void Update()
Definition: qsoftbody.cpp:79
float GetMass()
Definition: qsoftbody.h:220
bool GetShapeMatchingFixedTransformEnabled()
Definition: qsoftbody.h:270
bool GetPassivationOfInternalSpringsEnabled()
Definition: qsoftbody.h:255
QSoftBody * SetShapeMatchingFixedTransformEnabled(bool value)
Definition: qsoftbody.h:178
QSoftBody * SetShapeMatchingRate(float value)
Definition: qsoftbody.h:169
virtual void PostUpdate()
Definition: qsoftbody.cpp:148
float GetAreaPreservingRate()
Definition: qsoftbody.h:231
QSoftBody * SetSelfCollisionsSpecifiedRadius(float value)
Definition: qsoftbody.h:141
QSoftBody * SetShapeMatchingFixedPosition(QVector value)
Definition: qsoftbody.h:187
float GetSelfCollisionsSpecifiedRadius()
Definition: qsoftbody.h:251
QSoftBody * SetParticleSpesificMass(float value)
Definition: qsoftbody.h:204
bool GetAreaPreservingEnabled()
Definition: qsoftbody.h:239
bool GetParticleSpesificMassEnabled()
Definition: qsoftbody.h:289
Definition: qvector.h:44