Skip to content

Commit

Permalink
[ERA-0] simulated vehicles
Browse files Browse the repository at this point in the history
  • Loading branch information
EldarMuradov committed Aug 21, 2024
1 parent b342237 commit 43d3247
Show file tree
Hide file tree
Showing 23 changed files with 224 additions and 169 deletions.
120 changes: 60 additions & 60 deletions modules/core/src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,37 +331,37 @@ namespace era_engine

#if PX_BLAST_ENABLE

{
//if (auto mesh = loadMeshFromFileAsync(getAssetPath("/resources/assets/obj/untitled.obj")))
//{
// model_asset ass = load3DModelFromFile(getAssetPath("/resources/assets/obj/untitled.obj"));
//{
// if (auto mesh = loadMeshFromFileAsync(getAssetPath("/resources/assets/obj/untitled.obj")))
// {
// model_asset ass = load3DModelFromFile(getAssetPath("/resources/assets/obj/untitled.obj"));

// auto& px_blast_entt1 = scene.createEntity("BlastPXTest")
// .addComponent<transform_component>(vec3(0.0f, 5.0f, 0.0f), quat::identity, vec3(1.0f))
// .addComponent<mesh_component>(mesh);
// auto& px_blast_entt1 = scene.createEntity("BlastPXTest")
// .addComponent<transform_component>(vec3(0.0f, 5.0f, 0.0f), quat::identity, vec3(1.0f))
// .addComponent<mesh_component>(mesh);

// 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, 3, defaultmat, defaultmat, 10.0f, 1.0f);
// scene.deleteEntity(px_blast_entt1.handle);
//}
}
// 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_none, seed, 15, defaultmat, defaultmat, 20.0f, 1.0f);
// scene.deleteEntity(px_blast_entt1.handle);
// }
//}

#endif

/*{
model_asset ass = load3DModelFromFile(getAssetPath("/resources/assets/box.fbx"));
auto px_sphere_entt1 = scene.createEntity("SpherePXTest", (entity_handle)60)
.addComponent<transform_component>(vec3(0, 0.f, 0.f), quat(vec3(0.f, 0.f, 0.f), deg2rad(1.f)), vec3(1.0f))
.addComponent<mesh_component>(boxMesh);
physics::fracture fracture;
auto ref = make_ref<submesh_asset>(ass.meshes[0].submeshes[0]);
manager = fracture.fractureGameObject(ref, px_sphere_entt1, physics::anchor::Bottom, 10, 50, defaultmat, defaultmat, 1.0f, 5000);
scene.deleteEntity(px_sphere_entt1.handle);
}*/
//{
// model_asset ass = load3DModelFromFile(getAssetPath("/resources/assets/box.fbx"));
// auto& px_sphere_entt1 = scene.createEntity("BoxPXTest")
// .addComponent<transform_component>(vec3(0, 0.f, 0.f), quat(vec3(0.f, 0.f, 0.f), deg2rad(1.f)), vec3(1.0f))
// .addComponent<mesh_component>(boxMesh);

// physics::fracture fracture;
// auto ref = make_ref<submesh_asset>(ass.meshes[0].submeshes[0]);
// unsigned int seed = 7249U;
// fracture.fractureGameObject(ref, px_sphere_entt1, physics::anchor::anchor_bottom, seed, 50, defaultmat, defaultmat, 0.75f, 0.1f);
// scene.deleteEntity(px_sphere_entt1.handle);
//}

//auto soft_body = &scene.createEntity("SoftBody")
// .addComponent<transform_component>(vec3(0.f), quat::identity, vec3(1.f))
Expand Down Expand Up @@ -700,40 +700,40 @@ namespace era_engine
for (auto [entityHandle, cgm, _] : scene.group(component_group<physics::chunk_graph_manager, transform_component>).each())
{
cgm.update();
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);

ASSERT(body != nullptr);

physics::physics_lock_read lock{};

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();

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);
}
}
}
}
//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);

// ASSERT(body != nullptr);

// physics::physics_lock_read lock{};

// 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();

// 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
Expand Down
30 changes: 23 additions & 7 deletions modules/core/src/px/blast/px_blast_destructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@

namespace era_engine::physics
{
eentity buildChunk(const trs& transform, ref<pbr_material> insideMaterial, ref<pbr_material> outsideMaterial, std::pair<ref<submesh_asset>, ref<nvmesh>> mesh, float mass, uint32 generation)
eentity buildChunk(const trs& transform,
ref<pbr_material> insideMaterial,
ref<pbr_material> outsideMaterial,
std::pair<ref<submesh_asset>,
ref<nvmesh>> mesh,
float mass,
float& radius,
uint32 generation)
{
auto enttScene = globalApp.getCurrentScene();

Expand Down Expand Up @@ -42,20 +49,29 @@ namespace era_engine::physics
mm->aabb.grow(aabb.minCorner);
mm->aabb.grow(aabb.maxCorner);

radius = mm->aabb.volume();

mm->mesh = builder.createDXMesh();

return chunk;
}

std::vector<entity_handle> buildChunks(const trs& transform, ref<pbr_material> insideMaterial, ref<pbr_material> outsideMaterial, std::vector<::std::pair<ref<submesh_asset>, ref<nvmesh>>> meshes, float chunkMass, uint32 generation)
std::vector<entity_handle> buildChunks(const trs& transform,
ref<pbr_material> insideMaterial,
ref<pbr_material> outsideMaterial,
std::vector<::std::pair<ref<submesh_asset>,
ref<nvmesh>>> meshes,
float chunkMass,
std::vector<float>& radiuses,
uint32 generation)
{
std::vector<entity_handle> handles;

//px_aggregate* aggregate = new px_aggregate(meshes.size(), false);

for (size_t i = 0; i < meshes.size(); ++i)
{
eentity entity = buildChunk(transform, insideMaterial, outsideMaterial, meshes[i], chunkMass, generation);
eentity entity = buildChunk(transform, insideMaterial, outsideMaterial, meshes[i], chunkMass, radiuses[i], generation);
handles.push_back(entity.handle);
//aggregate->addActor(entity.getComponent<px_dynamic_body_component>().getRigidActor());
}
Expand Down Expand Up @@ -95,8 +111,8 @@ namespace era_engine::physics
{
setupRigidbody();

if(!isKinematic)
freeze();
/*if(!isKinematic)
freeze();*/

jointToChunk.clear();
chunkToJoint.clear();
Expand Down Expand Up @@ -270,7 +286,7 @@ namespace era_engine::physics

void chunk_graph_manager::update()
{
auto enttScene = globalApp.getCurrentScene();
/*auto enttScene = globalApp.getCurrentScene();
std::vector<std::pair<entity_handle, px_fixed_joint*>> brokenLinks;
Expand Down Expand Up @@ -312,7 +328,7 @@ namespace era_engine::physics
}
if (runSearch)
searchGraph(nodes);
searchGraph(nodes);*/
}

void chunk_graph_manager::searchGraph(std::vector<entity_handle> objects)
Expand Down
19 changes: 16 additions & 3 deletions modules/core/src/px/blast/px_blast_destructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,22 @@

namespace era_engine::physics
{
eentity buildChunk(const trs& transform, ref<pbr_material> insideMaterial, ref<pbr_material> outsideMaterial, std::pair<ref<submesh_asset>, ref<nvmesh>> mesh, float mass, uint32 generation);

std::vector<entity_handle> buildChunks(const trs& transform, ref<pbr_material> insideMaterial, ref<pbr_material> outsideMaterial, std::vector<::std::pair<ref<submesh_asset>, ref<nvmesh>>> meshes, float chunkMass, uint32 generation = 0);
eentity buildChunk(const trs& transform,
ref<pbr_material> insideMaterial,
ref<pbr_material> outsideMaterial,
std::pair<ref<submesh_asset>,
ref<nvmesh>> mesh,
float mass,
float& radius,
uint32 generation);

std::vector<entity_handle> buildChunks(const trs& transform, ref<pbr_material> insideMaterial,
ref<pbr_material> outsideMaterial,
std::vector<::std::pair<ref<submesh_asset>,
ref<nvmesh>>> meshes,
float chunkMass,
std::vector<float>& radiuses,
uint32 generation = 0);

static inline uint32 id = 0;

Expand Down
17 changes: 10 additions & 7 deletions modules/core/src/px/blast/px_blast_fracture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,21 @@ namespace era_engine::physics
else
meshes = fractureMeshesInNvblast(totalChunks, nvMesh);

std::vector<float> radiusArray(totalChunks);

// Build chunks gameobjects
float chunkMass = volumeOfMesh(meshAsset) * density / totalChunks;
auto chunks = buildChunks(gameObject.getComponent<transform_component>(), insideMaterial, outsideMaterial, meshes, chunkMass);
auto chunks = buildChunks(gameObject.getComponent<transform_component>(), insideMaterial, outsideMaterial, meshes, chunkMass, radiusArray);

// Graph manager freezes/unfreezes blocks depending on whether they are connected to the graph or not
graphManager.setup(chunks);

physics::physics_holder::physicsRef->update(0.016f);
//physics::physics_holder::physicsRef->update(0.016f);
// 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, radiusArray, i);
//}

for (auto chunk : chunks)
{
Expand Down Expand Up @@ -347,7 +349,7 @@ namespace era_engine::physics
return anchoredChunks;
}

void fracture::connectTouchingChunks(chunk_graph_manager& manager, ref<submesh_asset> asset, entity_handle chunk, float jointBreakForce, float touchRadius)
void fracture::connectTouchingChunks(chunk_graph_manager& manager, ref<submesh_asset> asset, entity_handle chunk, float jointBreakForce, std::vector<float>& radiuses, int index, float touchRadius)
{
auto enttScene = globalApp.getCurrentScene();
eentity entt{ chunk, &enttScene->registry };
Expand Down Expand Up @@ -380,6 +382,7 @@ namespace era_engine::physics
eentity body{ overlap, &enttScene->registry };

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

std::vector<PxFilterData> fd1 = getFilterData(rb.getRigidActor());
std::vector<PxFilterData> fd2 = getFilterData(rbOverlap.getRigidActor());
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/px/blast/px_blast_fracture.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace era_engine::physics

std::unordered_set<entity_handle> getAnchoredColliders(anchor anchor, const trs& meshTransform, const bounds& bounds);

void connectTouchingChunks(chunk_graph_manager& manager, ref<submesh_asset> asset, entity_handle chunk, float jointBreakForce, float touchRadius = 0.01f);
void connectTouchingChunks(chunk_graph_manager& manager, ref<submesh_asset> asset, entity_handle chunk, float jointBreakForce, std::vector<float>& radiuses, int index, float touchRadius = 0.01f);

std::unordered_set<chunkPair> jointPairs;
};
Expand Down
Loading

0 comments on commit 43d3247

Please sign in to comment.