Skip to content

Commit

Permalink
Fixed some sleeping feature problems. Fixed low-mass inertia problems.
Browse files Browse the repository at this point in the history
  • Loading branch information
erayzesen committed Sep 4, 2024
1 parent 164cf53 commit 62a2987
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 63 deletions.
61 changes: 16 additions & 45 deletions QuarkPhysics/qbody.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ class QBody{
Modes mode=QBody::Modes::DYNAMIC;
bool inertiaNeedsUpdate=true;
bool circumferenceNeedsUpdate=true;
QVector force=QVector::Zero();
float angularForce=0.0f;
bool enableBodySpecificTimeScale=false;
float bodySpecificTimeScale=1.0f;
BodyTypes bodyType=BodyTypes::RIGID;
Expand Down Expand Up @@ -162,12 +160,12 @@ class QBody{



//General Get Methods

/** Returns the type of the body. */
BodyTypes GetBodyType(){
return bodyType;
}
//General Get Methods
/** Returns the world. */
QWorld *GetWorld(){
return world;
Expand Down Expand Up @@ -240,7 +238,7 @@ class QBody{
float GetInertia(){
if(inertiaNeedsUpdate==true){
inertia=GetTotalInitialArea()*2.0f*mass;
inertia=inertia==0.0f ? 0.25:inertia;
inertia=inertia<500.0f ? 500.0f:inertia;
inertiaNeedsUpdate=false;
}
return inertia;
Expand Down Expand Up @@ -318,14 +316,7 @@ class QBody{

return circumference;
}
/** Returns the current force value of the body. */
QVector GetForce(){
return force;
}
/** Returns the current angular force value of the body. */
float GetAngularForce(){
return angularForce;
}

/** Returns whether the body spesific time scale is enabled. */
bool GetBodySpecificTimeScaleEnabled(){
return enableBodySpecificTimeScale;
Expand Down Expand Up @@ -402,22 +393,7 @@ class QBody{
QBody * SetRotationDegree(float degree, bool withPreviousRotation=true){
return SetRotation( degree*(M_PI/180.0f),withPreviousRotation );
}
/** Sets the force value of the body. Set forces determine the force to be applied to a body object at the next physics step from the current step.
* @param value A value to set.
* @return A pointer to the body itself.
*/
QBody *SetForce(QVector value){
force=value;
return this;

}
/** Adds a vector to the force value of the body. Set forces determine the force to be applied to a body object at the next physics step from the current step.
* @param value A value to add.
* @return A pointer to the body itself.
*/
QBody *AddForce(QVector value){
return SetForce(GetForce()+value);
}

/** Adds a value to the rotation of the body.
* @param angleRadian A value to add, in radians.
* @return A pointer to the body itself.
Expand All @@ -440,21 +416,7 @@ class QBody{
QBody *AddPreviousRotation(float angleRadian){
return SetPreviousRotation(GetPreviousRotation()+angleRadian);
}
/** Sets the angular force of the body.
* @param value A value to set.
* @return A pointer to the body itself.
*/
QBody *SetAngularForce(float value){
angularForce=value;
return this;
}
/** Adds a value to the angular force of the body.
* @param value A value to add.
* @return A pointer to the body itself.
*/
QBody *AddAngularForce(float value){
return SetAngularForce(GetAngularForce()+value);
}




Expand Down Expand Up @@ -574,8 +536,6 @@ class QBody{
enabled=true;
}




//Mesh Methods
/** Adds a mesh to the body.
Expand Down Expand Up @@ -604,6 +564,17 @@ class QBody{
QBody * AddMeshesFromFile(string filePath);


//Methods About the Sleeping Feature
/**
* Wakes up a body that is in a sleeping state.
* @return A pointer to the body itself.
*/
QBody* WakeUp() {
isSleeping = false;
return this;
}


friend class QMesh;
friend class QWorld;
friend class QManifold;
Expand Down
20 changes: 10 additions & 10 deletions QuarkPhysics/qmesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ void QMesh::UpdateSubConvexPolygons()
subConvexPolygons.clear();
if (CheckIsPolygonConcave(polygon)==true ){
DecompositePolygon(polygon,subConvexPolygons);
cout<<"polygon is concave"<<endl;
// cout<<"polygon is concave"<<endl;
}else{
subConvexPolygons.push_back( polygon );
cout<<"polygon is convex"<<endl;
// cout<<"polygon is convex"<<endl;
}

}
Expand Down Expand Up @@ -421,7 +421,7 @@ void QMesh::DecompositePolygon(vector<QParticle *> &polygonParticles, vector<vec

//Per all subPolygons
while (ia!=subPolygons.size()){
cout<<"started subpolygons loop ia is:"<<ia <<endl;
// cout<<"started subpolygons loop ia is:"<<ia <<endl;
auto polyA=subPolygons[ia];
bool isDiagonal=false;
//Check diagonals
Expand All @@ -432,7 +432,7 @@ void QMesh::DecompositePolygon(vector<QParticle *> &polygonParticles, vector<vec
for (size_t ib=0;ib<subPolygons.size();ib++ ){
if (ia==ib)
continue;
cout<<"started subpolygons loop ib is:"<<ib <<endl;
// cout<<"started subpolygons loop ib is:"<<ib <<endl;
auto polyB=subPolygons[ib];
for (size_t nb=0;nb<polyB.size();nb++ ){
int pB1=polyB[nb];
Expand All @@ -447,32 +447,32 @@ void QMesh::DecompositePolygon(vector<QParticle *> &polygonParticles, vector<vec
int pIndex=(nb+1)%polyB.size();
int pos=na+1;
//Adding points to first polygon
cout<<"adding next polygon to prev polygon"<<endl;
// cout<<"adding next polygon to prev polygon"<<endl;
while (pIndex!=(nb-1+polyB.size() )%polyB.size() ){
pIndex=(pIndex+1)%polyB.size();
polyA.insert(polyA.begin()+pos,polyB[pIndex]);
pos+=1;
}
subPolygons[ia]=polyA;
cout<<"erased polygon"<<endl;
// cout<<"erased polygon"<<endl;
subPolygons.erase(subPolygons.begin()+ib );
cout<<"erased polygon finished"<<endl;
// cout<<"erased polygon finished"<<endl;
isDiagonal=true;
break;
}
}
if (isDiagonal){
cout<<"exiting for polyB loop"<<endl;
// cout<<"exiting for polyB loop"<<endl;
break;
}
}
if (isDiagonal){
cout<<"exiting for polyA loop"<<endl;
// cout<<"exiting for polyA loop"<<endl;
break;
}
}
if (isDiagonal){
cout<<"continue subpolygons loop ia is:"<<ia <<endl;
// cout<<"continue subpolygons loop ia is:"<<ia <<endl;
continue;
}
ia+=1;
Expand Down
8 changes: 8 additions & 0 deletions QuarkPhysics/qparticle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ QParticle *QParticle::SetPosition(QVector value){
if(ownerBody!=nullptr){
ownerBody->inertiaNeedsUpdate=true;
ownerBody->circumferenceNeedsUpdate=true;
if (ownerBody->GetBodyType()==QBody::BodyTypes::SOFT){
ownerBody->WakeUp();
}
}
}
return this;
Expand Down Expand Up @@ -129,6 +132,11 @@ QParticle *QParticle::ApplyForce(QVector value)
return this;
}
QParticle *QParticle::SetForce(QVector value){
if (ownerMesh!=nullptr){
if (ownerMesh->GetOwnerBody()!=nullptr ){
ownerMesh->GetOwnerBody()->WakeUp();
}
}
force=value;
return this;
}
Expand Down
31 changes: 27 additions & 4 deletions QuarkPhysics/qrigidbody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,33 @@ QRigidBody *QRigidBody::ApplyImpulse(QVector impulse, QVector r)
return this;
}

void QRigidBody::Update(){
if(mode==QBody::STATIC){
QRigidBody *QRigidBody::SetForce(QVector value)
{
WakeUp();
force=value;
return this;
}

QRigidBody *QRigidBody::AddForce(QVector value)
{
return SetForce(GetForce()+value);
}

QRigidBody *QRigidBody::SetAngularForce(float value)
{
WakeUp();
angularForce=value;
return this;
}

QRigidBody *QRigidBody::AddAngularForce(float value)
{
return SetAngularForce(GetAngularForce()+value);
}

void QRigidBody::Update()
{
if(mode==QBody::STATIC){
return;
}
if(world==nullptr){
Expand Down Expand Up @@ -133,5 +158,3 @@ void QRigidBody::Update(){
UpdateMeshTransforms();
UpdateAABB();
}


35 changes: 35 additions & 0 deletions QuarkPhysics/qrigidbody.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
class QRigidBody : public QBody
{
bool fixedRotation=false;
protected:
QVector force=QVector::Zero();
float angularForce=0.0f;
public:
QRigidBody();

Expand All @@ -56,6 +59,15 @@ class QRigidBody : public QBody
return allowKinematicCollisions;
}

/** Returns the current force value of the body. */
QVector GetForce(){
return force;
}
/** Returns the current angular force value of the body. */
float GetAngularForce(){
return angularForce;
}

//Set Methods
/** Sets whether the fixed rotation option is enabled. If set to true, the rotation value of the body never affected with the physics simulation. */
QRigidBody* SetFixedRotationEnabled(bool value){
Expand Down Expand Up @@ -93,6 +105,29 @@ class QRigidBody : public QBody
* @param r The relative position to apply impulse.
*/
QRigidBody* ApplyImpulse(QVector impulse,QVector r);
/** Sets the force value of the body. Set forces determine the force to be applied to a body object at the next physics step from the current step.
* @param value A value to set.
* @return A pointer to the body itself.
*/
QRigidBody *SetForce(QVector value);
/** Adds a vector to the force value of the body. Set forces determine the force to be applied to a body object at the next physics step from the current step.
* @param value A value to add.
* @return A pointer to the body itself.
*/
QRigidBody *AddForce(QVector value);

/** Sets the angular force of the body.
* @param value A value to set.
* @return A pointer to the body itself.
*/
QRigidBody *SetAngularForce(float value);
/** Adds a value to the angular force of the body.
* @param value A value to add.
* @return A pointer to the body itself.
*/
QRigidBody *AddAngularForce(float value);


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

Expand Down
4 changes: 2 additions & 2 deletions examples/examplesceneplatformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,14 @@ bool ExampleScenePlatformer::OnPlayerCollision(QBody *body, QBody::CollisionInfo
//Floor properties
auto side=QVector::GetVectorSide(-info.normal,QVector::Up());

if(info.body->GetBodyType()==QBody::BodyTypes::AREA){
if(info.body->GetBodyType()!=QBody::BodyTypes::RIGID){
return false;
}
//cout<<side<<endl;
if(side==QSides::DOWN){
isOnFloor=true;
//Defining currentfloor
currentFloor=info.body;
currentFloor=static_cast<QRigidBody*>(info.body);
}else if(side==QSides::RIGHT){
isOnRightWall=true;
}else if(side==QSides::LEFT){
Expand Down
2 changes: 1 addition & 1 deletion examples/examplesceneplatformer.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ExampleScenePlatformer : public QExampleScene
//Player properties
QRigidBody *player;
QVector velocity;
QBody *currentFloor=nullptr;
QRigidBody *currentFloor=nullptr;
int jumpTickCount=30;
int jumpTickDown=0;

Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ int main()
window->setFramerateLimit(60);
window->setKeyRepeatEnabled(false);
LoadExampleScene(9);
LoadExampleScene(1);
while (window->isOpen())
{
Expand Down
Binary file modified resources/robotoFont.hpp.gch
Binary file not shown.

0 comments on commit 62a2987

Please sign in to comment.