From 6311f8e42242046fc331a5b0e2d8fee74b01e3c0 Mon Sep 17 00:00:00 2001 From: eduardodoria Date: Thu, 19 Oct 2023 14:27:05 -0300 Subject: [PATCH] Finished Jolt callbacks --- engine/core/object/physics/Contact3D.cpp | 66 +++++++++++++++++++++++- engine/core/object/physics/Contact3D.h | 25 ++++++++- engine/core/subsystem/PhysicsSystem.h | 4 +- engine/core/util/JoltPhysicsAux.h | 20 +++++-- 4 files changed, 107 insertions(+), 8 deletions(-) diff --git a/engine/core/object/physics/Contact3D.cpp b/engine/core/object/physics/Contact3D.cpp index 4664afcf..bf977db7 100644 --- a/engine/core/object/physics/Contact3D.cpp +++ b/engine/core/object/physics/Contact3D.cpp @@ -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(){ @@ -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; -} \ No newline at end of file +} + +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; +} + diff --git a/engine/core/object/physics/Contact3D.h b/engine/core/object/physics/Contact3D.h index 7bb63b9d..458c87af 100644 --- a/engine/core/object/physics/Contact3D.h +++ b/engine/core/object/physics/Contact3D.h @@ -10,6 +10,7 @@ namespace JPH{ class ContactManifold; + class ContactSettings; } namespace Supernova{ @@ -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); }; } diff --git a/engine/core/subsystem/PhysicsSystem.h b/engine/core/subsystem/PhysicsSystem.h index 0d30d59a..7085b6be 100644 --- a/engine/core/subsystem/PhysicsSystem.h +++ b/engine/core/subsystem/PhysicsSystem.h @@ -82,8 +82,8 @@ namespace Supernova{ FunctionSubscribe onBodyActivated3D; FunctionSubscribe onBodyDeactivated3D; FunctionSubscribe onContactAdded3D; - //FunctionSubscribe onContactPersisted3D; - //FunctionSubscribe onContactRemoved3D; + FunctionSubscribe onContactPersisted3D; + FunctionSubscribe onContactRemoved3D; FunctionSubscribe shouldCollide3D; diff --git a/engine/core/util/JoltPhysicsAux.h b/engine/core/util/JoltPhysicsAux.h index 7421ff6b..a70209ee 100644 --- a/engine/core/util/JoltPhysicsAux.h +++ b/engine/core/util/JoltPhysicsAux.h @@ -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); } };