Skip to content

Commit

Permalink
Added some needed functions to the engine and extension.
Browse files Browse the repository at this point in the history
  • Loading branch information
erayzesen committed Jan 14, 2025
1 parent e630741 commit 7f7b95c
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/QuarkPhysics/qbody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,17 @@ void QBody::UpdateMeshTransforms(){

}


void QBody::Update()
{
for (auto mesh: _meshes){
for(size_t i=0;i<mesh->GetParticleCount();++i ){
QParticle *particle=mesh->GetParticleAt(i);
if (particle->GetOneTimeCollisionEnabled()==true ){
particle->ResetOneTimeCollisions();
}
}
}
}

bool QBody::CanGiveCollisionResponseTo(QBody *otherBody)
{
Expand Down
2 changes: 1 addition & 1 deletion src/QuarkPhysics/qbody.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class QBody{
void UpdateAABB();
void UpdateMeshTransforms();
/** Updates properties of the soft body and applies needed physical dynamics. */
virtual void Update(){};
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);
Expand Down
62 changes: 61 additions & 1 deletion src/QuarkPhysics/qmanifold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ void QManifold::Solve()
for(int i=0;i<contacts.size();i++){
QCollision::Contact *contact=contacts[i];



if(betweenRigidbodies){
contact->penetration*=0.75f;
}else if(betweenPressuredSoftbodies){
Expand All @@ -152,6 +154,12 @@ void QManifold::Solve()
QBody *incidentBody=contact->particle->GetOwnerMesh()->GetOwnerBody();









QRigidBody *refRigidBody=nullptr;
QRigidBody *incRigidBody=nullptr;
Expand Down Expand Up @@ -211,15 +219,67 @@ void QManifold::Solve()
}



//Exceptions of Returns of Events.

if(collisionEnabledByRef==false || collisionEnabledByInc==false){
cancelSolving=true;
}


if(cancelSolving==true)
continue;


//Exceptions of Disabled Particles
if(contact->particle->GetEnabled()==false )
continue;

for (size_t n=0;n<contact->referenceParticles.size();++n ){
if (contact->referenceParticles[n]->GetEnabled()==false ){
cancelSolving=true;
break;
}

}

if(cancelSolving==true)
continue;







//Exceptions of the One Time Collisions of Particles
if (contact->particle->GetOneTimeCollisionEnabled() )
contact->particle->previousCollidedBodies.insert(referenceBody);

for (size_t n=0;n<contact->referenceParticles.size();++n ){
if (contact->referenceParticles[n]->GetOneTimeCollisionEnabled() )
contact->referenceParticles[n]->previousCollidedBodies.insert(incidentBody);
}

if (contact->particle->GetOneTimeCollisionEnabled() ){
if( contact->particle->oneTimeCollidedBodies.find(referenceBody)!=contact->particle->oneTimeCollidedBodies.end() ){
continue;
}
}

for (size_t n=0;n<contact->referenceParticles.size();++n ){
if (contact->referenceParticles[n]->GetOneTimeCollisionEnabled() ){
if( contact->referenceParticles[n]->oneTimeCollidedBodies.find(incidentBody)!=contact->referenceParticles[n]->oneTimeCollidedBodies.end() )
cancelSolving=true;
break;
}

}

if(cancelSolving==true)
continue;





//APPLYING FORCES TO BODIES
Expand Down
23 changes: 23 additions & 0 deletions src/QuarkPhysics/qparticle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@
#include "qbody.h"
#include "qvector.h"

void QParticle::ClearOneTimeCollisions()
{
oneTimeCollidedBodies.clear();
previousCollidedBodies.clear();
}

void QParticle::ResetOneTimeCollisions()
{
oneTimeCollidedBodies=previousCollidedBodies;
previousCollidedBodies.clear();
}

QParticle::QParticle()
{
Expand Down Expand Up @@ -123,6 +134,18 @@ QParticle *QParticle::SetIsInternal(bool value)
return this;
}

QParticle *QParticle::SetEnabled(bool value)
{
enabled=value;
return this;
}

QParticle *QParticle::SetOneTimeCollisionEnabled(bool value)
{
enableOneTimeCollision=value;
ClearOneTimeCollisions();
return this;
}

QParticle *QParticle::ApplyForce(QVector value)
{
Expand Down
45 changes: 45 additions & 0 deletions src/QuarkPhysics/qparticle.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,21 @@ class QParticle

std::vector<QVector> accumulatedForces;

bool enabled=true;

bool enableOneTimeCollision=false;

void ClearOneTimeCollisions();

protected:
std::unordered_set<QParticle*> springConnectedParticles;

unordered_set<QBody*> oneTimeCollidedBodies;
unordered_set<QBody*> previousCollidedBodies;

void ResetOneTimeCollisions();


public:
QParticle();
QParticle(float posX,float posY,float radius=0.5f);
Expand Down Expand Up @@ -97,6 +110,19 @@ class QParticle
QVector GetForce(){
return force;
}
/** Returns whether the particle is enabled. Disabled particles are not exempt from the collision tests that involve the meshes they belong to, but the solutions of their manifolds are not applied. Additionally, in body types where particles can move freely individually (e.g., QSoftBody), force and velocity integrations are not applied.
* @note Disabled particles in QRigidBody objects may appear to move because they are transformed based on the position and rotation of the rigid body.
*/
bool GetEnabled(){
return enabled;
}
/**
* Returns whether the particle's one-time collision feature is enabled. When the one-time collision feature is active, an interaction occurs once at the beginning of the collision with objects. For subsequent collision interactions, the object must exit the collision and re-enter to trigger another interaction.
*/

bool GetOneTimeCollisionEnabled(){
return enableOneTimeCollision;
}

//Set Methods
/** Sets the local position of the particle.
Expand Down Expand Up @@ -147,6 +173,22 @@ class QParticle
*/
QParticle *SetIsInternal(bool value);

/** Sets whether the particle is enabled. Disabled particles are not exempt from the collision tests that involve the meshes they belong to, but the solutions of their manifolds are not applied. Additionally, in body types where particles can move freely individually (e.g., QSoftBody), force and velocity integrations are not applied.
* @note Disabled particles in QRigidBody objects may appear to move because they are transformed based on the position and rotation of the rigid body.
* @param value A value to set.
* @return A pointer to the particle itself.
*/

QParticle *SetEnabled(bool value);

/**
* Sets whether the particle's one-time collision feature is enabled. When the one-time collision feature is active, an interaction occurs once at the beginning of the collision with objects. For subsequent collision interactions, the object must exit the collision and re-enter to trigger another interaction.
* @param value A value to set.
* @return A pointer to the particle itself.
*/

QParticle *SetOneTimeCollisionEnabled(bool value);

//
/** Applies a force immediately to the particle. You can use the method safely before the physics step (e.g. at the OnPreStep event of QBody objects). If you want to use this method after physics step, it can break the simulation.(Collisions and constraints may not be applied properly.) if you want to apply force at the next physic step safely, use SetForce() and AddForce() methods.
* @param value A force value to apply.
Expand Down Expand Up @@ -193,7 +235,10 @@ class QParticle
*/
static void ApplyForceToParticleSegment(QParticle *pA,QParticle *pB,QVector force,QVector fromPosition);


friend class QMesh;
friend class QBody;
friend class QManifold;


};
Expand Down
3 changes: 3 additions & 0 deletions src/QuarkPhysics/qrigidbody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ QRigidBody *QRigidBody::AddAngularForce(float value)

void QRigidBody::Update()
{
QBody::Update();


if(mode==QBody::STATIC){
return;
}
Expand Down
5 changes: 5 additions & 0 deletions src/QuarkPhysics/qsoftbody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,13 @@ QSoftBody::QSoftBody()

void QSoftBody::Update()
{
QBody::Update();

if (mode==QBody::STATIC){
return;
}


//Sleep Feature

if(isSleeping){
Expand All @@ -107,6 +110,8 @@ void QSoftBody::Update()
QMesh *mesh=_meshes[i];
for(int n=0;n<mesh->GetParticleCount();n++){
QParticle *particle=mesh->GetParticleAt(n);


/* if(GetPassivationOfInternalSpringsEnabled() && particle->GetIsInternal())
continue; */
auto vel=particle->GetGlobalPosition()-particle->GetPreviousGlobalPosition();
Expand Down
1 change: 1 addition & 0 deletions src/QuarkPhysics/qsoftbody.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ class QSoftBody : public QBody
void PreserveAreas();
/** Applies the shape matching operation to the body. */
void ApplyShapeMatching();



};
Expand Down
27 changes: 27 additions & 0 deletions src/qparticle_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ void QParticleObject::_bind_methods() {
ClassDB::bind_method(D_METHOD( "get_radius" ),&QParticleObject::get_radius );
ClassDB::bind_method(D_METHOD( "get_is_internal" ),&QParticleObject::get_is_internal );
ClassDB::bind_method(D_METHOD( "get_force" ),&QParticleObject::get_force );
ClassDB::bind_method(D_METHOD( "get_enabled" ),&QParticleObject::get_enabled );
ClassDB::bind_method(D_METHOD( "get_one_time_collision_enabled" ),&QParticleObject::get_one_time_collision_enabled );

//Set
ClassDB::bind_method(D_METHOD( "set_global_position","value" ),&QParticleObject::set_global_position );
ClassDB::bind_method(D_METHOD( "add_global_position","value" ),&QParticleObject::add_global_position );
Expand All @@ -26,6 +29,8 @@ void QParticleObject::_bind_methods() {
ClassDB::bind_method(D_METHOD( "apply_force","value" ),&QParticleObject::apply_force );
ClassDB::bind_method(D_METHOD( "set_force","value" ),&QParticleObject::set_force );
ClassDB::bind_method(D_METHOD( "add_force","value" ),&QParticleObject::add_force );
ClassDB::bind_method(D_METHOD( "set_enabled","value" ),&QParticleObject::set_enabled );
ClassDB::bind_method(D_METHOD( "set_one_time_collision_enabled","value" ),&QParticleObject::set_one_time_collision_enabled );


}
Expand Down Expand Up @@ -67,6 +72,16 @@ Vector2 QParticleObject::get_force() {
return Vector2(value.x,value.y);
}

bool QParticleObject::get_enabled()
{
return particleObject->GetEnabled();
}

bool QParticleObject::get_one_time_collision_enabled()
{
return particleObject->GetOneTimeCollisionEnabled();
}

//SET METHODS
Ref<QParticleObject> QParticleObject::set_global_position(Vector2 value) {
particleObject->SetGlobalPosition(QVector(value.x,value.y) );
Expand Down Expand Up @@ -127,3 +142,15 @@ Ref<QParticleObject> QParticleObject::add_force(Vector2 value) {
particleObject->AddForce(QVector(value.x,value.y) );
return Ref<QParticleObject>(this);
}

Ref<QParticleObject> QParticleObject::set_enabled(bool value)
{
particleObject->SetEnabled(value);
return Ref<QParticleObject>(this);
}

Ref<QParticleObject> QParticleObject::set_one_time_collision_enabled(bool value)
{
particleObject->SetOneTimeCollisionEnabled(value);
return Ref<QParticleObject>(this);
}
6 changes: 6 additions & 0 deletions src/qparticle_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class QParticleObject: public RefCounted{
float get_radius();
bool get_is_internal();
Vector2 get_force();
bool get_enabled();
bool get_one_time_collision_enabled();

//Set Methods
Ref<QParticleObject> set_global_position(Vector2 value);
Expand All @@ -91,6 +93,10 @@ class QParticleObject: public RefCounted{
Ref<QParticleObject> apply_force(Vector2 value);
Ref<QParticleObject> set_force(Vector2 value);
Ref<QParticleObject> add_force(Vector2 value);
Ref<QParticleObject> set_enabled(bool value);
Ref<QParticleObject> set_one_time_collision_enabled(bool value);



friend class QSpringObject;
friend class QMeshNode;
Expand Down

0 comments on commit 7f7b95c

Please sign in to comment.