Skip to content

Commit

Permalink
Dev version of v0.9.2
Browse files Browse the repository at this point in the history
  • Loading branch information
erayzesen committed Jan 4, 2024
1 parent 380bcac commit af63d00
Show file tree
Hide file tree
Showing 29 changed files with 3,384 additions and 438 deletions.
28 changes: 14 additions & 14 deletions QuarkPhysics/qaabb.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,11 @@ class QAABB
return (minPos+maxPos)*0.5f;
}

bool isContain(QAABB &otherAABB)const{
bool result=true;
result=result && minPos.x<=otherAABB.minPos.x;
result=result && minPos.y<=otherAABB.minPos.y;
result=result && maxPos.x>=otherAABB.maxPos.x;
result=result && maxPos.y>=otherAABB.maxPos.y;

return result;

bool isContain(const QAABB& otherAABB) const {
return (minPos.x <= otherAABB.minPos.x) &&
(minPos.y <= otherAABB.minPos.y) &&
(maxPos.x >= otherAABB.maxPos.x) &&
(maxPos.y >= otherAABB.maxPos.y);
}
static QAABB Combine( QAABB &b1, QAABB &b2){
QAABB output=QAABB();
Expand All @@ -116,11 +112,15 @@ class QAABB
return QAABB(minPos-amountVec,maxPos+amountVec);

}
bool isCollidingWith(QAABB otherAABB) const{
if( minPos.x<=otherAABB.maxPos.x && maxPos.x>=otherAABB.minPos.x && minPos.y<=otherAABB.maxPos.y && maxPos.y>=otherAABB.minPos.y ){
return true;
}
return false;

QAABB FattedWithRate(float rate)const{
QVector ratedSize=size*rate*0.5f;
return QAABB(minPos-ratedSize,maxPos+ratedSize);
}

bool isCollidingWith(const QAABB& otherAABB) const {
return maxPos.x >= otherAABB.minPos.x && minPos.x <= otherAABB.maxPos.x &&
maxPos.y >= otherAABB.minPos.y && minPos.y <= otherAABB.maxPos.y;
}


Expand Down
2 changes: 2 additions & 0 deletions QuarkPhysics/qbody.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class QBody{
float rotation=0.0f;
float prevRotation=0.0f;
QAABB aabb;
QAABB spatialContainerAABB;
QAABB fattedAABB;
Modes mode=QBody::Modes::DYNAMIC;
bool inertiaNeedsUpdate=true;
Expand Down Expand Up @@ -610,6 +611,7 @@ class QBody{
friend class QManifold;
friend class QParticle;
friend class QJoint;
friend class QBroadPhase;

protected:
vector<QMesh*> _meshes=vector<QMesh*>();
Expand Down
231 changes: 145 additions & 86 deletions QuarkPhysics/qbroadphase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,120 +26,179 @@
**************************************************************************************/

#include "qbroadphase.h"
#include <cmath>
#include <iostream>
#include <algorithm>
#include "qworld.h"
#include "qaabb.h"



QBroadPhase::~QBroadPhase()
{
QBroadPhase::QBroadPhase(float cellSize) : cellSize(cellSize) {}

}
void QBroadPhase::insert(QBody *body, const QAABB& aabb) {
std::vector<int> cellKeys = getCellKeys(aabb);

QBroadPhase *QBroadPhase::Add(QBody* body)
{
QAABB aabb=body->GetAABB();
if(body->GetFattedAABB().isContain(aabb)){
return this;
}
for (int key : cellKeys) {
hashTable[key].push_back(body);
}

}

RemoveBodyFromCells(body->GetFattedAABB(),body);
void QBroadPhase::update(QBody *body, const QAABB& newAABB) {

//Debug Test Bounding Boxes

/* body->GetWorld()->gizmos.push_back( new QGizmoRect(body->spatialContainerAABB) );
body->GetWorld()->gizmos.push_back( new QGizmoRect(newAABB) ); */

float fatFactor=5.0f;
if(body->GetSimulationModel()==QBody::SimulationModels::RIGID_BODY){
fatFactor+=(body->GetPosition()-body->GetPreviousPosition()).Length();
}
body->GetFattedAABB()=body->GetAABB().Fatted(fatFactor );
QVector min = body->GetFattedAABB().GetMin();
min *= inverseCellSize;
int minX = std::floor(min.x);
int minY= std::floor(min.y);
if(body->spatialContainerAABB.isContain(newAABB) ){
return;
}

auto fatAABB=newAABB.FattedWithRate(1.2f);


QVector max = body->GetFattedAABB().GetMax();
max *= inverseCellSize;
int maxX = std::floor(max.x);
int maxY= std::floor(max.y);
remove(body, body->spatialContainerAABB);


insert(body, fatAABB);

for (int x = minX; x <= maxX; x++){
for (int y = minY; y <= maxY; y++)
{
int cell=hashFunction(x,y);
body->spatialContainerAABB=fatAABB;
}

collisionGroups[cell].insert(body);
}
}
void QBroadPhase::remove(QBody *body, const QAABB& aabb) {
std::vector<int> cellKeys = getCellKeys(aabb);

isBodyAdded=true;
return this;
for (int key : cellKeys) {
auto& cell = hashTable[key];
auto it=find(cell.begin(),cell.end(),body);
if(it!=cell.end() ){
cell.erase(it);
}
}
}

void QBroadPhase::ApplySweepAndPruneToCells()
{
for (auto& cellPair : hashTable) {
vector<QBody*> &cell = cellPair.second;
sort(cell.begin(),cell.end(),QWorld::SortBodies);
}
}

void QBroadPhase::GetPotentialCollisions(QBody *body, unordered_set<QBody *> &collection)
{


if(body->GetEnabled()==false )
return;

std::vector<int> cellKeys = getCellKeys(body->spatialContainerAABB);

for (size_t i = 0; i < cellKeys.size(); ++i){
auto &cell = hashTable[cellKeys[i]];

auto itA = cell.begin();
while (itA != cell.end() && *itA != body) {
++itA;
}

for (auto itB = std::next(itA); itB != cell.end(); ++itB) {
QBody *otherBody=*itB;

if(otherBody->GetEnabled()==false )
continue;

if( QBody::CanCollide(body,otherBody)==false){
continue;
}
body->GetWorld()->debugAABBTestCount+=1;
if(body->GetAABB().GetMax().x >= otherBody->GetAABB().GetMin().x){
if( body->GetAABB().GetMin().y <= otherBody->GetAABB().GetMax().y &&
body->GetAABB().GetMax().y >= otherBody->GetAABB().GetMin().y) {
collection.insert(otherBody);
}

}else{
break;
}


}


}

}

vector< vector<QBody*> > QBroadPhase::GetBodiesFromCells() {
vector< vector<QBody*> > result;

// Hash tablosundaki her hücreyi dolaşıyoruz.
for (const auto& cellPair : hashTable) {
const vector<QBody*>& cell = cellPair.second;
std::vector<QBody*> uniqueBodies(cell.begin(), cell.end());
result.push_back(uniqueBodies);
}

QBroadPhase *QBroadPhase::Clear()
{
collisionGroups.clear();
return this;
return result;
}

std::unordered_set<std::pair<QBody*,QBody*>,QBroadPhase::pairHash,QBroadPhase::pairEqual>* QBroadPhase::GetPairs()
{
// if(isBodyAdded==false)
// return &pairList;

pairList.clear();

/* for(auto const &[key, value] : collisionGroups) {
for(auto ita=value.begin();ita!=value.end();++ita) {
auto bodyA=*ita;
for(auto itb=next(ita);itb!=value.end();++itb) {
auto bodyB=*itb;
auto pair = minmax(bodyA, bodyB);
pairList.insert(pair);
}
}
} */
isBodyAdded=false;
return &pairList;

}
void QBroadPhase::GetAllPairs( unordered_set<pair<QBody*,QBody*>,QBroadPhase::bodyPairHash,bodyPairEqual > &pairs){

void QBroadPhase::RemoveBodyFromCells(QAABB referenceAABB, QBody *body)
{
QVector min = referenceAABB.GetMin();
min *= inverseCellSize;
int minX = std::floor(min.x);
int minY= std::floor(min.y);


QVector max = referenceAABB.GetMax();
max *= inverseCellSize;
int maxX = std::floor(max.x);
int maxY= std::floor(max.y);

for (int x = minX; x <= maxX; x++){
for (int y = minY; y <= maxY; y++)
{
int cell=hashFunction(x,y);
auto groupIt=collisionGroups.find(cell);
if(groupIt==collisionGroups.end())



for (auto& cellPair : hashTable) {
vector<QBody*> &cell = cellPair.second;

for (auto itA = cell.begin(); itA != cell.end(); ++itA) {
QBody *body=*itA;
if(body->GetEnabled()==false )
continue;
unordered_set<QBody*> *group=&groupIt->second;
//auto it=std::find(group->begin(),group->end(),body);
auto it=group->find(body);
if(it!=group->end()){
group->erase(it);
}
for (auto itB = itA+1; itB != cell.end(); ++itB) {
QBody *otherBody=*itB;

if(otherBody->GetEnabled()==false )
continue;

/* if( QBody::CanCollide(body,otherBody)==false){
continue;
} */

body->GetWorld()->debugAABBTestCount+=1;
if(body->GetAABB().isCollidingWith(otherBody->GetAABB()) ){
pairs.insert(pair<QBody*,QBody*>{body,otherBody});
}


}
}




}


}

std::vector<int> QBroadPhase::getCellKeys(QAABB aabb) {
std::vector<int> cellKeys;

}
}
int minCellX = static_cast<int>(aabb.GetMin().x / cellSize);
int minCellY = static_cast<int>(aabb.GetMin().y / cellSize);
int maxCellX = static_cast<int>(aabb.GetMax().x / cellSize);
int maxCellY = static_cast<int>(aabb.GetMax().y / cellSize);

// AABB'nin kapsadığı hücreleri buluyoruz.
for (int cellX = minCellX; cellX <= maxCellX; ++cellX) {
for (int cellY = minCellY; cellY <= maxCellY; ++cellY) {
int key = cellX | (cellY << 16);
cellKeys.push_back(key);
}
}

return cellKeys;
}

Loading

0 comments on commit af63d00

Please sign in to comment.