Skip to content

Commit

Permalink
Added extensions folder to QuarkPhysics. Created QBroadPhase named ba…
Browse files Browse the repository at this point in the history
…se class to the creating custom broadphase solutions. Created the first extension QSpatialHashing.(It's not ready to use, it's empty)
  • Loading branch information
erayzesen committed Sep 4, 2024
1 parent 0417c5d commit fbac877
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 195 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ project(QuarkPhysics VERSION 0.9)
find_package(SFML 2.5 COMPONENTS graphics window system REQUIRED)
file(GLOB SOURCE_FILES
${PROJECT_SOURCE_DIR}/QuarkPhysics/*.cpp
${PROJECT_SOURCE_DIR}/QuarkPhysics/extensions/*.cpp
${PROJECT_SOURCE_DIR}/QuarkPhysics/json/*.hpp
${PROJECT_SOURCE_DIR}/examples/*.cpp
${PROJECT_SOURCE_DIR}/*.cpp
Expand Down
42 changes: 42 additions & 0 deletions QuarkPhysics/extensions/qspatialhashing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

/************************************************************************************
* MIT License
*
* Copyright (c) 2023 Eray Zesen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* https://github.com/erayzesen/QuarkPhysics
*
**************************************************************************************/

#include <iostream>
#include <algorithm>
#include "qspatialhashing.h"

void QSpatialHashing::Clear()
{
}

void QSpatialHashing::GetAllPairs(unordered_set<pair<int, int>, QBroadPhase::NumericPairHash, QBroadPhase::NumericPairEqual> &pairs, vector<QBody *> &originalCollection)
{
}

void QSpatialHashing::Update()
{
}
75 changes: 75 additions & 0 deletions QuarkPhysics/extensions/qspatialhashing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

/************************************************************************************
* MIT License
*
* Copyright (c) 2023 Eray Zesen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* https://github.com/erayzesen/QuarkPhysics
*
**************************************************************************************/




#ifndef QSPATIALHASHING_H
#define QSPATIALHASHING_H

#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "../qbroadphase.h"




class QSpatialHashing : public QBroadPhase {
vector<QBody*> bodies;
public:
QSpatialHashing(vector<QBody*> &worldBodies): QBroadPhase(worldBodies){
bodies=worldBodies;
}

~QSpatialHashing(){

}




void Clear();

void GetAllPairs(unordered_set<pair<int,int>,QBroadPhase::NumericPairHash,QBroadPhase::NumericPairEqual > &pairs, vector<QBody*> &originalCollection);

void Update();







private:



};


#endif // QSPATIALHASHING_H
130 changes: 5 additions & 125 deletions QuarkPhysics/qbroadphase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,134 +31,14 @@
#include "qworld.h"
#include "qaabb.h"



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

void QBroadPhase::Insert(int id, const QAABB& AABB) {
std::vector<int> cellKeys = GetCellKeys(AABB);

for (int key : cellKeys) {
hashTable[key].push_back(id);
}

}

void QBroadPhase::Update(int id, const QAABB& newAABB,QAABB& prevAABB) {

//Debug Test Bounding Boxes

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

if(isCleared==false)
if(prevAABB.isContain(newAABB) )
return;

auto fatAABB=newAABB.FattedWithRate(1.2f);


Remove(id, prevAABB);


Insert(id, fatAABB);

prevAABB=fatAABB;
}

void QBroadPhase::Remove(int id, const QAABB& AABB) {
std::vector<int> cellKeys = GetCellKeys(AABB);

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

void QBroadPhase::Clear(vector<QBody *> bodyCollection)
void QBroadPhase::Clear()
{
for (auto body: bodyCollection){
body->spatialContainerAABB=QAABB();
}
hashTable.clear();
isCleared=true;
}



void QBroadPhase::GetAllPairs( unordered_set<pair<int,int>,QBroadPhase::NumericPairHash,QBroadPhase::NumericPairEqual > &pairs,vector<QBody*> &originalCollection){




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

if(cell.size()==1) continue;

for (auto itA = cell.begin(); itA != cell.end(); ++itA) {
int idA=*itA;
QBody * body=originalCollection[idA];
if(body->GetEnabled()==false )
continue;
for (auto itB = itA+1; itB != cell.end(); ++itB) {
int idB=*itB;
QBody *otherBody=originalCollection[idB];

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<int,int>{idA,idB});
}


}
}

}

isCleared=false;


}


vector<int> QBroadPhase::GetCellItems(QAABB &aabb){
vector<int> items;
vector<int > cellKeys=GetCellKeys(aabb);

for (int key : cellKeys) {
vector<int> &cell=hashTable[key];
items.insert(items.end(),cell.begin(),cell.end() );
}

return items;
void QBroadPhase::GetAllPairs(unordered_set<pair<int, int>, QBroadPhase::NumericPairHash, QBroadPhase::NumericPairEqual> &pairs)
{
}

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;
void QBroadPhase::Update()
{
}
32 changes: 5 additions & 27 deletions QuarkPhysics/qbroadphase.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@

class QBroadPhase {
public:
QBroadPhase(){};
QBroadPhase(float cellSize);
QBroadPhase(vector<QBody*> &worldBodies){};
~QBroadPhase(){};

struct NumericPairHash {
size_t operator()(const std::pair<int, int>& p) const {
Expand All @@ -64,33 +64,11 @@ class QBroadPhase {




void Insert(int id, const QAABB& AABB);
void Update(int id, const QAABB& aabb, QAABB& prevAABB );
void Remove(int id, const QAABB& AABB);
virtual void Clear();

void Clear(vector<QBody *> bodyCollection);
virtual void GetAllPairs(unordered_set<pair<int,int>,QBroadPhase::NumericPairHash,QBroadPhase::NumericPairEqual > &pairs);

void GetAllPairs(unordered_set<pair<int,int>,QBroadPhase::NumericPairHash,QBroadPhase::NumericPairEqual > &pairs, vector<QBody*> &originalCollection);

vector<int> GetCellItems(QAABB &aabb);








private:
float cellSize;
std::unordered_map<int, std::vector<int>> hashTable;
std::vector<int> GetCellKeys(QAABB aabb);

bool isCleared=false;



virtual void Update();


};
Expand Down
22 changes: 13 additions & 9 deletions QuarkPhysics/qworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@

using namespace std;
QWorld::QWorld(){
broadPhase=QBroadPhase(spatialHashingSize);

}

Expand Down Expand Up @@ -95,13 +94,12 @@ void QWorld::Update(){

//Preparing Updated Broadphasevariables
if (enableBroadphase){
if (enableSpatialHashing){
for (int i=0;i<bodies.size();i++){
QBody *body=bodies[i];
broadPhase.Update(i,body->GetAABB(),body->spatialContainerAABB);
broadPhase.GetAllPairs(pairs,bodies);
}
if (broadPhase!=nullptr){
broadPhase->Update();
broadPhase->GetAllPairs(pairs);

}else{
//SweepAndPrune
sort(bodies.begin(),bodies.end(),SortBodiesHorizontal);
}

Expand All @@ -125,7 +123,7 @@ void QWorld::Update(){
if(enableBroadphase){


if(enableSpatialHashing){
if(broadPhase!=nullptr){
//Spatial Hashing method
for (auto pair: pairs){
QBody *bodyA=bodies[pair.first];
Expand Down Expand Up @@ -462,7 +460,10 @@ QWorld *QWorld::RemoveBodyAt(int index)

bodies.erase(bodies.begin()+index);

broadPhase.Clear(bodies);
if (broadPhase!=nullptr){
broadPhase->Clear();
}

}

return this;
Expand Down Expand Up @@ -662,6 +663,9 @@ QWorld* QWorld::ClearWorld(bool deleteAll){
ClearSprings(deleteAll);
ClearRaycasts(deleteAll);
ClearGizmos();
if (broadPhase!=nullptr){
delete broadPhase;
}

return this;
}
Expand Down
Loading

0 comments on commit fbac877

Please sign in to comment.