Skip to content

Commit

Permalink
Cleaned some leak problems.
Browse files Browse the repository at this point in the history
  • Loading branch information
erayzesen committed Sep 11, 2024
1 parent d6a3b14 commit bc38fca
Show file tree
Hide file tree
Showing 210 changed files with 6,192 additions and 1,917 deletions.
11 changes: 6 additions & 5 deletions Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ PROJECT_NAME = "Quark Physics"
# could be handy for archiving the generated documentation or if some version
# control system is used.

PROJECT_NUMBER =
PROJECT_NUMBER = "v1.0"

# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a
Expand All @@ -51,7 +51,7 @@ PROJECT_BRIEF = "2D Rigid and Soft Body Physics Engine"
# pixels and the maximum width should not exceed 200 pixels. Doxygen will copy
# the logo to the output directory.

PROJECT_LOGO =
PROJECT_LOGO = images/logo_x2.png

# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) path
# into which the generated documentation will be written. If a relative path is
Expand Down Expand Up @@ -864,7 +864,7 @@ WARN_LOGFILE =
# spaces. See also FILE_PATTERNS and EXTENSION_MAPPING
# Note: If this tag is empty the current directory is searched.

INPUT = "QuarkPhysics" "doxy_pages"
INPUT = "QuarkPhysics" "QuarkPhysics/extensions" "doxy_pages"

# This tag can be used to specify the character encoding of the source files
# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses
Expand Down Expand Up @@ -1283,8 +1283,9 @@ HTML_STYLESHEET =
# list). For an example see the documentation.
# This tag requires that the tag GENERATE_HTML is set to YES.

HTML_EXTRA_STYLESHEET =doxygen-awesome-css-for-qp/doxygen-awesome.css \
doxygen-awesome-css-for-qp/doxygen-awesome-sidebar-only.css
HTML_EXTRA_STYLESHEET =doxygen-awesome-css-for-qp/doxygen-awesome-custom-qp.css \
doxygen-awesome-css-for-qp/doxygen-awesome-sidebar-only.css \
doxygen-awesome-css-for-qp/tweeks.css

# The HTML_EXTRA_FILES tag can be used to specify one or more extra images or
# other source files which should be copied to the HTML output directory. Note
Expand Down
112 changes: 67 additions & 45 deletions QuarkPhysics/extensions/qspatialhashing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,79 +31,99 @@
#include "algorithm"


QSpatialHashing::QSpatialHashing(vector<QBody*>& worldBodies, float sizeOfCells): QBroadPhase(worldBodies) {
cellSize=sizeOfCells;

QSpatialHashing::QSpatialHashing(vector<QBody *> &worldBodies, float sizeOfCells) : QBroadPhase(worldBodies)
{

}

void QSpatialHashing::Clear()
{
bodyOldCells.clear();
cells.clear();
pairs.clear();
}


void QSpatialHashing::Update() {

float sizeFactor = 1 / cellSize;
void QSpatialHashing::Insert(QBody *body)
{


for (auto &body : bodies) {

auto aabb = body->GetAABB();
auto aabb = body->GetAABB();


CellAABB cellAABB(floor(aabb.GetMin().x * sizeFactor),floor(aabb.GetMin().y * sizeFactor),
floor(aabb.GetMax().x * sizeFactor),floor(aabb.GetMax().y * sizeFactor));
CellAABB cellAABB(floor(aabb.GetMin().x * cellSizeFactor),floor(aabb.GetMin().y * cellSizeFactor),
floor(aabb.GetMax().x * cellSizeFactor),floor(aabb.GetMax().y * cellSizeFactor));

auto it = bodyOldCells.find(body);


auto it = bodyOldCells.find(body);


if (it != bodyOldCells.end()) {
if (it != bodyOldCells.end()) {

CellAABB &oldCellAABB = it->second;

//AABB Cells doesn't change against to the previous.
if (oldCellAABB==cellAABB) {

continue;
}

//Removing body from cells
for (int cellX = oldCellAABB.minX; cellX <= oldCellAABB.maxX; ++cellX) {
for (int cellY = oldCellAABB.minY; cellY <= oldCellAABB.maxY; ++cellY) {
auto& cell = cells[{cellX, cellY}];
auto bodyIt=find(cell.begin(),cell.end(),body );
if (bodyIt!=cell.end ()){
cell.erase(bodyIt);
}
}
}

CellAABB &oldCellAABB = it->second;

//AABB Cells doesn't change against to the previous.
if (oldCellAABB==cellAABB) {

return;
}

bodyOldCells[body]= cellAABB;
//Adding body to cells
for (int cellX = cellAABB.minX; cellX <= cellAABB.maxX; ++cellX) {
for (int cellY = cellAABB.minY; cellY <= cellAABB.maxY; ++cellY) {
cells[{cellX, cellY}].push_back(body);
}
}
//Removing body from cells
RemoveBodyFromCells(body,oldCellAABB);

}


bodyOldCells[body]= cellAABB;
//Adding body to cells
for (int cellX = cellAABB.minX; cellX <= cellAABB.maxX; ++cellX) {
for (int cellY = cellAABB.minY; cellY <= cellAABB.maxY; ++cellY) {
cells[{cellX, cellY}].push_back(body);
}
}



}

void QSpatialHashing::Remove(QBody *body)
{

auto it = bodyOldCells.find(body);

if (it != bodyOldCells.end()) {

CellAABB &oldCellAABB = it->second;

void QSpatialHashing::GetAllPairs(std::unordered_set<std::pair<QBody*, QBody*>,QBody::BodyPairHash,QBody::BodyPairEqual> &pairs) {
//Removing body from cells
RemoveBodyFromCells(body,oldCellAABB);
//Removing from bodyOldCells collection
bodyOldCells.erase(it);
}

}

void QSpatialHashing::SetCellSize(float size)
{
Clear();
cellSize=size;
cellSizeFactor=1/cellSize;
}

void QSpatialHashing::RemoveBodyFromCells(QBody *body, CellAABB &cellAABB)
{
for (int cellX = cellAABB.minX; cellX <= cellAABB.maxX; ++cellX) {
for (int cellY = cellAABB.minY; cellY <= cellAABB.maxY; ++cellY) {
auto& cell = cells[{cellX, cellY}];
auto bodyIt=find(cell.begin(),cell.end(),body );
if (bodyIt!=cell.end ()){
cell.erase(bodyIt);
}
}
}
}

std::unordered_set<std::pair<QBody*, QBody*>,QBody::BodyPairHash,QBody::BodyPairEqual> & QSpatialHashing::GetPairs() {


pairs.clear();

for (auto& cell : cells) {
std::vector<QBody*>& cellBodies = cell.second;
size_t numBodies = cellBodies.size();
Expand Down Expand Up @@ -145,5 +165,7 @@ void QSpatialHashing::GetAllPairs(std::unordered_set<std::pair<QBody*, QBody*>,Q
}
}

return pairs;


}
11 changes: 9 additions & 2 deletions QuarkPhysics/extensions/qspatialhashing.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class QSpatialHashing : public QBroadPhase {
private:
float cellSize=128.0f;

float cellSizeFactor=1/cellSize;

struct PairHash {
template <class T1, class T2>
std::size_t operator()(const std::pair<T1, T2>& p) const {
Expand Down Expand Up @@ -89,14 +91,19 @@ class QSpatialHashing : public QBroadPhase {

std::unordered_map<QBody*,CellAABB> bodyOldCells;

void RemoveBodyFromCells(QBody *body,CellAABB &cellAABB);

public:
QSpatialHashing(vector<QBody*>& worldBodies, float sizeOfCells=128.0f);

void Clear();

void Update();
void Insert(QBody* body);
void Remove(QBody* body);

void SetCellSize(float size);

void GetAllPairs(std::unordered_set<std::pair<QBody*, QBody*>,QBody::BodyPairHash,QBody::BodyPairEqual> &pairs);
std::unordered_set<std::pair<QBody*, QBody*>,QBody::BodyPairHash,QBody::BodyPairEqual> & GetPairs();


};
Expand Down
1 change: 1 addition & 0 deletions QuarkPhysics/qbody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ QBody::~QBody()
{
for(int i=0;i<_meshes.size();i++){
delete _meshes[i];
_meshes[i]=nullptr;
}
_meshes.clear();
}
Expand Down
1 change: 1 addition & 0 deletions QuarkPhysics/qbody.h
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ class QBody{
*/
QBody *SetEnabled(bool value){
enabled=true;
return this;
}


Expand Down
9 changes: 7 additions & 2 deletions QuarkPhysics/qbroadphase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ void QBroadPhase::Clear()
{
}

void QBroadPhase::GetAllPairs(std::unordered_set<std::pair<QBody*, QBody*>,QBody::BodyPairHash,QBody::BodyPairEqual> &pairs)
std::unordered_set<pair<QBody*, QBody*>,QBody::BodyPairHash,QBody::BodyPairEqual> &QBroadPhase::GetPairs()
{
return pairs;
}

void QBroadPhase::Update()
void QBroadPhase::Insert(QBody *body)
{
}

void QBroadPhase::Remove(QBody *body)
{
}
14 changes: 9 additions & 5 deletions QuarkPhysics/qbroadphase.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@


class QBroadPhase {
protected:
std::unordered_set<pair<QBody*, QBody*>,QBody::BodyPairHash,QBody::BodyPairEqual> pairs;
bool BodiesCanCollide(QBody * bodyA,QBody *bodyB){
return QBody::CanCollide(bodyA,bodyB);
}

public:
vector<QBody*> &bodies;
QBroadPhase(vector<QBody*> &worldBodies): bodies(worldBodies){};
Expand All @@ -50,13 +56,11 @@ class QBroadPhase {

virtual void Clear();

virtual void GetAllPairs(std::unordered_set<std::pair<QBody*, QBody*>,QBody::BodyPairHash,QBody::BodyPairEqual> &pairs);
virtual std::unordered_set<pair<QBody*, QBody*>,QBody::BodyPairHash,QBody::BodyPairEqual> &GetPairs();

virtual void Update();
virtual void Insert(QBody* body);

bool BodiesCanCollide(QBody * bodyA,QBody *bodyB){
return QBody::CanCollide(bodyA,bodyB);
}
virtual void Remove(QBody* body);


};
Expand Down
24 changes: 0 additions & 24 deletions QuarkPhysics/qmanifold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,30 +61,6 @@ QManifold::QManifold(QBody *bodyA, QBody *bodyB)

}

//void QManifold::Update(vector<QCollision::Contact> newContacts)
//{

// //contacts.clear();
// vector<QCollision::Contact> mergedContacts;
// bool isFlipFlop=false;
// for(int i=0;i<newContacts.size();i++){
// auto nc=newContacts[i];
// int k=-1;
// for(int n=0;n<contacts.size();n++){
// auto c=contacts[n];
// if(c.particle==nc.particle){

// }

// }
// mergedContacts.push_back(nc);



// }
// contacts=mergedContacts;
//}



QVector QManifold::GetRelativeVelocity(QParticle *contactParticle,vector<QParticle*> referenceParticles,QVector rRef, QVector rInc)
Expand Down
9 changes: 9 additions & 0 deletions QuarkPhysics/qmesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,16 @@ QMesh::~QMesh()
{
for(int i=0;i<particles.size();i++){
delete particles[i];
particles[i]=nullptr;
}
particles.clear();

for(int i=0;i<springs.size();i++){
delete springs[i];
springs[i]=nullptr;
}
springs.clear();

}

void QMesh::UpdateCollisionBehavior()
Expand Down
12 changes: 11 additions & 1 deletion QuarkPhysics/qraycast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ vector<QBody *> QRaycast::GetPotentialBodies(QWorld *whichWorld, QVector rayPosi
void QRaycast::UpdateContacts()
{
if(world==nullptr)return;
contacts=RaycastTo(world,position,ray,1,enabledContainingBodies);
contacts=RaycastTo(world,position,ray,collidableLayersBit,enabledContainingBodies);
}


Expand Down Expand Up @@ -135,6 +135,10 @@ bool QRaycast::GetEnabledContainingBodies()
return enabledContainingBodies;
}

int QRaycast::GetCollidableLayersBit()
{
return collidableLayersBit;
}


float QRaycast::GetRotation()
Expand Down Expand Up @@ -168,6 +172,12 @@ QRaycast *QRaycast::SetEnabledContainingBodies(bool value)
return this;
}

QRaycast *QRaycast::SetCollidableLayersBit(int value)
{
collidableLayersBit=value;
return this;
}

void QRaycast::RaycastToParticles(QBody *body, QMesh *mesh, QVector rayPosition, QVector rayVector, QVector rayUnit, QVector rayNormal, bool enableContainingBodies,vector<QRaycast::Contact> *contacts)
{
int nearParticleIndex=-1;
Expand Down
9 changes: 9 additions & 0 deletions QuarkPhysics/qraycast.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class QRaycast

protected:
void UpdateContacts();

int collidableLayersBit=1;

public:
/** Creates a raycast.
Expand Down Expand Up @@ -89,6 +91,8 @@ class QRaycast
QVector GetRayVector();
/** Returns whether a body should be ignored in raycast collisions if the ray position is inside the shape representing the body in the world. If set to true, these objects will be ignored in raycast collisions. */
bool GetEnabledContainingBodies();
/**Returns the bit mask that represents the layers in which the body object is present. */
int GetCollidableLayersBit();

//Set Methods
/** Sets the position of the raycast
Expand All @@ -108,6 +112,11 @@ class QRaycast
*/
QRaycast *SetEnabledContainingBodies(bool value);

/**Sets the bit mask that represents the collidable layers in which the body object is present.A raycast object can collide with other body objects present in the layers defined by the user.
* @param value A bit mask value to set.
*/
QRaycast *SetCollidableLayersBit(int value);


friend class QWorld;

Expand Down
Loading

0 comments on commit bc38fca

Please sign in to comment.