Skip to content

Commit

Permalink
Merge pull request #8 from erayzesen/v1.0.1
Browse files Browse the repository at this point in the history
V1.0.1
  • Loading branch information
erayzesen authored Dec 3, 2024
2 parents cfd4c24 + 3fcb6a7 commit fef4c29
Show file tree
Hide file tree
Showing 245 changed files with 7,368 additions and 2,848 deletions.
632 changes: 632 additions & 0 deletions QuarkPhysics/extensions/qplatformerbody.cpp

Large diffs are not rendered by default.

430 changes: 430 additions & 0 deletions QuarkPhysics/extensions/qplatformerbody.h

Large diffs are not rendered by default.

22 changes: 19 additions & 3 deletions QuarkPhysics/qbody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,24 @@ QBody::~QBody()
_meshes.clear();
}

float QBody::GetVelocityLimit() {
return velocityLimit;
}

bool QBody::GetIntegratedVelocitiesEnabled() {
return enableIntegratedVelocities;
}

QBody *QBody::SetVelocityLimit(float value)
{
velocityLimit=value;
return this;
}

QVector QBody::ComputeFriction(QBody *bodyA, QBody *bodyB, QVector &normal,float penetration, QVector &relativeVelocity){
QVector QBody::ComputeFriction(QBody *bodyA, QBody *bodyB, QVector &normal, float penetration, QVector &relativeVelocity)
{

QVector tangent=relativeVelocity-(relativeVelocity.Dot(normal)*normal );
QVector tangent=relativeVelocity-(relativeVelocity.Dot(normal)*normal );

tangent=tangent.Normalized();

Expand Down Expand Up @@ -108,8 +120,12 @@ bool QBody::CanCollide(QBody *bodyA, QBody *bodyB,bool checkBodiesAreEnabled)

//GENERAL METHODS

QBody *QBody::SetIntegratedVelocitiesEnabled(bool value) {
enableIntegratedVelocities=value;
return this;
}

QBody * QBody::AddMesh(QMesh *mesh){
QBody *QBody::AddMesh(QMesh *mesh) {
_meshes.push_back(mesh);
mesh->ownerBody=this;
UpdateMeshTransforms();
Expand Down
37 changes: 33 additions & 4 deletions QuarkPhysics/qbody.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ class QBody{
float bodySpecificTimeScale=1.0f;
BodyTypes bodyType=BodyTypes::RIGID;
bool enabled=true;
float velocityLimit=0.0f;
bool enableIntegratedVelocities=true;

//Material Properties;

Expand All @@ -122,7 +124,10 @@ class QBody{

void UpdateAABB();
void UpdateMeshTransforms();
/** Updates properties of the soft body and applies needed physical dynamics. */
virtual void Update(){};
/** Called after all bodies have completed their Update step to perform post-update operations. */
virtual void PostUpdate(){};
virtual bool CanGiveCollisionResponseTo(QBody *otherBody);

public:
Expand Down Expand Up @@ -341,6 +346,15 @@ class QBody{
bool GetEnabled(){
return enabled;
}
/**
* Returns the velocity limit of the physics body. If set to 0, no velocity limit is applied. The default value is 0.
*/
float GetVelocityLimit();

/**
* Returns whether the application of gravity and various velocity integrators necessary for the body's movement in the physics world is enabled. It is set to true by default. Typically, it is disabled for specific body objects that require manual control.
*/
bool GetIntegratedVelocitiesEnabled();



Expand All @@ -355,6 +369,7 @@ class QBody{
if (withPreviousPosition) {
prevPosition=position;
}
WakeUp();

UpdateMeshTransforms();
UpdateAABB();
Expand All @@ -363,10 +378,11 @@ class QBody{

/** Adds a vector value the position of the body.
* @param value A vector value to add.
* @param withPreviousPosition Determines whether apply the position to the previous position of the body.In this simulation, since velocities are implicit, if the previous position are the same as the newly set position, the positional velocity of the body will also be zeroed.Therefore, if you want to zero out the positional velocity when setting the new position, use this option.
* @return A pointer to the body itself.
*/
QBody *AddPosition(QVector value){
return SetPosition(GetPosition()+value);
QBody *AddPosition(QVector value, bool withPreviousPosition=true){
return SetPosition(GetPosition()+value,withPreviousPosition);
}
/** Sets the previous position of the body.
* @param value A position value to set.
Expand All @@ -392,6 +408,7 @@ class QBody{
rotation=angleRadian;
if(withPreviousRotation)
prevRotation=angleRadian;
WakeUp();
UpdateMeshTransforms();
return this;
}
Expand All @@ -406,10 +423,11 @@ class QBody{

/** Adds a value to the rotation of the body.
* @param angleRadian A value to add, in radians.
* @param withPreviousRotation Determines whether apply the rotation to the previous rotation of the body.In this simulation, since velocities are implicit, if the previous rotation are the same as the newly set rotation, the angular velocity of the body will also be zeroed.Therefore, if you want to zero out the angular velocity when setting the new rotation, use this option.
* @return A pointer to the body itself.
*/
QBody *AddRotation(float angleRadian){
return SetRotation(GetRotation()+angleRadian);
QBody *AddRotation(float angleRadian, bool withPreviousRotation=true){
return SetRotation(GetRotation()+angleRadian,withPreviousRotation);
}
/** Sets the previous rotation of the body.
* @param angleRadian A rotation value to set, in radians.
Expand Down Expand Up @@ -547,6 +565,11 @@ class QBody{
return this;
}

/**
* Sets whether the application of gravity and various velocity integrators necessary for the body's movement in the physics world is enabled. It is set to true by default. Typically, it is disabled for specific body objects that require manual control.
*/
QBody *SetIntegratedVelocitiesEnabled(bool value);


//Mesh Methods
/** Adds a mesh to the body.
Expand Down Expand Up @@ -585,6 +608,12 @@ class QBody{
return this;
}

/**
* Limits the velocity of the physics body. If set to 0, no velocity limit is applied. The default value is 0.
* @return A pointer to the body itself.
*/
QBody *SetVelocityLimit(float value);


friend class QMesh;
friend class QWorld;
Expand Down
2 changes: 1 addition & 1 deletion QuarkPhysics/qmesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ float QMesh::GetPolygonArea(vector<QParticle *> &polygonPoints,bool withLocalPos
area+=(a+b)*h*0.5f;
}

return abs(area);
return -area;
}

bool QMesh::CheckCollisionBehaviors(QMesh *meshA, QMesh *meshB, CollisionBehaviors firstBehavior, CollisionBehaviors secondBehavior){
Expand Down
11 changes: 10 additions & 1 deletion QuarkPhysics/qrigidbody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,17 @@ void QRigidBody::Update()
prevPosition.y=position.y;
}

if (velocityLimit>0.0f && vel.Length()>velocityLimit){
vel=velocityLimit*vel.Normalized();
}

//Angular Velocity
float rotVel=rotation-prevRotation;
prevRotation=rotation;


//Verlet Integration
if(isKinematic==false){
if(isKinematic==false && enableIntegratedVelocities==true){
position+=vel-(vel*airFriction);
position+=(world->GetGravity()*mass*ts);
rotation+=rotVel-(rotVel*airFriction);
Expand All @@ -158,3 +162,8 @@ void QRigidBody::Update()
UpdateMeshTransforms();
UpdateAABB();
}

void QRigidBody::PostUpdate()
{

}
7 changes: 5 additions & 2 deletions QuarkPhysics/qrigidbody.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class QRigidBody : public QBody
{
bool fixedRotation=false;
protected:
QVector force=QVector::Zero();
float angularForce=0.0f;
QVector force=QVector::Zero();
public:
QRigidBody();

Expand Down Expand Up @@ -129,7 +129,10 @@ class QRigidBody : public QBody


/** Updates properties of the rigid body and applies needed physical dynamics. */
void Update();
virtual void Update();

/** Called after all bodies have completed their Update step to perform post-update operations. */
virtual void PostUpdate();


};
Expand Down
68 changes: 62 additions & 6 deletions QuarkPhysics/qsoftbody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,38 @@
#include "cmath"
#include <utility>

bool QSoftBody::IsPolygonCW(vector<QParticle *> polygon)
{
int n = polygon.size();
if (n < 3) return false; // Poligon geçerli değil

// Geometrik merkez (ortalama pozisyon)
QVector center(0, 0);
for (const auto& point : polygon) {
center += point->GetGlobalPosition();
}
center /= n;

// İşaret kontrolü
float totalCross = 0.0f; // Toplam çapraz çarpım
for (int i = 0; i < n; ++i) {
QVector current = polygon[i]->GetGlobalPosition();
QVector next = polygon[(i + 1) % n]->GetGlobalPosition();

// Kenar vektörü
QVector edge = next - current;

// Kenarın merkezle olan vektörüne göre çapraz çarpımı hesapla
QVector toCenter = center - current;
float cross = edge.x * toCenter.y - edge.y * toCenter.x;

totalCross += cross;
}

// Toplam çapraz çarpımın işareti poligonun yönünü belirler
return totalCross < 0; // Pozitifse saat yönünün tersi
}

QSoftBody::QSoftBody()
{
simulationModel=SimulationModels::MASS_SPRING;
Expand Down Expand Up @@ -78,9 +110,15 @@ void QSoftBody::Update()
if(GetPassivationOfInternalSpringsEnabled() && particle->GetIsInternal())
continue;
auto vel=particle->GetGlobalPosition()-particle->GetPreviousGlobalPosition();
if (velocityLimit>0.0f && vel.Length()>velocityLimit){
vel=velocityLimit*vel.Normalized();
}

particle->SetPreviousGlobalPosition(particle->GetGlobalPosition() );
particle->ApplyForce(vel-(vel*airFriction) );
particle->ApplyForce(mass*world->GetGravity()*ts);
if(enableIntegratedVelocities==true){
particle->ApplyForce(vel-(vel*airFriction) );
particle->ApplyForce(mass*world->GetGravity()*ts);
}
particle->ApplyForce(particle->GetForce());
particle->SetForce(QVector::Zero());
}
Expand All @@ -104,6 +142,12 @@ void QSoftBody::Update()

}

void QSoftBody::PostUpdate()
{

}


void QSoftBody::PreserveAreas()
{

Expand All @@ -123,28 +167,40 @@ void QSoftBody::PreserveAreas()

if(mesh->GetSpringCount()==0)continue;
float currentMeshesArea=mesh->GetPolygonsArea();
float circumference=GetCircumference();

if(currentMeshesArea<-targetPreservationArea*5){
currentMeshesArea=-targetPreservationArea*5;
}
if(currentMeshesArea>targetPreservationArea*5){
currentMeshesArea=targetPreservationArea*5;
}
float circumference=max (GetCircumference(),0.001f);

float deltaArea=(targetPreservationArea*areaPreservingRate)-currentMeshesArea;

if(enableAreaStability==false){
if(deltaArea<0){
deltaArea=0;
}else{
enableAreaStability=true;
}
}


if (deltaArea==0.0f){
return;
}

float pressure=(deltaArea/circumference)*areaPreservingRigidity;



QVector volumeForces[mesh->polygon.size()];
QVector centerOfMesh=QMesh::GetAveragePositionAndRotation(mesh->polygon).first;
for(int n=0;n<mesh->polygon.size();n++){
QParticle *pp=mesh->polygon[ (n-1+mesh->polygon.size())%mesh->polygon.size() ];
QParticle *np=mesh->polygon[ (n+1)%mesh->polygon.size() ];
QVector vec=np->GetGlobalPosition()-pp->GetGlobalPosition();
volumeForces[n]=pressure*(vec.Perpendicular().Normalized())*ts;
QVector normal=vec.Perpendicular().Normalized();
volumeForces[n]=pressure*(normal)*ts;
}

for(int n=0;n<mesh->polygon.size();n++){
Expand Down
6 changes: 5 additions & 1 deletion QuarkPhysics/qsoftbody.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class QSoftBody : public QBody
}
}

bool IsPolygonCW(vector<QParticle*> polygon);


public:
QSoftBody();
Expand Down Expand Up @@ -295,7 +297,9 @@ class QSoftBody : public QBody

//
/** Updates properties of the soft body and applies needed physical dynamics. */
void Update();
virtual void Update();
/** Called after all bodies have completed their Update step to perform post-update operations. */
virtual void PostUpdate();
/** Applies the preserve area operation to the body. */
void PreserveAreas();
/** Applies the shape matching operation to the body. */
Expand Down
Loading

0 comments on commit fef4c29

Please sign in to comment.