Skip to content

Commit

Permalink
Fixed bugs on polygon angle constraints.
Browse files Browse the repository at this point in the history
  • Loading branch information
erayzesen committed Mar 18, 2024
1 parent 9c7ae4d commit 8fd60f0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 28 deletions.
9 changes: 7 additions & 2 deletions QuarkPhysics/qcollision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ void QCollision::CircleAndPolyline(vector<QParticle *> &circleParticles, vector<
D. If the circle particle is not within the polyline and the radius value is greater than 0.5, a collision edge is queried with a distance
test between the circle diameter and the edges of the nearest particle.
*/


vector< vector<QParticle*> > nearestSides;

Expand Down Expand Up @@ -275,6 +276,8 @@ void QCollision::CircleAndPolyline(vector<QParticle *> &circleParticles, vector<

float minDistance=-QWorld::MAX_WORLD_SIZE;

float maxIntersectionDistance=-QWorld::MAX_WORLD_SIZE;

for( int n=0;n<nearestSides.size();n++ ){
QParticle *sA=nearestSides[n][0];
QParticle *sB=nearestSides[n][1];
Expand Down Expand Up @@ -308,13 +311,15 @@ void QCollision::CircleAndPolyline(vector<QParticle *> &circleParticles, vector<
float radius=pA->GetRadius();
QVector bridgeVec=pA->GetGlobalPosition()-sAPos;
float dist=bridgeVec.Dot( sideNormal );
float distIntersection=(intersection-pA->GetGlobalPosition()).Length();
//pA->GetOwnerMesh()->GetOwnerBody()->GetWorld()->gizmos.push_back(new QGizmoLine(pA->GetGlobalPosition(),pA->GetGlobalPosition()+rayUnit*16,true ) );
if(dist<0 && dist>minDistance){
minDistance=dist;
normal=sideNormal;
penetration=dist-radius;
collidedSideIndex=n;
/* pA->GetOwnerMesh()->GetOwnerBody()->GetWorld()->gizmos.push_back(new QGizmoLine(pA->GetGlobalPosition(),pA->GetGlobalPosition()+rayUnit*64,true ) );
pA->GetOwnerMesh()->GetOwnerBody()->GetWorld()->gizmos.push_back(new QGizmoLine(sAPos,sBPos,false ) ); */
//pA->GetOwnerMesh()->GetOwnerBody()->GetWorld()->gizmos.push_back(new QGizmoLine(pA->GetGlobalPosition(),pA->GetGlobalPosition()+rayUnit*16,true ) );
//pA->GetOwnerMesh()->GetOwnerBody()->GetWorld()->gizmos.push_back(new QGizmoLine(sAPos,sBPos,false ) );
}
}
}else{
Expand Down
33 changes: 33 additions & 0 deletions QuarkPhysics/qmesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,39 @@ void QMesh::ApplyAngleConstraintsToPolygon()
return;
}

//Intersection Test
bool polygonIntersection=false;
for(int i=0;i<polygon.size();++i ){
int ni=(i+1)%polygon.size();
QParticle* d1A=polygon[i];
QParticle* d1B=polygon[ (i+1)%polygon.size() ];
for(int n=i+1;n<polygon.size();++n ){
if(n==i || n==ni || n==(i-1+polygon.size())%polygon.size() )continue;
QParticle* d2A=polygon[n];
QParticle* d2B=polygon[ (n+1)%polygon.size() ];

QVector intersection=QCollision::LineIntersectionLine(d1A->GetGlobalPosition(),d1B->GetGlobalPosition(),d2A->GetGlobalPosition(),d2B->GetGlobalPosition() );

if(intersection.isNaN()==false ){
d1A->GetOwnerMesh()->GetOwnerBody()->GetWorld()->GetGizmos()->push_back(new QGizmoCircle(intersection,5.0) );
polygonIntersection=true;
break;
}
}

}
if(polygonIntersection==true){
//cout<<"there is line intersection in polygon"<<endl;
pair<QVector,float> averagePosRot=QMesh::GetAveragePositionAndRotation(polygon);
vector<QVector> matchingShape=QMesh::GetMatchingParticlePositions(polygon,averagePosRot.first,averagePosRot.second);
for(int i=0;i<matchingShape.size();i++ ){
QVector force=(matchingShape[i]-polygon[i]->GetGlobalPosition())*0.2;
polygon[i]->ApplyForce(force );
lastPolygonCornerAngles.clear();
return;
}
}

bool beginToSaveAngles=false;

if (lastPolygonCornerAngles.size()!=polygon.size() ){
Expand Down
24 changes: 5 additions & 19 deletions QuarkPhysics/qworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -901,19 +901,17 @@ vector<QCollision::Contact*> QWorld::GetCollisions(QBody *bodyA, QBody *bodyB){
QMesh *polylineMesh=meshA->collisionBehavior==QMesh::POLYLINE ? meshA:meshB;
QMesh *polygonMesh=meshA->collisionBehavior==QMesh::POLYGONS ? meshA:meshB;
for(int b=0;b<polygonMesh->GetSubConvexPolygonCount();b++){
//QCollision::PolygonAndPolygon(polylineMesh->subConvexPolygons[a],polygonMesh->subConvexPolygons[b],contactList);
QCollision::CircleAndPolygon(polylineMesh->polygon,polygonMesh->GetSubConvexPolygonAt(b),contactList);
QCollision::PolylineAndPolygon(polylineMesh->polygon,polygonMesh->GetSubConvexPolygonAt(b),contactList);
}
}else if(QMesh::CheckCollisionBehaviors(meshA,meshB,QMesh::POLYLINE, QMesh::POLYLINE )){


if(bodyA->simulationModel==QBody::SimulationModels::MASS_SPRING && bodyB->simulationModel==QBody::SimulationModels::MASS_SPRING){
QCollision::CircleAndCircle(meshA->polygon,meshB->polygon,bboxB, contactList);
//QCollision::CircleAndCircle(meshA->polygon,meshB->polygon,bboxB, contactList);
QCollision::CircleAndPolyline(meshA->polygon,meshB->polygon,bboxB,contactList);
QCollision::CircleAndPolyline(meshB->polygon,meshA->polygon,bboxA, contactList);
/* QCollision::PolylineAndPolygon(meshA->polygon,meshB->polygon,contactList);
QCollision::PolylineAndPolygon(meshB->polygon,meshA->polygon,contactList); */


}

Expand Down Expand Up @@ -1055,16 +1053,7 @@ bool QWorld::SortBodiesVertical(const QBody *bodyA, const QBody *bodyB)


//Apply The Shape Matching Feature to Soft Bodies
/* for(auto body:bodies){
if(body->isSleeping)
continue;
if(body->GetMode()!=QBody::STATIC && body->GetSimulationModel()!=QBody::SimulationModels::RIGID_BODY){
QSoftBody *sBody=static_cast<QSoftBody*>(body);
if(sBody->GetShapeMatchingEnabled()){
sBody->ApplyShapeMatching();
}
}
} */

//Other Soft Body Constraints
for(auto body:bodies){

Expand Down Expand Up @@ -1092,12 +1081,9 @@ bool QWorld::SortBodiesVertical(const QBody *bodyA, const QBody *bodyB)
spring->Update(sBody->GetRigidity()*ts,sBody->GetPassivationOfInternalSpringsEnabled());
}
}






}

}

for(auto spring:springs){
Expand Down
14 changes: 7 additions & 7 deletions examples/examplesceneblobs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
ExampleSceneBlobs::ExampleSceneBlobs(QVector sceneSize):QExampleScene(sceneSize)
{

world->SetIterationCount(10);
world->SetIterationCount(2);

// Floor and walls
QBody *floor=new QBody();
Expand All @@ -59,12 +59,12 @@ ExampleSceneBlobs::ExampleSceneBlobs(QVector sceneSize):QExampleScene(sceneSize)
void ExampleSceneBlobs::OnKeyPressed(sf::Keyboard::Key key)
{
if(key==sf::Keyboard::Space){
QSoftBody *griddedPressuredBody=new QSoftBody();
griddedPressuredBody->AddMesh( QMesh::CreateWithPolygon(64,16,QVector::Zero(),-1 ,true,true) );
griddedPressuredBody->SetRigidity(0.5f)->SetPosition(mousePos)->SetMass(0.5f);
griddedPressuredBody->SetAreaPreservingEnabled(true)->SetAreaPreservingRate(0.7);
//griddedPressuredBody->SetSelfCollisionsEnabled(true)->SetSelfCollisionsSpecifiedRadius(6.0f);
world->AddBody(griddedPressuredBody);
QSoftBody *pressureBody=new QSoftBody();
pressureBody->AddMesh( QMesh::CreateWithPolygon(64,24,QVector::Zero(),-1 ,true,true) );
pressureBody->SetRigidity(0.5f)->SetPosition(mousePos)->SetMass(0.5f);
pressureBody->SetAreaPreservingEnabled(true)->SetAreaPreservingRate(0.7);
pressureBody->SetSelfCollisionsEnabled(true)->SetSelfCollisionsSpecifiedRadius(6.0f);
world->AddBody(pressureBody);
}
}

Expand Down
Binary file modified resources/robotoFont.hpp.gch
Binary file not shown.

0 comments on commit 8fd60f0

Please sign in to comment.