Skip to content

Commit

Permalink
Added shouldCollide callback
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardodoria committed Sep 6, 2023
1 parent b509582 commit 6ecd2b3
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 6 deletions.
18 changes: 18 additions & 0 deletions engine/core/object/physics/Body2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ Body2D::Body2D(const Body2D& rhs): EntityHandle(rhs){

}

b2Body* Body2D::getBox2DBody() const{
Body2DComponent& body = getComponent<Body2DComponent>();

return body.body;
}

b2Fixture* Body2D::getBox2DFixture(size_t index) const{
Body2DComponent& body = getComponent<Body2DComponent>();

if (index >=0 && index < MAX_SHAPES){
return body.shapes[index].fixture;
}else{
Log::error("Cannot find shape %i of body", index);
}

return NULL;
}

Object Body2D::getAttachedObject(){
return Object(scene, entity);
}
Expand Down
3 changes: 3 additions & 0 deletions engine/core/object/physics/Body2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ namespace Supernova{
Body2D(const Body2D& rhs);
Body2D& operator=(const Body2D& rhs);

b2Body* getBox2DBody() const;
b2Fixture* getBox2DFixture(size_t index) const;

Object getAttachedObject();

int createRectShape(float width, float height);
Expand Down
6 changes: 6 additions & 0 deletions engine/core/object/physics/Joint2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ Joint2D& Joint2D::operator=(const Joint2D& rhs){
return *this;
}

b2Joint* Joint2D::getBox2DJoint() const{
Joint2DComponent& joint = getComponent<Joint2DComponent>();

return joint.joint;
}

void Joint2D::setDistanceJoint(Entity bodyA, Entity bodyB, Vector2 worldAnchorOnBodyA, Vector2 worldAnchorOnBodyB){
scene->getSystem<PhysicsSystem>()->loadDistanceJoint2D(getComponent<Joint2DComponent>(), bodyA, bodyB, worldAnchorOnBodyA, worldAnchorOnBodyB);
}
Expand Down
2 changes: 2 additions & 0 deletions engine/core/object/physics/Joint2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ namespace Supernova{
Joint2D(const Joint2D& rhs);
Joint2D& operator=(const Joint2D& rhs);

b2Joint* getBox2DJoint() const;

void setDistanceJoint(Entity bodyA, Entity bodyB, Vector2 worldAnchorOnBodyA, Vector2 worldAnchorOnBodyB);
void setRevoluteJoint(Entity bodyA, Entity bodyB, Vector2 worldAnchor);
void setPrismaticJoint(Entity bodyA, Entity bodyB, Vector2 worldAnchor, Vector2 worldAxis);
Expand Down
8 changes: 7 additions & 1 deletion engine/core/subsystem/PhysicsSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "box2d.h"
#include "util/Box2DContactListener.h"

#include "util/Box2DContactFilter.h"

using namespace Supernova;

Expand All @@ -24,8 +24,10 @@ PhysicsSystem::PhysicsSystem(Scene* scene): SubSystem(scene){
world2D = new b2World(gravity);

contactListener2D = new Box2DContactListener(scene, this);
contactFilter2D = new Box2DContactFilter(scene, this);

world2D->SetContactListener(contactListener2D);
world2D->SetContactFilter(contactFilter2D);
}

PhysicsSystem::~PhysicsSystem(){
Expand All @@ -37,6 +39,10 @@ PhysicsSystem::~PhysicsSystem(){
if (contactListener2D){
delete contactListener2D;
}

if (contactFilter2D){
delete contactFilter2D;
}
}

float PhysicsSystem::getPointsToMeterScale(){
Expand Down
4 changes: 4 additions & 0 deletions engine/core/subsystem/PhysicsSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class b2JointDef;
namespace Supernova{

class Box2DContactListener;
class Box2DContactFilter;


class PhysicsSystem : public SubSystem {
Expand All @@ -29,6 +30,7 @@ namespace Supernova{
float pointsToMeterScale;

Box2DContactListener* contactListener2D;
Box2DContactFilter* contactFilter2D;

void updateBodyPosition(Signature signature, Entity entity, Body2DComponent& body, bool updateAnyway);

Expand All @@ -44,6 +46,8 @@ namespace Supernova{
FunctionSubscribe<void(Contact2D, Manifold2D)> preSolve2D;
FunctionSubscribe<void(Contact2D, ContactImpulse2D)> postSolve2D;

FunctionSubscribe<bool(Body2D, size_t, Body2D, size_t)> shouldCollide2D;

void createBody2D(Entity entity);
void removeBody2D(Entity entity);
int addRectShape2D(Entity entity, float width, float height);
Expand Down
41 changes: 41 additions & 0 deletions engine/core/util/Box2DContactFilter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

//
// (c) 2023 Eduardo Doria.
//

#ifndef Box2DContactFilter_h
#define Box2DContactFilter_h

#include "box2d.h"
#include "subsystem/PhysicsSystem.h"

namespace Supernova{

class Box2DContactFilter : public b2ContactFilter{
private:
Scene* scene;
PhysicsSystem* physicsSystem;

public:

Box2DContactFilter(Scene* scene, PhysicsSystem* physicsSystem){
this->scene = scene;
this->physicsSystem = physicsSystem;
}

bool ShouldCollide(b2Fixture* fixtureA, b2Fixture* fixtureB){
size_t shapeIndexA = fixtureA->GetUserData().pointer;
Entity entityA = fixtureA->GetBody()->GetUserData().pointer;
Body2D bodyA(scene, entityA);

size_t shapeIndexB = fixtureB->GetUserData().pointer;
Entity entityB = fixtureB->GetBody()->GetUserData().pointer;
Body2D bodyB(scene, entityB);

return physicsSystem->shouldCollide2D.callRet(bodyA, shapeIndexA, bodyB, shapeIndexB, true);
}
};

}

#endif //Box2DContactFilter_h
15 changes: 10 additions & 5 deletions engine/core/util/FunctionSubscribe.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,19 @@ namespace Supernova {
function(args...);
};
} else {
for (auto& function : functions)
{
return function(args...);
};
return Ret();
return callRet(args..., Ret());
}
}

template<typename T>
Ret callRet(Args... args, T def){
for (auto& function : functions)
{
return function(args...);
};
return def;
}

Ret operator()(Args... args)
{
return call(args...);
Expand Down

0 comments on commit 6ecd2b3

Please sign in to comment.