Skip to content

Commit

Permalink
Finished Jolt callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardodoria committed Oct 19, 2023
1 parent 4ba2ba9 commit 6311f8e
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 8 deletions.
66 changes: 64 additions & 2 deletions engine/core/object/physics/Contact3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

using namespace Supernova;

Contact3D::Contact3D(Scene* scene, const JPH::ContactManifold* contactManifold){
Contact3D::Contact3D(Scene* scene, const JPH::ContactManifold* contactManifold, JPH::ContactSettings* contactSettings){
this->scene = scene;
this->contactManifold = contactManifold;
this->contactSettings = contactSettings;
}

Contact3D::~Contact3D(){
Expand All @@ -21,16 +22,77 @@ Contact3D::~Contact3D(){
Contact3D::Contact3D(const Contact3D& rhs){
scene = rhs.scene;
contactManifold = rhs.contactManifold;
contactSettings = rhs.contactSettings;

}

Contact3D& Contact3D::operator=(const Contact3D& rhs){
scene = rhs.scene;
contactManifold = rhs.contactManifold;
contactSettings = rhs.contactSettings;

return *this;
}

const JPH::ContactManifold* Contact3D::getJoltContactManifold() const{
return contactManifold;
}
}

JPH::ContactSettings* Contact3D::getJoltContactSettings() const{
return contactSettings;
}

Vector3 Contact3D::getBaseOffset() const{
return Vector3(contactManifold->mBaseOffset.GetX(), contactManifold->mBaseOffset.GetY(), contactManifold->mBaseOffset.GetZ());
}

Vector3 Contact3D::getWorldSpaceNormal() const{
return Vector3(contactManifold->mWorldSpaceNormal.GetX(), contactManifold->mWorldSpaceNormal.GetY(), contactManifold->mWorldSpaceNormal.GetZ());
}

float Contact3D::getPenetrationDepth() const{
return contactManifold->mPenetrationDepth;
}

int32_t Contact3D::getSubShapeID1() const{
return contactManifold->mSubShapeID1.GetValue();
}

int32_t Contact3D::getSubShapeID12() const{
return contactManifold->mSubShapeID2.GetValue();
}

Vector3 Contact3D::getRelativeContactPointsOnA(size_t index) const{
JPH::Vec3 point = contactManifold->mRelativeContactPointsOn1[index];
return Vector3(point.GetX(), point.GetY(), point.GetZ());
}

Vector3 Contact3D::getRelativeContactPointsOnB(size_t index) const{
JPH::Vec3 point = contactManifold->mRelativeContactPointsOn2[index];
return Vector3(point.GetX(), point.GetY(), point.GetZ());
}

float Contact3D::getCombinedFriction() const{
return contactSettings->mCombinedFriction;
}

void Contact3D::setCombinedFriction(float combinedFriction){
contactSettings->mCombinedFriction = combinedFriction;
}

float Contact3D::getCombinedRestitution() const{
return contactSettings->mCombinedRestitution;
}

void Contact3D::setCombinedRestitution(float combinedRestitution){
contactSettings->mCombinedRestitution = combinedRestitution;
}

bool Contact3D::isSensor() const{
return contactSettings->mIsSensor;
}

void Contact3D::setSensor(bool sensor){
contactSettings->mIsSensor = sensor;
}

25 changes: 24 additions & 1 deletion engine/core/object/physics/Contact3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace JPH{
class ContactManifold;
class ContactSettings;
}

namespace Supernova{
Expand All @@ -18,15 +19,37 @@ namespace Supernova{
private:
Scene* scene;
const JPH::ContactManifold* contactManifold;
JPH::ContactSettings* contactSettings;

public:
Contact3D(Scene* scene, const JPH::ContactManifold* contactManifold);
Contact3D(Scene* scene, const JPH::ContactManifold* contactManifold, JPH::ContactSettings* contactSettings);
virtual ~Contact3D();

Contact3D(const Contact3D& rhs);
Contact3D& operator=(const Contact3D& rhs);

const JPH::ContactManifold* getJoltContactManifold() const;
JPH::ContactSettings* getJoltContactSettings() const;

// ContactManifold
Vector3 getBaseOffset() const;
Vector3 getWorldSpaceNormal() const;
float getPenetrationDepth() const;
int32_t getSubShapeID1() const;
int32_t getSubShapeID12() const;
Vector3 getRelativeContactPointsOnA(size_t index) const;
Vector3 getRelativeContactPointsOnB(size_t index) const;


// ContactSettings
float getCombinedFriction() const;
void setCombinedFriction(float combinedFriction);

float getCombinedRestitution() const;
void setCombinedRestitution(float combinedRestitution);

bool isSensor() const;
void setSensor(bool sensor);
};
}

Expand Down
4 changes: 2 additions & 2 deletions engine/core/subsystem/PhysicsSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ namespace Supernova{
FunctionSubscribe<void(Body3D)> onBodyActivated3D;
FunctionSubscribe<void(Body3D)> onBodyDeactivated3D;
FunctionSubscribe<void(Body3D, Body3D, Contact3D)> onContactAdded3D;
//FunctionSubscribe<void(Entity, Entity)> onContactPersisted3D;
//FunctionSubscribe<void(Entity, Entity)> onContactRemoved3D;
FunctionSubscribe<void(Body3D, Body3D, Contact3D)> onContactPersisted3D;
FunctionSubscribe<void(Body3D, Body3D, int, int)> onContactRemoved3D;

FunctionSubscribe<bool(Body3D, Body3D, Vector3, CollideShapeResult3D)> shouldCollide3D;

Expand Down
20 changes: 17 additions & 3 deletions engine/core/util/JoltPhysicsAux.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,29 @@ namespace Supernova{
Entity entity2 = inBody2.GetUserData();
Body3D body2(scene, entity2);

physicsSystem->onContactAdded3D(body1, body2, Contact3D(scene, &inManifold));
physicsSystem->onContactAdded3D(body1, body2, Contact3D(scene, &inManifold, &ioSettings));
}

virtual void OnContactPersisted(const JPH::Body &inBody1, const JPH::Body &inBody2, const JPH::ContactManifold &inManifold, JPH::ContactSettings &ioSettings) override{
printf("OnContactPersisted\n");
Entity entity1 = inBody1.GetUserData();
Body3D body1(scene, entity1);

Entity entity2 = inBody2.GetUserData();
Body3D body2(scene, entity2);

physicsSystem->onContactPersisted3D(body1, body2, Contact3D(scene, &inManifold, &ioSettings));
}

virtual void OnContactRemoved(const JPH::SubShapeIDPair &inSubShapePair) override{
printf("OnContactRemoved\n");
JPH::BodyInterface &body_interface = physicsSystem->getWorld3D()->GetBodyInterface();

Body3D body1(scene, body_interface.GetUserData(inSubShapePair.GetBody1ID()));
Body3D body2(scene, body_interface.GetUserData(inSubShapePair.GetBody2ID()));

int32_t subShapeID1 = inSubShapePair.GetSubShapeID1().GetValue();
int32_t subShapeID2 = inSubShapePair.GetSubShapeID2().GetValue();

physicsSystem->onContactRemoved3D(body1, body2, (int)subShapeID1, (int)subShapeID2);
}
};

Expand Down

0 comments on commit 6311f8e

Please sign in to comment.