From 76e2ed67317be9accaa57cc9c32dd6325315523c Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 30 Jul 2024 19:38:05 +0300 Subject: [PATCH] Blast toolset work --- modules/core/src/application.cpp | 6 +-- .../src/px/blast/px_blast_destructions.cpp | 25 ++++----- .../core/src/px/blast/px_blast_fracture.cpp | 49 +++++++---------- .../core/src/px/core/px_physics_engine.cpp | 53 ++++++++++--------- modules/core/src/px/core/px_physics_engine.h | 11 +++- modules/core/src/px/physics/px_joint.cpp | 21 +++++--- modules/core/src/px/physics/px_joint.h | 4 ++ .../src/px/physics/px_joint_component.cpp | 26 +++++++++ .../core/src/px/physics/px_joint_component.h | 31 +++++++++++ .../src/px/physics/px_rigidbody_component.h | 4 +- 10 files changed, 149 insertions(+), 81 deletions(-) create mode 100644 modules/core/src/px/physics/px_joint_component.cpp create mode 100644 modules/core/src/px/physics/px_joint_component.h diff --git a/modules/core/src/application.cpp b/modules/core/src/application.cpp index b00a27a98..f355a89b9 100644 --- a/modules/core/src/application.cpp +++ b/modules/core/src/application.cpp @@ -332,15 +332,15 @@ namespace era_engine { model_asset ass = load3DModelFromFile(getAssetPath("/resources/assets/obj/untitled.obj")); - auto& px_sphere_entt1 = scene.createEntity("BlastPXTest") + auto& px_blast_entt1 = scene.createEntity("BlastPXTest") .addComponent(vec3(0.0f, 5.0f, 0.0f), quat::identity, vec3(1.0f)) .addComponent(mesh); physics::fracture fracture; auto ref = make_ref(ass.meshes[0].submeshes[0]); unsigned int seed = 7249U; - fracture.fractureGameObject(ref, px_sphere_entt1, physics::anchor::anchor_none, seed, 15, defaultmat, defaultmat, 1.0f, 3.0f); - scene.deleteEntity(px_sphere_entt1.handle); + fracture.fractureGameObject(ref, px_blast_entt1, physics::anchor::anchor_none, seed, 15, defaultmat, defaultmat, 100.0f, 3.0f); + scene.deleteEntity(px_blast_entt1.handle); } } diff --git a/modules/core/src/px/blast/px_blast_destructions.cpp b/modules/core/src/px/blast/px_blast_destructions.cpp index 81a95684c..ef73faa18 100644 --- a/modules/core/src/px/blast/px_blast_destructions.cpp +++ b/modules/core/src/px/blast/px_blast_destructions.cpp @@ -87,15 +87,12 @@ namespace era_engine::physics { setupRigidbody(); - //if(!isKinematic) - // freeze(); + if(!isKinematic) + freeze(); jointToChunk.clear(); chunkToJoint.clear(); - if (!manager->joints.contains(handle)) - return; - auto enttScene = globalApp.getCurrentScene(); for (auto joint : manager->joints[handle]) @@ -210,8 +207,10 @@ namespace era_engine::physics auto enttScene = globalApp.getCurrentScene(); eentity renderEntity{ handle, &enttScene->registry }; - - renderEntity.getComponent().setConstraints(0); + auto& rb = renderEntity.getComponent(); + rb.setConstraints(0); + rb.setGravity(true); + rb.setFilterMask(-1, -1); } void chunk_graph_manager::chunk_node::freeze() @@ -221,11 +220,15 @@ namespace era_engine::physics auto enttScene = globalApp.getCurrentScene(); eentity renderEntity{ handle, &enttScene->registry }; + auto& transform = renderEntity.getComponent(); - frozenPos = renderEntity.getComponent().position; - frozenRot = renderEntity.getComponent().rotation; + frozenPos = transform.position; + frozenRot = transform.rotation; - renderEntity.getComponent().setConstraints(PX_FREEZE_ALL); + auto& rb = renderEntity.getComponent(); + rb.setGravity(false); + rb.setConstraints(PX_FREEZE_ALL); + rb.setFilterMask(16, 16); } void chunk_graph_manager::setup(std::vector bodies, uint32 generation) @@ -233,8 +236,6 @@ namespace era_engine::physics nbNodes = bodies.size(); nodes.reserve(nbNodes); - physics_holder::physicsRef->unfreezeBlastQueue.reserve(nbNodes * 5); - auto enttScene = globalApp.getCurrentScene(); for (size_t i = 0; i < bodies.size(); i++) diff --git a/modules/core/src/px/blast/px_blast_fracture.cpp b/modules/core/src/px/blast/px_blast_fracture.cpp index 5570b959e..641ac9b90 100644 --- a/modules/core/src/px/blast/px_blast_fracture.cpp +++ b/modules/core/src/px/blast/px_blast_fracture.cpp @@ -7,6 +7,7 @@ #include "px/blast/px_nvmesh.h" #include "px/blast/px_blast_destructions.h" #include "px/core/px_extensions.h" +#include "px/physics/px_joint_component.h" #include @@ -195,10 +196,10 @@ namespace era_engine::physics graphManager.setup(chunks); // Connect blocks that are touching with fixed joints - //for (size_t i = 0; i < chunks.size(); i++) - //{ - // connectTouchingChunks(graphManager, meshes[i].first, chunks[i], jointBreakForce); - //} + for (size_t i = 0; i < chunks.size(); i++) + { + connectTouchingChunks(graphManager, meshes[i].first, chunks[i], jointBreakForce); + } for (auto chunk : chunks) { @@ -206,7 +207,7 @@ namespace era_engine::physics renderEntity.setParent(fractureGameObject); } - //anchorChunks(gameObject.handle, anchor); + anchorChunks(gameObject.handle, anchor); return fractureGameObject.handle; } @@ -375,33 +376,23 @@ namespace era_engine::physics { if (overlap != chunk) { - { - eentity body{ overlap, &enttScene->registry }; - - auto& rbOverlap = body.getComponent(); - - std::vector fd1 = getFilterData(rb.getRigidActor()); - std::vector fd2 = getFilterData(rbOverlap.getRigidActor()); + eentity body{ overlap, &enttScene->registry }; - px_fixed_joint* joint = new px_fixed_joint(px_fixed_joint_desc{ 0.1f, 0.1f, 200.0f, 100.0f }, rb.getRigidActor(), rbOverlap.getRigidActor()); + auto& rbOverlap = body.getComponent(); - joint->joint->setInvInertiaScale0(0.0f); - joint->joint->setInvInertiaScale1(0.0f); - joint->joint->setConstraintFlag(PxConstraintFlag::eCOLLISION_ENABLED, false); + body.addComponent(jointBreakForce); + auto& joint = body.getComponent(); + joint.setPair(rbOverlap.getRigidActor(), rb.getRigidActor()); - setFilterData(rb.getRigidActor(), fd1); - setFilterData(rbOverlap.getRigidActor(), fd2); - - if (manager.joints.contains(chunk)) - { - manager.joints[chunk].push_back(joint); - } - else - { - std::vector jointVec; - jointVec.push_back(joint); - manager.joints.emplace(chunk, jointVec); - } + if (manager.joints.contains(chunk)) + { + manager.joints[chunk].push_back(joint.getJoint()); + } + else + { + std::vector jointVec; + jointVec.push_back(joint.getJoint()); + manager.joints.emplace(chunk, jointVec); } } } diff --git a/modules/core/src/px/core/px_physics_engine.cpp b/modules/core/src/px/core/px_physics_engine.cpp index 2a91a5390..ef415430b 100644 --- a/modules/core/src/px/core/px_physics_engine.cpp +++ b/modules/core/src/px/core/px_physics_engine.cpp @@ -53,9 +53,11 @@ namespace era_engine pairFlags |= physx::PxPairFlag::eNOTIFY_CONTACT_POINTS; pairFlags |= physx::PxPairFlag::eSOLVE_CONTACT; pairFlags |= physx::PxPairFlag::eDETECT_DISCRETE_CONTACT; + + return physx::PxFilterFlag::eDEFAULT; } - - return physx::PxFilterFlag::eDEFAULT; + + return physx::PxFilterFlag::eSUPPRESS; } static void clearColliderFromCollection(const physics::px_body_component* collider, @@ -789,6 +791,8 @@ namespace era_engine newTriggerPairs.clear(); lostTriggerPairs.clear(); + + brokenJoints.clear(); } void physics::px_simulation_event_callback::sendCollisionEvents() @@ -823,40 +827,37 @@ namespace era_engine { } + void physics::px_simulation_event_callback::sendJointEvents() + { + for (auto* joint : brokenJoints) + { + if (joint->onJointBreakCallback) + joint->onJointBreakCallback(joint); + } + } + void physics::px_simulation_event_callback::onColliderRemoved(px_body_component* collider) { clearColliderFromCollection(collider, newTriggerPairs); clearColliderFromCollection(collider, lostTriggerPairs); } - void physics::px_simulation_event_callback::onConstraintBreak(PxConstraintInfo* constraints, PxU32 count) + void physics::px_simulation_event_callback::onJointRemoved(px_joint* joint) { -#if !_DEBUG - PxRigidActor* act1; - PxRigidActor* act2; - constraints->constraint->getActors(act1, act2); - - auto rb1 = physics::physics_holder::physicsRef->actorsMap[act1]; - auto rb2 = physics::physics_holder::physicsRef->actorsMap[act1]; - - if (!rb1 || !rb2) - return; - - auto enttScene = globalApp.getCurrentScene(); - - eentity entt1{ (entity_handle)rb1->entityHandle, &enttScene->registry }; - eentity entt2{ (entity_handle)rb2->entityHandle, &enttScene->registry }; - - if (auto node1 = entt1.getComponentIfExists()) - { - node1->onJointBreak(); - } + brokenJoints.findAndReplaceWithLast(joint); + brokenJoints.popBack(); + } - if (auto node2 = entt2.getComponentIfExists()) + void physics::px_simulation_event_callback::onConstraintBreak(PxConstraintInfo* constraints, PxU32 count) + { + for (uint32 i = 0; i < count; i++) { - node2->onJointBreak(); + PxJoint* joint = reinterpret_cast(constraints[i].externalReference); + if (joint->userData) + { + brokenJoints.pushBack(static_cast(joint->userData)); + } } -#endif } void physics::px_simulation_event_callback::onContact(const PxContactPairHeader& pairHeader, const PxContactPair* pairs, PxU32 nbPairs) diff --git a/modules/core/src/px/core/px_physics_engine.h b/modules/core/src/px/core/px_physics_engine.h index dfa4fe18d..8f51b7eca 100644 --- a/modules/core/src/px/core/px_physics_engine.h +++ b/modules/core/src/px/core/px_physics_engine.h @@ -231,6 +231,7 @@ namespace era_engine::physics struct px_body_component; struct px_collider_component_base; struct px_soft_body; + struct px_joint; struct px_simulation_event_callback; @@ -479,8 +480,12 @@ filterData.data.word2 = hitTriggers ? 1 : 0 void sendTriggerEvents(); + void sendJointEvents(); + void onColliderRemoved(px_body_component* collider); + void onJointRemoved(px_joint* joint); + void onConstraintBreak(PxConstraintInfo* constraints, PxU32 count) override; void onWake(PxActor** actors, PxU32 count) override { std::cout << "onWake\n"; } void onSleep(PxActor** actors, PxU32 count) override { std::cout << "onSleep\n"; } @@ -492,6 +497,8 @@ filterData.data.word2 = hitTriggers ? 1 : 0 PxArray kinematicsToRemoveFlag; + PxArray brokenJoints; + PxArray removedCollisions; PxArray newTriggerPairs; @@ -686,4 +693,6 @@ filterData.data.word2 = hitTriggers ? 1 : 0 #include "px/physics/px_character_controller_component.h" #include "px/features/px_particles.h" #include "px/features/cloth/px_clothing_factory.h" -#include "px/core/px_extensions.h" \ No newline at end of file +#include "px/core/px_extensions.h" +#include "px/physics/px_joint.h" +#include "px/physics/px_joint_component.h" \ No newline at end of file diff --git a/modules/core/src/px/physics/px_joint.cpp b/modules/core/src/px/physics/px_joint.cpp index 57b3fa025..d3f83fca5 100644 --- a/modules/core/src/px/physics/px_joint.cpp +++ b/modules/core/src/px/physics/px_joint.cpp @@ -67,6 +67,7 @@ namespace era_engine::physics px_revolute_joint::px_revolute_joint(px_revolute_joint_desc desc, PxRigidActor* f, PxRigidActor* s) : px_joint(f, s) { joint = createRevoluteJoint(f, s); + joint->userData = this; type = px_joint_type::joint_type_revolute; PxJointAngularLimitPair limitPair(desc.angularPair.lower, desc.angularPair.upper); @@ -74,7 +75,7 @@ namespace era_engine::physics limitPair.damping = desc.angularPair.damping; auto jInstance = joint->is(); - jInstance->setConstraintFlag(PxConstraintFlag::eVISUALIZATION, true); + if (desc.drive.anabledDrive) { jInstance->setDriveVelocity(desc.drive.driveVelocity); @@ -82,7 +83,9 @@ namespace era_engine::physics jInstance->setDriveForceLimit(desc.drive.driveForceLimit); jInstance->setRevoluteJointFlag(PxRevoluteJointFlag::eDRIVE_ENABLED, true); } +#if PX_GPU_BROAD_PHASE jInstance->setConstraintFlag(PxConstraintFlag::eGPU_COMPATIBLE, true); +#endif jInstance->setLimit(limitPair); jInstance->setRevoluteJointFlag(PxRevoluteJointFlag::eLIMIT_ENABLED, true); } @@ -95,7 +98,7 @@ namespace era_engine::physics { joint = createSphericalJoint(f, s); type = px_joint_type::joint_type_spherical; - + joint->userData = this; PxJointLimitCone limitCone(desc.limitCone.yAngle, desc.limitCone.zAngle); @@ -103,8 +106,9 @@ namespace era_engine::physics limitCone.damping = desc.limitCone.damping; auto jInstance = joint->is(); - jInstance->setConstraintFlag(PxConstraintFlag::eVISUALIZATION, true); +#if PX_GPU_BROAD_PHASE jInstance->setConstraintFlag(PxConstraintFlag::eGPU_COMPATIBLE, true); +#endif jInstance->setLimitCone(limitCone); jInstance->setSphericalJointFlag(PxSphericalJointFlag::eLIMIT_ENABLED, true); } @@ -116,6 +120,7 @@ namespace era_engine::physics px_prismatic_joint::px_prismatic_joint(px_prismatic_joint_desc desc, PxRigidActor* f, PxRigidActor* s) : px_joint(f, s) { joint = createPrismaticJoint(f, s); + joint->userData = this; type = px_joint_type::joint_type_prismatic; PxTolerancesScale ts; ts.length = 1.0; @@ -129,8 +134,9 @@ namespace era_engine::physics limitPair.damping = desc.linearPair.damping; auto jInstance = joint->is(); - jInstance->setConstraintFlag(PxConstraintFlag::eVISUALIZATION, true); +#if PX_GPU_BROAD_PHASE jInstance->setConstraintFlag(PxConstraintFlag::eGPU_COMPATIBLE, true); +#endif jInstance->setLimit(limitPair); jInstance->setPrismaticJointFlag(PxPrismaticJointFlag::eLIMIT_ENABLED, true); } @@ -142,9 +148,9 @@ namespace era_engine::physics px_distance_joint::px_distance_joint(px_distance_joint_desc desc, PxRigidActor* f, PxRigidActor* s) : px_joint(f, s) { joint = createDistanceJoint(f, s); + joint->userData = this; type = px_joint_type::joint_type_distance; auto jInstance = joint->is(); - jInstance->setConstraintFlag(PxConstraintFlag::eVISUALIZATION, true); jInstance->setDamping(desc.damping); jInstance->setStiffness(desc.stiffness); jInstance->setDistanceJointFlag(PxDistanceJointFlag::eSPRING_ENABLED, true); @@ -161,10 +167,9 @@ namespace era_engine::physics px_fixed_joint::px_fixed_joint(px_fixed_joint_desc desc, PxRigidActor* f, PxRigidActor* s) : px_joint(f, s) { joint = createFixedJoint(f, s); + joint->userData = this; type = px_joint_type::joint_type_fixed; auto jInstance = joint->is(); - jInstance->setConstraintFlag(PxConstraintFlag::eVISUALIZATION, true); - jInstance->setConstraintFlag(PxConstraintFlag::eVISUALIZATION, false); #if PX_GPU_BROAD_PHASE jInstance->setConstraintFlag(PxConstraintFlag::eGPU_COMPATIBLE, true); #endif @@ -179,9 +184,9 @@ namespace era_engine::physics px_d6_joint::px_d6_joint(px_d6_joint_desc desc, PxRigidActor* f, PxRigidActor* s) : px_joint(f, s) { joint = createD6Joint(f, s); + joint->userData = this; type = px_joint_type::joint_type_d6; auto jInstance = joint->is(); - jInstance->setConstraintFlag(PxConstraintFlag::eVISUALIZATION, true); if (desc.drive.anabledDrive) { auto jointDrive = PxD6JointDrive(); diff --git a/modules/core/src/px/physics/px_joint.h b/modules/core/src/px/physics/px_joint.h index c6afea9eb..62c96479c 100644 --- a/modules/core/src/px/physics/px_joint.h +++ b/modules/core/src/px/physics/px_joint.h @@ -4,6 +4,8 @@ #include "px/core/px_physics_engine.h" +#include + namespace era_engine::physics { using namespace physx; @@ -188,6 +190,8 @@ namespace era_engine::physics px_joint_type type = px_joint_type::joint_type_none; PxJoint* joint = nullptr; + + std::function onJointBreakCallback; }; struct px_revolute_joint : px_joint diff --git a/modules/core/src/px/physics/px_joint_component.cpp b/modules/core/src/px/physics/px_joint_component.cpp new file mode 100644 index 000000000..fd4d52620 --- /dev/null +++ b/modules/core/src/px/physics/px_joint_component.cpp @@ -0,0 +1,26 @@ +// Copyright (c) 2023-present Eldar Muradov. All rights reserved. + +#include "px/physics/px_joint_component.h" + +namespace era_engine::physics +{ + px_fixed_joint_component::px_fixed_joint_component(uint32 handle, float breakForce) + : jointBreakForce(breakForce), px_physics_component_base(handle) + { + } + + px_fixed_joint_component::~px_fixed_joint_component() + { + } + + void px_fixed_joint_component::setPair(PxRigidActor* f, PxRigidActor* s) + { + joint = new px_fixed_joint(px_fixed_joint_desc{10.0f, 10.0f, jointBreakForce, jointBreakForce}, f, s); + } + + void px_fixed_joint_component::release(bool releaseActor) + { + RELEASE_PTR(joint) + } + +} \ No newline at end of file diff --git a/modules/core/src/px/physics/px_joint_component.h b/modules/core/src/px/physics/px_joint_component.h new file mode 100644 index 000000000..d294c7581 --- /dev/null +++ b/modules/core/src/px/physics/px_joint_component.h @@ -0,0 +1,31 @@ +// Copyright (c) 2023-present Eldar Muradov. All rights reserved. + +#pragma once + +#include "px/core/px_physics_engine.h" + +#include "px/physics/px_joint.h" + +namespace era_engine::physics +{ + using namespace physx; + + struct px_fixed_joint; + + struct px_fixed_joint_component : px_physics_component_base + { + px_fixed_joint_component() {}; + px_fixed_joint_component(uint32 handle, float breakForce = 200.0f); + virtual ~px_fixed_joint_component(); + + void setPair(PxRigidActor* f, PxRigidActor* s); + + px_fixed_joint* getJoint() const { return joint; } + + virtual void release(bool releaseActor = false) override; + + private: + px_fixed_joint* joint = nullptr; + float jointBreakForce = 200.0f; + }; +} \ No newline at end of file diff --git a/modules/core/src/px/physics/px_rigidbody_component.h b/modules/core/src/px/physics/px_rigidbody_component.h index db420ad3c..7796bbefb 100644 --- a/modules/core/src/px/physics/px_rigidbody_component.h +++ b/modules/core/src/px/physics/px_rigidbody_component.h @@ -16,7 +16,7 @@ namespace era_engine::physics enum class px_rigidbody_type : uint8 { - rigidbody_type_none, + rigidbody_type_none = 0, rigidbody_type_static, rigidbody_type_dynamic, rigidbody_type_kinematic @@ -24,7 +24,7 @@ namespace era_engine::physics enum class px_force_mode : uint8 { - force_mode_none, + force_mode_none = 0, force_mode_force, force_mode_impulse, force_mode_velocity_change,