Skip to content

Commit

Permalink
Blast toolset work
Browse files Browse the repository at this point in the history
  • Loading branch information
EldarMuradov committed Aug 5, 2024
1 parent 2a22149 commit b1b3fb0
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 86 deletions.
2 changes: 1 addition & 1 deletion apps/EraEditor/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ int main(int argc, char** argv)
using namespace era_engine;

#ifndef _DEBUG || DEBUG
::ShowWindow(::GetConsoleWindow(), SW_HIDE);
::ShowWindow(::GetConsoleWindow(), SW_SHOW);
#endif

try
Expand Down
88 changes: 62 additions & 26 deletions modules/core/src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ namespace era_engine
.addComponent<mesh_component>(sphereMesh)
.addComponent<physics::px_sphere_collider_component>(5.0f)
.addComponent<physics::px_dynamic_body_component>();
px_sphere1->getComponent<physics::px_dynamic_body_component>().setCCD(true);
px_sphere1->getComponent<physics::px_dynamic_body_component>().setMass(500.0f);
//px_sphere1->getComponent<physics::px_dynamic_body_component>().setFilterMask(2, 1 | 3);

Expand Down Expand Up @@ -340,7 +341,7 @@ namespace era_engine
physics::fracture fracture;
auto ref = make_ref<submesh_asset>(ass.meshes[0].submeshes[0]);
unsigned int seed = 7249U;
fracture.fractureGameObject(ref, px_blast_entt1, physics::anchor::anchor_bottom, seed, 50, defaultmat, defaultmat, 2500.0f, 1.0f);
fracture.fractureGameObject(ref, px_blast_entt1, physics::anchor::anchor_bottom, seed, 50, defaultmat, defaultmat, 400.0f, 1.0f);
scene.deleteEntity(px_blast_entt1.handle);
}
}
Expand Down Expand Up @@ -662,6 +663,17 @@ namespace era_engine
if (running)
updatePhysXCallbacksAndScripting(scene, linker, dt, input);

#ifndef ERA_RUNTIME

eentity selectedEntity = editor.selectedEntity;

#else

eentity selectedEntity{};

#endif


#if PX_GPU_BROAD_PHASE
{
CPU_PROFILE_BLOCK("PhysX GPU clothes render step");
Expand All @@ -686,20 +698,44 @@ namespace era_engine
for (auto [entityHandle, cgm, _] : scene.group(component_group<physics::chunk_graph_manager, transform_component>).each())
{
cgm.update();
}
}
#endif
updateTestScene(dt, scene, input);
for (auto&[handle, joints] : cgm.joints)
{
if (joints.empty())
continue;
//if (selectedEntity != handle)
//continue;
for (auto& joint : joints)
{
entity_handle h = *(entity_handle*)joint->second->userData;
eentity entity{ h, &scene.registry };
if (physics::px_convex_mesh_collider_component* cm = entity.getComponentIfExists<physics::px_convex_mesh_collider_component>())
{
physics::px_body_component* body = physics::getBodyComponent(entity);

#ifndef ERA_RUNTIME
ASSERT(body != nullptr);

eentity selectedEntity = editor.selectedEntity;
physics::physics_lock_read lock{};

#else
physx::PxShape* shape[1];
body->getRigidActor()->getShapes(shape, 1);
auto geom = (physx::PxConvexMeshGeometry*)cm->getGeometry();
auto mesh = geom->convexMesh;

eentity selectedEntity{};
auto vertices = mesh->getVertices();
auto nbv = mesh->getNbVertices();

for (size_t i = 0; i < nbv; i++)
{
vec3 a = physx::createVec3(vertices[i] + shape[0]->getLocalPose().p) + entity.getComponent<transform_component>().position;
renderPoint(a, vec4(1, 0, 0, 1), &ldrRenderPass, true);
}
}
}
}
}
}
#endif
updateTestScene(dt, scene, input);

//if (renderer->mode != renderer_mode_pathtraced)
{
Expand Down Expand Up @@ -762,28 +798,28 @@ namespace era_engine
}
else if (physics::px_convex_mesh_collider_component* cm = selectedEntity.getComponentIfExists<physics::px_convex_mesh_collider_component>())
{
physics::px_body_component* body = nullptr;
body = selectedEntity.getComponentIfExists<physics::px_dynamic_body_component>();
if(!body)
body = selectedEntity.getComponentIfExists<physics::px_static_body_component>();
//physics::px_body_component* body = nullptr;
//body = selectedEntity.getComponentIfExists<physics::px_dynamic_body_component>();
//if(!body)
// body = selectedEntity.getComponentIfExists<physics::px_static_body_component>();

ASSERT(body != nullptr);
//ASSERT(body != nullptr);

physics::physics_lock_read lock{};
//physics::physics_lock_read lock{};

physx::PxShape* shape[1];
body->getRigidActor()->getShapes(shape, 1);
auto geom = (physx::PxConvexMeshGeometry*)cm->getGeometry();
auto mesh = geom->convexMesh;
//physx::PxShape* shape[1];
//body->getRigidActor()->getShapes(shape, 1);
//auto geom = (physx::PxConvexMeshGeometry*)cm->getGeometry();
//auto mesh = geom->convexMesh;

auto vertices = mesh->getVertices();
auto nbv = mesh->getNbVertices();
//auto vertices = mesh->getVertices();
//auto nbv = mesh->getNbVertices();

for (size_t i = 0; i < nbv; i++)
{
vec3 a = physx::createVec3(vertices[i] + shape[0]->getLocalPose().p) + selectedEntity.getComponent<transform_component>().position;
renderPoint(a, vec4(1, 0, 0, 1), &ldrRenderPass, true);
}
//for (size_t i = 0; i < nbv; i++)
//{
// vec3 a = physx::createVec3(vertices[i] + shape[0]->getLocalPose().p) + selectedEntity.getComponent<transform_component>().position;
// renderPoint(a, vec4(1, 0, 0, 1), &ldrRenderPass, true);
//}
}
}
#endif
Expand Down
42 changes: 31 additions & 11 deletions modules/core/src/px/blast/px_blast_destructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ namespace era_engine::physics

eentity chunk = enttScene->createEntity(asset.name.c_str())
.addComponent<transform_component>(transform)
.addComponent<physics::px_convex_mesh_collider_component>(&asset)
.addComponent<physics::px_dynamic_body_component>()
.addComponent<px_convex_mesh_collider_component>(&asset)
.addComponent<px_dynamic_body_component>()
.addComponent<nvmesh_chunk_component>(mesh.second)
.addComponent<mesh_component>(mm);

Expand Down Expand Up @@ -166,6 +166,7 @@ namespace era_engine::physics
eentity renderEntity{ body, &enttScene->registry };

renderEntity.getComponent<chunk_graph_manager::chunk_node>().remove(handle);

neighbours.erase(body);
}

Expand All @@ -183,16 +184,13 @@ namespace era_engine::physics

auto& rb = renderEntity.getComponent<px_dynamic_body_component>();

constexpr float chunkMass = 1.0f;

rb.setMaxAngularVelosity(100.0f);
rb.setAngularDamping(0.01f);
rb.setLinearDamping(0.01f);
rb.setAngularDamping(0.1f);
rb.setLinearDamping(0.2f);

auto dyn = rb.getRigidDynamic();
dyn->setMaxDepenetrationVelocity(3.0f);

rb.updateMassAndInertia(chunkMass);
dyn->setSolverIterationCounts(16);
rb.updateMassAndInertia(rb.getMass());

if (rb.isKinematicBody())
isKinematic = true;
Expand All @@ -203,7 +201,7 @@ namespace era_engine::physics
auto enttScene = globalApp.getCurrentScene();

eentity renderEntity{ handle, &enttScene->registry };
auto& rb = renderEntity.getComponent<physics::px_dynamic_body_component>();
auto& rb = renderEntity.getComponent<px_dynamic_body_component>();
if (rb.isKinematicBody())
return;
rb.setConstraints(0);
Expand Down Expand Up @@ -265,14 +263,20 @@ namespace era_engine::physics
{
auto enttScene = globalApp.getCurrentScene();

std::vector<std::pair<entity_handle, px_fixed_joint*>> brokenLinks;

for (auto& node : joints)
{
eentity renderEntity{ node.first, &enttScene->registry };

for (auto& joint : node.second)
{
if (joint->joint->getConstraintFlags() & PxConstraintFlag::eBROKEN)
{
brokenLinks.push_back({node.first, joint});
renderEntity.getComponent<chunk_graph_manager::chunk_node>().onJointBreak();
}

}
}

Expand All @@ -293,6 +297,11 @@ namespace era_engine::physics
}
}

for (auto&[h, j] : brokenLinks)
{
remove(h, j);
}

if (runSearch)
searchGraph(nodes);
}
Expand Down Expand Up @@ -339,6 +348,17 @@ namespace era_engine::physics
}
}

void chunk_graph_manager::remove(entity_handle handle, px_fixed_joint* joint)
{
auto& jointsList = joints[handle];
auto end = jointsList.end();
for (auto iter = jointsList.begin(); iter != end; ++iter)
{
if (*iter == joint)
jointsList.erase(iter);
}
}

void chunk_graph_manager::traverse(chunk_node* o, std::unordered_set<chunk_node*>& search, std::unordered_set<chunk_node*>& visited)
{
if (search.contains(o) && visited.contains(o) == false)
Expand All @@ -350,7 +370,7 @@ namespace era_engine::physics
for (auto n : o->neighbours)
{
eentity renderEntity{ n, &enttScene->registry };
traverse(renderEntity.getComponentIfExists<physics::chunk_graph_manager::chunk_node>(), search, visited);
traverse(renderEntity.getComponentIfExists<chunk_graph_manager::chunk_node>(), search, visited);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions modules/core/src/px/blast/px_blast_destructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ namespace era_engine::physics

void searchGraph(std::vector<entity_handle> objects);

void remove(entity_handle handle, px_fixed_joint* joint);

void traverse(chunk_node* o, std::unordered_set<chunk_node*>& search, std::unordered_set<chunk_node*>& visited);

std::vector<entity_handle> nodes;
Expand Down
9 changes: 9 additions & 0 deletions modules/core/src/px/blast/px_blast_fracture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,19 @@ namespace era_engine::physics

auto& rbOverlap = body.getComponent<physics::px_dynamic_body_component>();

std::vector<PxFilterData> fd1 = getFilterData(rb.getRigidActor());
std::vector<PxFilterData> fd2 = getFilterData(rbOverlap.getRigidActor());

body.addComponent<px_fixed_joint_component>(jointBreakForce);
auto& joint = body.getComponent<px_fixed_joint_component>();
joint.setPair(rbOverlap.getRigidActor(), rb.getRigidActor());

joint.getJoint()->joint->setInvInertiaScale0(0.0f);
joint.getJoint()->joint->setInvInertiaScale1(0.0f);

setFilterData(rb.getRigidActor(), fd1);
setFilterData(rbOverlap.getRigidActor(), fd2);

if (manager.joints.contains(overlap))
{
manager.joints[overlap].push_back(joint.getJoint());
Expand Down
26 changes: 5 additions & 21 deletions modules/core/src/px/core/px_physics_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,18 +277,6 @@ namespace era_engine
{
ASSERT(omniPvd->startSampling());
}

/*PxPvdSceneClient* client = scene->getScenePvdClient();
if (client)
{
client->setScenePvdFlag(PxPvdSceneFlag::eTRANSMIT_CONSTRAINTS, true);
client->setScenePvdFlag(PxPvdSceneFlag::eTRANSMIT_CONTACTS, true);
client->setScenePvdFlag(PxPvdSceneFlag::eTRANSMIT_SCENEQUERIES, true);
}
if (pvd->isConnected())
std::cout << "Physics> PVD Connection enabled.\n";*/
#endif

dispatcher = PxDefaultCpuDispatcherCreate(nbCPUDispatcherThreads);
Expand All @@ -299,7 +287,6 @@ namespace era_engine
sceneDesc.solverType = PxSolverType::eTGS;
sceneDesc.flags |= PxSceneFlag::eENABLE_CCD;
sceneDesc.flags |= PxSceneFlag::eENABLE_ACTIVE_ACTORS;
sceneDesc.flags |= PxSceneFlag::eENABLE_ENHANCED_DETERMINISM;
sceneDesc.flags |= PxSceneFlag::eENABLE_STABILIZATION;
sceneDesc.flags |= PxSceneFlag::eENABLE_PCM;
#if PX_GPU_BROAD_PHASE
Expand All @@ -322,9 +309,8 @@ namespace era_engine
scene = physics->createScene(sceneDesc);

scene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_SHAPES, 1.0f);
scene->setVisualizationParameter(PxVisualizationParameter::eJOINT_LIMITS, 1.0f);
scene->setVisualizationParameter(PxVisualizationParameter::eJOINT_LOCAL_FRAMES, 1.0f);

scene->setVisualizationParameter(PxVisualizationParameter::eCONTACT_POINT, 1.0f);
scene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_FNORMALS, 1.0f);
scene->setVisualizationParameter(PxVisualizationParameter::eSCALE, 1.0f);

#if PX_ENABLE_RAYCAST_CCD
Expand Down Expand Up @@ -857,6 +843,7 @@ namespace era_engine
if (joint->userData)
{
brokenJoints.pushBack(static_cast<px_joint*>(joint->userData));
std::cout << "pizda\n";
}
}
}
Expand Down Expand Up @@ -992,11 +979,8 @@ namespace era_engine
cookingParams.buildGPUData = true;
#endif
cookingParams.convexMeshCookingType = PxConvexMeshCookingType::eQUICKHULL;
cookingParams.gaussMapLimit = 32;
cookingParams.suppressTriangleMeshRemapTable = false;
cookingParams.midphaseDesc = PxMeshMidPhase::eBVH34;
cookingParams.meshPreprocessParams = PxMeshPreprocessingFlag::eDISABLE_ACTIVE_EDGES_PRECOMPUTE;
return PxCreateConvexMesh(cookingParams, desc);
cookingParams.meshPreprocessParams = PxMeshPreprocessingFlag::eENABLE_INERTIA;
return PxCreateConvexMesh(cookingParams, desc, physics::physics_holder::physicsRef->getPhysics()->getPhysicsInsertionCallback());
}
}
catch (...)
Expand Down
36 changes: 23 additions & 13 deletions modules/core/src/px/physics/px_collider_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,30 @@ namespace era_engine::physics

PxI32 targetTriangleCount = totalTrianglesCount < 5000 ? totalTrianglesCount : totalTrianglesCount * 0.66f;

PxTetMaker::simplifyTriangleMesh(vertices, indices, targetTriangleCount, maximalTriangleEdgeLength, outputVertices, outputIndices);

PxTriangleMeshDesc meshDesc;
meshDesc.points.count = outputVertices.size();
meshDesc.points.stride = sizeof(physx::PxVec3);
meshDesc.points.data = &outputVertices[0];

meshDesc.triangles.count = outputIndices.size() / 3;
meshDesc.triangles.stride = 3 * sizeof(PxU32);
meshDesc.triangles.data = &outputIndices[0];
if (targetTriangleCount != totalTrianglesCount)
{
PxTetMaker::simplifyTriangleMesh(vertices, indices, targetTriangleCount, maximalTriangleEdgeLength, outputVertices, outputIndices);

meshDesc.points.count = outputVertices.size();
meshDesc.points.stride = sizeof(physx::PxVec3);
meshDesc.points.data = &outputVertices[0];

meshDesc.triangles.count = outputIndices.size() / 3;
meshDesc.triangles.stride = 3 * sizeof(PxU32);
meshDesc.triangles.data = &outputIndices[0];
}
else
{
meshDesc.points.count = vertices.size();
meshDesc.points.stride = sizeof(physx::PxVec3);
meshDesc.points.data = &vertices[0];

meshDesc.triangles.count = indices.size() / 3;
meshDesc.triangles.stride = 3 * sizeof(PxU32);
meshDesc.triangles.data = &indices[0];
}

px_triangle_mesh_builder mesh_adapter;

Expand Down Expand Up @@ -337,11 +351,7 @@ namespace era_engine::physics
meshDesc.points.count = vertices.size();
meshDesc.points.stride = sizeof(PxVec3);
meshDesc.points.data = &vertices[0];
meshDesc.flags = PxConvexFlag::eCOMPUTE_CONVEX | PxConvexFlag::eQUANTIZE_INPUT;

#if PX_GPU_BROAD_PHASE
meshDesc.flags |= PxConvexFlag::eGPU_COMPATIBLE;
#endif
meshDesc.flags = PxConvexFlag::eCOMPUTE_CONVEX | PxConvexFlag::eDISABLE_MESH_VALIDATION | PxConvexFlag::eFAST_INERTIA_COMPUTATION;

px_convex_mesh_builder mesh_adapter;

Expand Down
Loading

0 comments on commit b1b3fb0

Please sign in to comment.