From 2a221497cdb5ab2f37c01efdf7551a400056989c Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 31 Jul 2024 20:56:49 +0300 Subject: [PATCH] Blast toolset work --- modules/core/src/application.cpp | 23 ++++++++++--------- .../src/px/blast/px_blast_destructions.cpp | 22 +++++++++--------- .../core/src/px/blast/px_blast_fracture.cpp | 12 +++++----- .../core/src/px/core/px_physics_engine.cpp | 1 + modules/core/src/px/physics/px_joint.cpp | 3 ++- .../src/px/physics/px_joint_component.cpp | 2 +- .../src/px/physics/px_rigidbody_component.cpp | 6 +++-- 7 files changed, 37 insertions(+), 32 deletions(-) diff --git a/modules/core/src/application.cpp b/modules/core/src/application.cpp index f355a89b..2fca4d44 100644 --- a/modules/core/src/application.cpp +++ b/modules/core/src/application.cpp @@ -302,6 +302,7 @@ namespace era_engine .addComponent(1.0f) .addComponent(); px_sphere_entt.getComponent().setMass(1000.f); + px_sphere_entt.getComponent().setCCD(true); //px_sphere_entt.getComponent().setFilterMask(1, 2); sphere = px_sphere_entt.handle; @@ -339,7 +340,7 @@ namespace era_engine physics::fracture fracture; auto ref = make_ref(ass.meshes[0].submeshes[0]); unsigned int seed = 7249U; - fracture.fractureGameObject(ref, px_blast_entt1, physics::anchor::anchor_none, seed, 15, defaultmat, defaultmat, 100.0f, 3.0f); + fracture.fractureGameObject(ref, px_blast_entt1, physics::anchor::anchor_bottom, seed, 50, defaultmat, defaultmat, 2500.0f, 1.0f); scene.deleteEntity(px_blast_entt1.handle); } } @@ -612,7 +613,7 @@ namespace era_engine eentity sphereEntity{ sphere, &scene.registry }; if (input.keyboard['G'].pressEvent) { - sphereEntity.getComponent().addForce(vec3(20.0f, 1.0f, 0.0f), physics::px_force_mode::force_mode_impulse); + sphereEntity.getComponent().addForce(vec3(200.0f, 1.0f, 0.0f), physics::px_force_mode::force_mode_impulse); } } } @@ -679,15 +680,15 @@ namespace era_engine } #endif - - //{ - // CPU_PROFILE_BLOCK("PhysX blast chuncks"); - // for (auto [entityHandle, cgm, _] : scene.group(component_group).each()) - // { - // cgm.update(); - // } - //} - +#if PX_BLAST_ENABLE + { + CPU_PROFILE_BLOCK("PhysX blast chuncks"); + for (auto [entityHandle, cgm, _] : scene.group(component_group).each()) + { + cgm.update(); + } + } +#endif updateTestScene(dt, scene, input); #ifndef ERA_RUNTIME diff --git a/modules/core/src/px/blast/px_blast_destructions.cpp b/modules/core/src/px/blast/px_blast_destructions.cpp index ef73faa1..2dd318b1 100644 --- a/modules/core/src/px/blast/px_blast_destructions.cpp +++ b/modules/core/src/px/blast/px_blast_destructions.cpp @@ -67,7 +67,7 @@ namespace era_engine::physics void chunk_graph_manager::chunk_node::update() { - if (frozen) + if (frozen && !isKinematic) { auto enttScene = globalApp.getCurrentScene(); @@ -160,9 +160,6 @@ namespace era_engine::physics { auto body = jointToChunk[link]; - link->joint->setInvInertiaScale0(0.0f); - link->joint->setInvInertiaScale1(0.0f); - jointToChunk.erase(link); chunkToJoint.erase(body); @@ -186,31 +183,34 @@ namespace era_engine::physics auto& rb = renderEntity.getComponent(); - constexpr float chunkMass = 3.0f; + constexpr float chunkMass = 1.0f; rb.setMaxAngularVelosity(100.0f); rb.setAngularDamping(0.01f); rb.setLinearDamping(0.01f); auto dyn = rb.getRigidDynamic(); - dyn->setSolverIterationCounts(4, 4); dyn->setMaxDepenetrationVelocity(3.0f); - dyn->setMaxContactImpulse(1000.0f); rb.updateMassAndInertia(chunkMass); + + if (rb.isKinematicBody()) + isKinematic = true; } void chunk_graph_manager::chunk_node::unfreeze() { - frozen = false; - auto enttScene = globalApp.getCurrentScene(); eentity renderEntity{ handle, &enttScene->registry }; auto& rb = renderEntity.getComponent(); + if (rb.isKinematicBody()) + return; rb.setConstraints(0); rb.setGravity(true); rb.setFilterMask(-1, -1); + + frozen = false; } void chunk_graph_manager::chunk_node::freeze() @@ -284,7 +284,7 @@ namespace era_engine::physics eentity renderEntity{ node, &enttScene->registry }; auto nodeComponent = renderEntity.getComponentIfExists(); - + nodeComponent->update(); if (nodeComponent->hasBrokenLinks) { nodeComponent->cleanBrokenLinks(); @@ -335,7 +335,7 @@ namespace era_engine::physics } for (auto sub : search) { - sub->addToUnfreezeQueue(); + sub->unfreeze(); } } diff --git a/modules/core/src/px/blast/px_blast_fracture.cpp b/modules/core/src/px/blast/px_blast_fracture.cpp index 641ac9b9..7173728f 100644 --- a/modules/core/src/px/blast/px_blast_fracture.cpp +++ b/modules/core/src/px/blast/px_blast_fracture.cpp @@ -192,9 +192,6 @@ namespace era_engine::physics float chunkMass = volumeOfMesh(meshAsset) * density / totalChunks; auto chunks = buildChunks(gameObject.getComponent(), insideMaterial, outsideMaterial, meshes, chunkMass); - // Graph manager freezes/unfreezes blocks depending on whether they are connected to the graph or not - graphManager.setup(chunks); - // Connect blocks that are touching with fixed joints for (size_t i = 0; i < chunks.size(); i++) { @@ -209,6 +206,9 @@ namespace era_engine::physics anchorChunks(gameObject.handle, anchor); + // Graph manager freezes/unfreezes blocks depending on whether they are connected to the graph or not + graphManager.setup(chunks); + return fractureGameObject.handle; } @@ -384,15 +384,15 @@ namespace era_engine::physics auto& joint = body.getComponent(); joint.setPair(rbOverlap.getRigidActor(), rb.getRigidActor()); - if (manager.joints.contains(chunk)) + if (manager.joints.contains(overlap)) { - manager.joints[chunk].push_back(joint.getJoint()); + manager.joints[overlap].push_back(joint.getJoint()); } else { std::vector jointVec; jointVec.push_back(joint.getJoint()); - manager.joints.emplace(chunk, jointVec); + manager.joints.emplace(overlap, jointVec); } } } diff --git a/modules/core/src/px/core/px_physics_engine.cpp b/modules/core/src/px/core/px_physics_engine.cpp index ef415430..5071ef8e 100644 --- a/modules/core/src/px/core/px_physics_engine.cpp +++ b/modules/core/src/px/core/px_physics_engine.cpp @@ -737,6 +737,7 @@ namespace era_engine { simulationEventCallback->sendCollisionEvents(); simulationEventCallback->sendTriggerEvents(); + simulationEventCallback->sendJointEvents(); simulationEventCallback->clear(); } diff --git a/modules/core/src/px/physics/px_joint.cpp b/modules/core/src/px/physics/px_joint.cpp index d3f83fca..ec79154d 100644 --- a/modules/core/src/px/physics/px_joint.cpp +++ b/modules/core/src/px/physics/px_joint.cpp @@ -173,7 +173,8 @@ namespace era_engine::physics #if PX_GPU_BROAD_PHASE jInstance->setConstraintFlag(PxConstraintFlag::eGPU_COMPATIBLE, true); #endif - //jInstance->setConstraintFlag(PxConstraintFlag::eALWAYS_UPDATE, true); + jInstance->setConstraintFlag(PxConstraintFlag::eCOLLISION_ENABLED, false); + jInstance->setConstraintFlag(PxConstraintFlag::eVISUALIZATION, true); jInstance->setBreakForce(desc.forceThreshold, desc.torqueThreshold); } diff --git a/modules/core/src/px/physics/px_joint_component.cpp b/modules/core/src/px/physics/px_joint_component.cpp index fd4d5262..1b51c2ca 100644 --- a/modules/core/src/px/physics/px_joint_component.cpp +++ b/modules/core/src/px/physics/px_joint_component.cpp @@ -15,7 +15,7 @@ namespace era_engine::physics 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); + joint = new px_fixed_joint(px_fixed_joint_desc{10.0f, 10.0f, jointBreakForce, jointBreakForce * 2.0f}, f, s); } void px_fixed_joint_component::release(bool releaseActor) diff --git a/modules/core/src/px/physics/px_rigidbody_component.cpp b/modules/core/src/px/physics/px_rigidbody_component.cpp index 2ad698e2..693e41de 100644 --- a/modules/core/src/px/physics/px_rigidbody_component.cpp +++ b/modules/core/src/px/physics/px_rigidbody_component.cpp @@ -29,13 +29,15 @@ namespace era_engine::physics { physics_lock_write lock{}; + actor->setGlobalPose(PxTransform(createPxVec3(pos), createPxQuat(rot))); + if (auto dyn = actor->is()) { + if (dyn->getRigidBodyFlags() & PxRigidBodyFlag::eKINEMATIC) + return; dyn->setAngularVelocity(PxVec3(0.0f)); dyn->setLinearVelocity(PxVec3(0.0f)); } - - actor->setGlobalPose(PxTransform(createPxVec3(pos), createPxQuat(rot))); } uint32 px_body_component::getFilterMask() const