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  shapeMatchingFixedPosition=GetPosition();
181  shapeMatchingFixedRotation=GetRotation();
182  return this;
183  }
184 
190  shapeMatchingFixedPosition=value;
191  return this;
192  }
193 
199  shapeMatchingFixedRotation=value;
200  return this;
201  }
207  particleSpesificMass=value;
208  return this;
209  }
210 
216  enableParticleSpesificMass=value;
217  return this;
218  }
219 
220  //Properties Get Methods
222  float GetMass(){
223  if(enableParticleSpesificMass)
224  return particleSpesificMass;
225  else
226  return mass;
227  }
229  float GetRigidity(){
230  return rigidity;
231  };
234  return areaPreservingRate;
235  };
238  return areaPreservingRigidity;
239  };
242  return enableAreaPreserving;
243  }
246  return targetPreservationArea;
247  }
250  return enableSelfCollisions;
251  }
254  return selfCollisionParticleRadius;
255  }
258  return enablePassivationOfInternalSprings;
259  }
262  return enableShapeMatching;
263  }
264 
265 
268  return shapeMatchingRate;
269  }
270 
273  return enableShapeMatchingFixedTransform;
274  }
275 
278  return shapeMatchingFixedPosition;
279  }
280 
283  return shapeMatchingFixedRotation;
284  }
285 
288  return particleSpesificMass;
289  }
292  return enableParticleSpesificMass;
293  }
294 
298  virtual QSoftBody* ApplyForce(QVector force) override;
299 
300 
301  //
303  virtual void Update();
305  virtual void PostUpdate();
307  void PreserveAreas();
309  void ApplyShapeMatching();
310 
311 
312 
313 };
314 
315 #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:233
float GetRotation()
Definition: qbody.h:206
QVector GetPosition()
Definition: qbody.h:198
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:277
float GetTargetPreservationArea()
Definition: qsoftbody.h:245
float GetAreaPreservingRigidity()
Definition: qsoftbody.h:237
QSoftBody * SetRigidity(float value)
Definition: qsoftbody.h:88
float GetParticleSpesificMass()
Definition: qsoftbody.h:287
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:184
float GetShapeMatchingRate()
Definition: qsoftbody.h:267
bool GetShapeMatchingEnabled()
Definition: qsoftbody.h:261
bool GetSelfCollisionsEnabled()
Definition: qsoftbody.h:249
QSoftBody * SetPassivationOfInternalSpringsEnabled(bool value)
Definition: qsoftbody.h:150
QSoftBody * SetParticleSpesificMassEnabled(bool value)
Definition: qsoftbody.h:215
float GetRigidity()
Definition: qsoftbody.h:229
float GetShapeMatchingFixedRotation()
Definition: qsoftbody.h:282
QSoftBody * SetSelfCollisionsEnabled(bool value)
Definition: qsoftbody.h:132
void ApplyShapeMatching()
Definition: qsoftbody.cpp:260
virtual QSoftBody * ApplyForce(QVector force) override
Definition: qsoftbody.cpp:74
QSoftBody * SetShapeMatchingFixedRotation(float value)
Definition: qsoftbody.h:198
virtual void Update()
Definition: qsoftbody.cpp:93
float GetMass()
Definition: qsoftbody.h:222
bool GetShapeMatchingFixedTransformEnabled()
Definition: qsoftbody.h:272
bool GetPassivationOfInternalSpringsEnabled()
Definition: qsoftbody.h:257
QSoftBody * SetShapeMatchingFixedTransformEnabled(bool value)
Definition: qsoftbody.h:178
QSoftBody * SetShapeMatchingRate(float value)
Definition: qsoftbody.h:169
virtual void PostUpdate()
Definition: qsoftbody.cpp:178
float GetAreaPreservingRate()
Definition: qsoftbody.h:233
QSoftBody * SetSelfCollisionsSpecifiedRadius(float value)
Definition: qsoftbody.h:141
QSoftBody * SetShapeMatchingFixedPosition(QVector value)
Definition: qsoftbody.h:189
float GetSelfCollisionsSpecifiedRadius()
Definition: qsoftbody.h:253
QSoftBody * SetParticleSpesificMass(float value)
Definition: qsoftbody.h:206
bool GetAreaPreservingEnabled()
Definition: qsoftbody.h:241
bool GetParticleSpesificMassEnabled()
Definition: qsoftbody.h:291
Definition: qvector.h:44